Skip to content

Commit

Permalink
changed error logs
Browse files Browse the repository at this point in the history
  • Loading branch information
SoulKa committed Nov 25, 2023
1 parent 2cbbbdd commit f6b717d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
6 changes: 3 additions & 3 deletions objectpath/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func compareStringsIgnoreCase(target string) func(string) bool {
func GetValueAtPath(source any, path ObjectPath, out *reflect.Value) error {
value := reflect.ValueOf(source)
if value.Kind() != reflect.Ptr {
return fmt.Errorf(`cannot get value at path "%s": source is not a pointer`, path.String())
return fmt.Errorf(`cannot get value at path [%s]: source is not a pointer`, path.String())
}
value = value.Elem()

Expand All @@ -27,7 +27,7 @@ func GetValueAtPath(source any, path ObjectPath, out *reflect.Value) error {

// Check if the value is zero or nil
if !value.IsValid() {
return fmt.Errorf(`cannot enter field "%s" of path %s at index %d: value is zero or nil`, element.name, path.String(), i)
return fmt.Errorf(`cannot enter field [%s] of path [%s] at index %d: value is zero or nil`, element.name, path.String(), i)
}

// Dereference pointer
Expand All @@ -48,7 +48,7 @@ func GetValueAtPath(source any, path ObjectPath, out *reflect.Value) error {
}
value = value.FieldByIndex(field.Index)
default:
return fmt.Errorf(`cannot get value at path "%s": value at path index %d is neither a map nor struct`, path.String(), i)
return fmt.Errorf(`cannot get value at path [%s]: value at path index %d is neither a map nor struct`, path.String(), i)
}
}
*out = value
Expand Down
29 changes: 29 additions & 0 deletions objectpath/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ type TestCase struct {
output any
}

type ErrorTestCase struct {
inputObject any
inputPath string
newType reflect.Type
error string
}

type Animal struct {
Name string
Specifics any
Expand Down Expand Up @@ -80,3 +87,25 @@ func TestAssignTypeAtPath(t *testing.T) {
}
}
}

func TestAssignTypeAtPathWithError(t *testing.T) {
var testCases = []ErrorTestCase{
{true, "Specifics", reflect.TypeOf(0), `cannot get value at path ["Specifics"]: value at path index 0 is neither a map nor struct`},
}

for _, tc := range testCases {

// Arrange
err, inputPath := NewObjectPathFromString(tc.inputPath)
if err != nil {
t.Fatalf("error parsing input path [%s]: %s", tc.inputPath, err)
}

// Act
if err := AssignTypeAtPath(&tc.inputObject, *inputPath, tc.newType); err == nil {
t.Fatalf("expected error, but got none")
} else if err.Error() != tc.error {
t.Fatalf(`expected error to be [%s], but got [%s]`, tc.error, err)
}
}
}
12 changes: 6 additions & 6 deletions objectpath/parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ func (ctx *Context) hasNextChar() bool {
// assertChar returns an error if the current character is not the expected character.
func (ctx *Context) assertChar(expected rune) error {
if c := ctx.chars[ctx.i]; c != expected {
return fmt.Errorf(`unexpected character "%c" at index %d. Expected "%c"`, c, ctx.i, expected)
return fmt.Errorf(`unexpected character [%c] at index %d. Expected [%c]`, c, ctx.i, expected)
}
return nil
}

// assertNextChar returns an error if the next character is not the expected character.
func (ctx *Context) assertNextChar(expected rune) error {
if !ctx.hasNextChar() {
return fmt.Errorf(`unexpected end of string after %d runes. Expected "%c"`, len(ctx.chars), expected)
return fmt.Errorf(`unexpected end of string after %d runes. Expected [%c]`, len(ctx.chars), expected)
}
ctx.i++
return ctx.assertChar(expected)
Expand Down Expand Up @@ -103,9 +103,9 @@ var charParsingFunctions = []func(ctx *Context) error{
}
}
if ctx.path.isCurrentPartEmpty() && !unicode.IsLetter(c) {
return fmt.Errorf(`unexpected character "%c" at index %d. A non-enclosed path must start with a letter`, c, ctx.i)
return fmt.Errorf(`unexpected character [%c] at index %d. A non-enclosed path must start with a letter`, c, ctx.i)
} else if !unicode.IsLetter(c) && !unicode.IsDigit(c) {
return fmt.Errorf(`unexpected character "%c" at index %d. A non-enclosed path may only contain letters and digits`, c, ctx.i)
return fmt.Errorf(`unexpected character [%c] at index %d. A non-enclosed path may only contain letters and digits`, c, ctx.i)
}
ctx.path.appendCharToCurrentElement(c)
return nil
Expand Down Expand Up @@ -143,7 +143,7 @@ var charParsingFunctions = []func(ctx *Context) error{
case '.':
ctx.path.appendCharToCurrentElement(c)
if e.name == "..." {
return fmt.Errorf(`invalid path element "..." at index %d. Only "." or ".." allowed`, ctx.i-2)
return fmt.Errorf(`invalid path element [...] at index %d. Only [.] or [..] allowed`, ctx.i-2)
} else if !ctx.hasNextChar() {
ctx.chars = append(ctx.chars, '/') // append slash to end of string to parse last element
}
Expand All @@ -157,7 +157,7 @@ var charParsingFunctions = []func(ctx *Context) error{
ctx.state = ParsingStateSlash
ctx.reprocess = true
default:
return fmt.Errorf(`unexpected character "%c" at index %d. Expected either "." or "/"`, c, ctx.i)
return fmt.Errorf(`unexpected character [%c] at index %d. Expected either [.] or [/]`, c, ctx.i)
}
return nil
},
Expand Down
6 changes: 3 additions & 3 deletions objectpath/parsing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ func TestParsePathString(t *testing.T) {
{"foo/", []Element{{"foo", ElementTypeIdentifier}}, nil},
{".", []Element{ElementSelfReference}, nil},
{"", []Element{}, nil},
{`foo/"bar`, nil, errors.New(`unexpected end of string after 8 runes. Expected """`)},
{`fo#`, nil, errors.New(`unexpected character "#" at index 2. A non-enclosed path may only contain letters and digits`)},
{`foo/"bar`, nil, errors.New(`unexpected end of string after 8 runes. Expected ["]`)},
{`fo#`, nil, errors.New(`unexpected character [#] at index 2. A non-enclosed path may only contain letters and digits`)},
{`fo//bar`, nil, errors.New(`empty path element provided at index 3. Empty elements must be enclosed in quotes, e.g. /""/data`)},
{`.../foo`, nil, errors.New(`invalid path element "..." at index 0. Only "." or ".." allowed`)},
{`.../foo`, nil, errors.New(`invalid path element [...] at index 0. Only [.] or [..] allowed`)},
}

for _, tc := range testCases {
Expand Down

0 comments on commit f6b717d

Please sign in to comment.