Start fixing TestUnmarshal.
Fixed unmarshalling for int, strings and slices. TODO: Fix maps. Related to issue #4.
This commit is contained in:
parent
698be9359e
commit
cf16bc67de
2 changed files with 14 additions and 14 deletions
|
@ -53,8 +53,7 @@ func fuzzyEqual(a, b any) bool {
|
||||||
|
|
||||||
func checkFuzzyEqualValue(a, b reflect.Value) (err error) {
|
func checkFuzzyEqualValue(a, b reflect.Value) (err error) {
|
||||||
if !fuzzyEqualValue(a, b) {
|
if !fuzzyEqualValue(a, b) {
|
||||||
err = errors.New(fmt.Sprint(a, " != ", b,
|
err = fmt.Errorf("Wanted %v(%v) got %v(%v)", a, a.Interface(), b, b.Interface())
|
||||||
": ", a.Interface(), "!=", b.Interface()))
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -189,9 +188,9 @@ func checkUnmarshal(expected string, data any) (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
dataValue := reflect.ValueOf(data)
|
dataValue := reflect.ValueOf(data)
|
||||||
newOne := reflect.New(dataValue.Type())
|
newOne := reflect.New(reflect.TypeOf(data))
|
||||||
buf := bytes.NewBufferString(expected)
|
buf := bytes.NewBufferString(expected)
|
||||||
if err = Unmarshal(buf, newOne.Interface()); err != nil {
|
if err = UnmarshalValue(buf, newOne); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err = checkFuzzyEqualValue(dataValue, newOne.Elem()); err != nil {
|
if err = checkFuzzyEqualValue(dataValue, newOne.Elem()); err != nil {
|
||||||
|
@ -235,15 +234,15 @@ type structA struct {
|
||||||
|
|
||||||
// TODO: make this test pass.
|
// TODO: make this test pass.
|
||||||
|
|
||||||
func xTestUnmarshal(t *testing.T) {
|
func TestUnmarshal(t *testing.T) {
|
||||||
type structNested struct {
|
type structNested struct {
|
||||||
T string "t"
|
T string "t"
|
||||||
Y string "y"
|
Y string "y"
|
||||||
Q string "q"
|
Q string "q"
|
||||||
A map[string]string "a"
|
A map[string]string "a"
|
||||||
}
|
}
|
||||||
innerDict := map[string]string{"id": "abcdefghij0123456789"}
|
//innerDict := map[string]string{"id": "abcdefghij0123456789"}
|
||||||
nestedDictionary := structNested{"aa", "q", "ping", innerDict}
|
//nestedDictionary := structNested{"aa", "q", "ping", innerDict}
|
||||||
|
|
||||||
tests := []SVPair{
|
tests := []SVPair{
|
||||||
SVPair{"i100e", 100},
|
SVPair{"i100e", 100},
|
||||||
|
@ -256,12 +255,11 @@ func xTestUnmarshal(t *testing.T) {
|
||||||
SVPair{"l3:abc3:defe", []string{"abc", "def"}},
|
SVPair{"l3:abc3:defe", []string{"abc", "def"}},
|
||||||
SVPair{"li42e3:abce", []any{42, "abc"}},
|
SVPair{"li42e3:abce", []any{42, "abc"}},
|
||||||
SVPair{"de", map[string]any{}},
|
SVPair{"de", map[string]any{}},
|
||||||
SVPair{"d3:cati1e3:dogi2ee", map[string]any{"cat": 1, "dog": 2}},
|
//SVPair{"d3:cati1e3:dogi2ee", map[string]any{"cat": 1, "dog": 2}},
|
||||||
SVPair{"d1:ai10e1:b3:fooe", structA{10, "foo"}},
|
//SVPair{"d1:ai10e1:b3:fooe", structA{10, "foo"}},
|
||||||
SVPair{"d1:ad2:id20:abcdefghij0123456789e1:q4:ping1:t2:aa1:y1:qe", nestedDictionary},
|
//SVPair{"d1:ad2:id20:abcdefghij0123456789e1:q4:ping1:t2:aa1:y1:qe", nestedDictionary},
|
||||||
}
|
}
|
||||||
for _, sv := range tests {
|
for _, sv := range tests {
|
||||||
println(sv.s)
|
|
||||||
if err := checkUnmarshal(sv.s, sv.v); err != nil {
|
if err := checkUnmarshal(sv.s, sv.v); err != nil {
|
||||||
t.Error(err.Error())
|
t.Error(err.Error())
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,6 +52,8 @@ func setint(val reflect.Value, i int64) {
|
||||||
v.SetUint(uint64(i))
|
v.SetUint(uint64(i))
|
||||||
case reflect.Interface:
|
case reflect.Interface:
|
||||||
v.Set(reflect.ValueOf(i))
|
v.Set(reflect.ValueOf(i))
|
||||||
|
default:
|
||||||
|
panic("setint called for bogus type: " + val.Kind().String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -266,7 +268,7 @@ func Unmarshal(r io.Reader, val interface{}) (err error) {
|
||||||
err = errors.New("Attempt to unmarshal into a non-pointer")
|
err = errors.New("Attempt to unmarshal into a non-pointer")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = UnmarshalValue(r, reflect.ValueOf(val))
|
err = UnmarshalValue(r, reflect.Indirect(reflect.ValueOf(val)))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -276,9 +278,9 @@ func Unmarshal(r io.Reader, val interface{}) (err error) {
|
||||||
func UnmarshalValue(r io.Reader, v reflect.Value) (err error) {
|
func UnmarshalValue(r io.Reader, v reflect.Value) (err error) {
|
||||||
var b *structBuilder
|
var b *structBuilder
|
||||||
|
|
||||||
// If val is a pointer to a slice, we append to the slice.
|
// XXX: Decide if the extra codnitions are needed. Affect map?
|
||||||
if ptr := v; ptr.Kind() == reflect.Ptr {
|
if ptr := v; ptr.Kind() == reflect.Ptr {
|
||||||
if slice := ptr.Elem(); slice.Kind() == reflect.Slice {
|
if slice := ptr.Elem(); slice.Kind() == reflect.Slice || slice.Kind() == reflect.Int || slice.Kind() == reflect.String {
|
||||||
b = &structBuilder{val: slice}
|
b = &structBuilder{val: slice}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue