Skip to content

Commit

Permalink
Improve argument errors
Browse files Browse the repository at this point in the history
  • Loading branch information
TomWright committed Nov 18, 2022
1 parent 2694719 commit d46d974
Show file tree
Hide file tree
Showing 16 changed files with 125 additions and 37 deletions.
64 changes: 63 additions & 1 deletion func.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package dasel

import "fmt"
import (
"fmt"
"strings"
)

type ErrUnknownFunction struct {
Function string
Expand All @@ -15,6 +18,21 @@ func (e ErrUnknownFunction) Is(other error) bool {
return ok
}

type ErrUnexpectedFunctionArgs struct {
Function string
Args []string
Message string
}

func (e ErrUnexpectedFunctionArgs) Error() string {
return fmt.Sprintf("unexpected function args: %s(%s): %s", e.Function, strings.Join(e.Args, ", "), e.Message)
}

func (e ErrUnexpectedFunctionArgs) Is(other error) bool {
_, ok := other.(ErrUnexpectedFunctionArgs)
return ok
}

func standardFunctions() *FunctionCollection {
collection := &FunctionCollection{}
collection.Add(
Expand Down Expand Up @@ -107,3 +125,47 @@ func (bf BasicFunction) AlternativeSelector(part string) *Selector {
}
return bf.alternativeSelectorFn(part)
}

func requireNoArgs(name string, args []string) error {
if len(args) > 0 {
return &ErrUnexpectedFunctionArgs{
Function: name,
Args: args,
Message: "0 arguments expected",
}
}
return nil
}

func requireExactlyXArgs(name string, args []string, x int) error {
if len(args) != x {
return &ErrUnexpectedFunctionArgs{
Function: name,
Args: args,
Message: fmt.Sprintf("exactly %d arguments expected", x),
}
}
return nil
}

func requireXOrMoreArgs(name string, args []string, x int) error {
if len(args) < x {
return &ErrUnexpectedFunctionArgs{
Function: name,
Args: args,
Message: fmt.Sprintf("expected %d or more arguments", x),
}
}
return nil
}

func requireModulusXArgs(name string, args []string, x int) error {
if len(args)%x != 0 {
return &ErrUnexpectedFunctionArgs{
Function: name,
Args: args,
Message: fmt.Sprintf("expected arguments in groups of %d", x),
}
}
return nil
}
8 changes: 4 additions & 4 deletions func_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
var AllFunc = BasicFunction{
name: "all",
runFn: func(c *Context, s *Step, args []string) (Values, error) {
input := c.inputValue(s)

if len(args) > 0 {
return nil, fmt.Errorf("unexpected last args given")
if err := requireNoArgs("all", args); err != nil {
return nil, err
}

input := c.inputValue(s)

res := make(Values, 0)

for _, val := range input {
Expand Down
7 changes: 7 additions & 0 deletions func_equal.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ import (
var EqualFunc = BasicFunction{
name: "equal",
runFn: func(c *Context, s *Step, args []string) (Values, error) {
if err := requireXOrMoreArgs("equal", args, 2); err != nil {
return nil, err
}
if err := requireModulusXArgs("equal", args, 2); err != nil {
return nil, err
}

input := c.inputValue(s)

type comparison struct {
Expand Down
4 changes: 4 additions & 0 deletions func_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (
var FilterFunc = BasicFunction{
name: "filter",
runFn: func(c *Context, s *Step, args []string) (Values, error) {
if err := requireXOrMoreArgs("filter", args, 1); err != nil {
return nil, err
}

input := c.inputValue(s)

runComparison := func(value Value, selector string) (bool, error) {
Expand Down
4 changes: 4 additions & 0 deletions func_filter_or.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (
var FilterOrFunc = BasicFunction{
name: "filterOr",
runFn: func(c *Context, s *Step, args []string) (Values, error) {
if err := requireXOrMoreArgs("filterOr", args, 1); err != nil {
return nil, err
}

input := c.inputValue(s)

runComparison := func(value Value, selector string) (bool, error) {
Expand Down
8 changes: 4 additions & 4 deletions func_first.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
var FirstFunc = BasicFunction{
name: "first",
runFn: func(c *Context, s *Step, args []string) (Values, error) {
input := c.inputValue(s)

if len(args) > 0 {
return nil, fmt.Errorf("unexpected first args given")
if err := requireNoArgs("first", args); err != nil {
return nil, err
}

input := c.inputValue(s)

res := make(Values, 0)

for _, val := range input {
Expand Down
8 changes: 4 additions & 4 deletions func_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ func (e ErrIndexNotFound) Is(other error) bool {
var IndexFunc = BasicFunction{
name: "index",
runFn: func(c *Context, s *Step, args []string) (Values, error) {
input := c.inputValue(s)

if len(args) == 0 {
return nil, fmt.Errorf("no index arg given")
if err := requireXOrMoreArgs("index", args, 1); err != nil {
return nil, err
}

input := c.inputValue(s)

res := make(Values, 0)

for _, val := range input {
Expand Down
4 changes: 4 additions & 0 deletions func_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package dasel
var KeyFunc = BasicFunction{
name: "key",
runFn: func(c *Context, s *Step, args []string) (Values, error) {
if err := requireNoArgs("key", args); err != nil {
return nil, err
}

input := c.inputValue(s)

res := make(Values, 0)
Expand Down
8 changes: 4 additions & 4 deletions func_last.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
var LastFunc = BasicFunction{
name: "last",
runFn: func(c *Context, s *Step, args []string) (Values, error) {
input := c.inputValue(s)

if len(args) > 0 {
return nil, fmt.Errorf("unexpected last args given")
if err := requireNoArgs("last", args); err != nil {
return nil, err
}

input := c.inputValue(s)

res := make(Values, 0)

for _, val := range input {
Expand Down
12 changes: 4 additions & 8 deletions func_len.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package dasel

import (
"fmt"
)

var LenFunc = BasicFunction{
name: "len",
runFn: func(c *Context, s *Step, args []string) (Values, error) {
input := c.inputValue(s)

if len(args) > 0 {
return nil, fmt.Errorf("unexpected last args given")
if err := requireNoArgs("len", args); err != nil {
return nil, err
}

input := c.inputValue(s)

res := make(Values, 0)

for _, val := range input {
Expand Down
4 changes: 4 additions & 0 deletions func_less_than.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
var LessThanFunc = BasicFunction{
name: "lessThan",
runFn: func(c *Context, s *Step, args []string) (Values, error) {
if err := requireExactlyXArgs("lessThan", args, 2); err != nil {
return nil, err
}

input := c.inputValue(s)

type comparison struct {
Expand Down
12 changes: 4 additions & 8 deletions func_metadata.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package dasel

import (
"fmt"
)

var MetadataFunc = BasicFunction{
name: "metadata",
runFn: func(c *Context, s *Step, args []string) (Values, error) {
input := c.inputValue(s)

if len(args) == 0 {
return nil, fmt.Errorf("unexpected metadata args given")
if err := requireXOrMoreArgs("metadata", args, 1); err != nil {
return nil, err
}

input := c.inputValue(s)

res := make(Values, 0)

for _, val := range input {
Expand Down
4 changes: 4 additions & 0 deletions func_more_than.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
var MoreThanFunc = BasicFunction{
name: "moreThan",
runFn: func(c *Context, s *Step, args []string) (Values, error) {
if err := requireExactlyXArgs("moreThan", args, 2); err != nil {
return nil, err
}

input := c.inputValue(s)

type comparison struct {
Expand Down
4 changes: 4 additions & 0 deletions func_parent.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package dasel
var ParentFunc = BasicFunction{
name: "parent",
runFn: func(c *Context, s *Step, args []string) (Values, error) {
if err := requireNoArgs("parent", args); err != nil {
return nil, err
}

input := c.inputValue(s)

res := make(Values, 0)
Expand Down
8 changes: 4 additions & 4 deletions func_property.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ func (e ErrPropertyNotFound) Is(other error) bool {
var PropertyFunc = BasicFunction{
name: "property",
runFn: func(c *Context, s *Step, args []string) (Values, error) {
input := c.inputValue(s)

if len(args) == 0 {
return nil, fmt.Errorf("no property arg given")
if err := requireXOrMoreArgs("property", args, 1); err != nil {
return nil, err
}

input := c.inputValue(s)

res := make(Values, 0)

for _, val := range input {
Expand Down
3 changes: 3 additions & 0 deletions func_this.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package dasel
var ThisFunc = BasicFunction{
name: "this",
runFn: func(c *Context, s *Step, args []string) (Values, error) {
if err := requireNoArgs("this", args); err != nil {
return nil, err
}
return c.inputValue(s), nil
},
}

0 comments on commit d46d974

Please sign in to comment.