Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat. mapping Optimized error stack #4178

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions core/mapping/fieldoptions.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package mapping

import "fmt"

const notSymbol = '!'

type (
Expand Down Expand Up @@ -76,21 +74,21 @@ func (o *fieldOptions) toOptionsWithContext(key string, m Valuer, fullName strin
} else if dep[0] == notSymbol {
dep = dep[1:]
if len(dep) == 0 {
return nil, fmt.Errorf("wrong optional value for %q in %q", key, fullName)
return nil, newError("wrong optional value for %q in %q", key, fullName)
}

_, baseOn := m.Value(dep)
_, selfOn := m.Value(key)
if baseOn == selfOn {
return nil, fmt.Errorf("set value for either %q or %q in %q", dep, key, fullName)
return nil, newError("set value for either %q or %q in %q", dep, key, fullName)
}

optional = baseOn
} else {
_, baseOn := m.Value(dep)
_, selfOn := m.Value(key)
if baseOn != selfOn {
return nil, fmt.Errorf("values for %q and %q should be both provided or both not in %q",
return nil, newError("values for %q and %q should be both provided or both not in %q",
dep, key, fullName)
}

Expand Down
4 changes: 2 additions & 2 deletions core/mapping/jsonunmarshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ func TestUnmarshalBytesError(t *testing.T) {
}

err := UnmarshalJsonBytes([]byte(payload), &v)
assert.Equal(t, errTypeMismatch, err)
assert.Equal(t, ErrTypeMismatch, err)
}

func TestUnmarshalReaderError(t *testing.T) {
Expand All @@ -866,7 +866,7 @@ func TestUnmarshalReaderError(t *testing.T) {
Any string
}

assert.Equal(t, errTypeMismatch, UnmarshalJsonReader(reader, &v))
assert.Equal(t, ErrTypeMismatch, UnmarshalJsonReader(reader, &v))
}

func TestUnmarshalMap(t *testing.T) {
Expand Down
10 changes: 5 additions & 5 deletions core/mapping/marshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ func validateOptional(field reflect.StructField, value reflect.Value) error {
switch field.Type.Kind() {
case reflect.Ptr:
if value.IsNil() {
return fmt.Errorf("field %q is nil", field.Name)
return newError("field %q is nil", field.Name)
}
case reflect.Array, reflect.Slice, reflect.Map:
if value.IsNil() || value.Len() == 0 {
return fmt.Errorf("field %q is empty", field.Name)
return newError("field %q is empty", field.Name)
}
}

Expand All @@ -137,7 +137,7 @@ func validateOptions(value reflect.Value, opt *fieldOptions) error {
}
}
if !found {
return fmt.Errorf("field %q not in options", val)
return newError("field %q not in options", val)
}

return nil
Expand Down Expand Up @@ -171,15 +171,15 @@ func validateRange(value reflect.Value, opt *fieldOptions) error {
case float64:
val = v
default:
return fmt.Errorf("unknown support type for range %q", value.Type().String())
return newError("unknown support type for range %q", value.Type().String())
}

// validates [left, right], [left, right), (left, right], (left, right)
if val < opt.Range.left ||
(!opt.Range.leftInclude && val == opt.Range.left) ||
val > opt.Range.right ||
(!opt.Range.rightInclude && val == opt.Range.right) {
return fmt.Errorf("%v out of range", value.Interface())
return newError("%v out of range", value.Interface())
}

return nil
Expand Down
8 changes: 4 additions & 4 deletions core/mapping/marshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ func TestMarshal_RangeIllegal(t *testing.T) {

for _, test := range tests {
_, err := Marshal(test)
assert.Equal(t, err, errNumberRange)
assert.Equal(t, err, ErrNumberRange)
}
}

Expand All @@ -302,7 +302,7 @@ func TestMarshal_RangeLeftEqualsToRight(t *testing.T) {
}{
Int: 2,
},
err: errNumberRange,
err: ErrNumberRange,
},
{
name: "left exclusive, right inclusive",
Expand All @@ -311,7 +311,7 @@ func TestMarshal_RangeLeftEqualsToRight(t *testing.T) {
}{
Int: 2,
},
err: errNumberRange,
err: ErrNumberRange,
},
{
name: "left exclusive, right exclusive",
Expand All @@ -320,7 +320,7 @@ func TestMarshal_RangeLeftEqualsToRight(t *testing.T) {
}{
Int: 2,
},
err: errNumberRange,
err: ErrNumberRange,
},
}

Expand Down
Loading
Loading