mirror of
https://github.com/dolthub/dolt.git
synced 2026-05-25 03:05:52 -05:00
dolt/go/store: marshal: Remove Format_7_18 references, thread *Format.
This commit is contained in:
committed by
Brian Hendriks
parent
a7f5e325ce
commit
644b2be0f2
@@ -131,7 +131,7 @@ func MarshalAsNomsValue(ctx context.Context, vrw types.ValueReadWriter, sch sche
|
||||
// UnmarshalNomsValue takes a types.Value instance and Unmarshalls it into a Schema.
|
||||
func UnmarshalNomsValue(ctx context.Context, schemaVal types.Value) (schema.Schema, error) {
|
||||
var sd schemaData
|
||||
err := marshal.Unmarshal(ctx, schemaVal, &sd)
|
||||
err := marshal.Unmarshal(ctx, types.Format_7_18, schemaVal, &sd)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -278,7 +278,7 @@ func writeMetaLines(ctx context.Context, node LogNode, maxLines, lineno, maxLabe
|
||||
// field of type datetime.DateTimeType
|
||||
if types.TypeOf(v).Equals(types.Format_7_18, datetime.DateTimeType) {
|
||||
var dt datetime.DateTime
|
||||
err = dt.UnmarshalNoms(ctx, v)
|
||||
err = dt.UnmarshalNoms(ctx, types.Format_7_18, v)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
|
||||
+55
-54
@@ -61,12 +61,12 @@ import (
|
||||
// - a Noms value is not appropriate for a given target type
|
||||
// - a Noms number overflows the target type
|
||||
// - a Noms list is decoded into a Go array of a different length
|
||||
func Unmarshal(ctx context.Context, v types.Value, out interface{}) (err error) {
|
||||
return UnmarshalOpt(ctx, v, Opt{}, out)
|
||||
func Unmarshal(ctx context.Context, f *types.Format, v types.Value, out interface{}) (err error) {
|
||||
return UnmarshalOpt(ctx, f, v, Opt{}, out)
|
||||
}
|
||||
|
||||
// UnmarshalOpt is like Unmarshal but provides additional options.
|
||||
func UnmarshalOpt(ctx context.Context, v types.Value, opt Opt, out interface{}) (err error) {
|
||||
func UnmarshalOpt(ctx context.Context, f *types.Format, v types.Value, opt Opt, out interface{}) (err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
switch r := r.(type) {
|
||||
@@ -80,18 +80,18 @@ func UnmarshalOpt(ctx context.Context, v types.Value, opt Opt, out interface{})
|
||||
}
|
||||
}()
|
||||
|
||||
MustUnmarshalOpt(ctx, v, opt, out)
|
||||
MustUnmarshalOpt(ctx, f, v, opt, out)
|
||||
return
|
||||
}
|
||||
|
||||
// Unmarshals a Noms value into a Go value using the same rules as Unmarshal().
|
||||
// Panics on failure.
|
||||
func MustUnmarshal(ctx context.Context, v types.Value, out interface{}) {
|
||||
MustUnmarshalOpt(ctx, v, Opt{}, out)
|
||||
func MustUnmarshal(ctx context.Context, f *types.Format, v types.Value, out interface{}) {
|
||||
MustUnmarshalOpt(ctx, f, v, Opt{}, out)
|
||||
}
|
||||
|
||||
// MustUnmarshalOpt is like MustUnmarshal but with additional options.
|
||||
func MustUnmarshalOpt(ctx context.Context, v types.Value, opt Opt, out interface{}) {
|
||||
func MustUnmarshalOpt(ctx context.Context, f *types.Format, v types.Value, opt Opt, out interface{}) {
|
||||
rv := reflect.ValueOf(out)
|
||||
if rv.Kind() != reflect.Ptr || rv.IsNil() {
|
||||
panic(&InvalidUnmarshalError{reflect.TypeOf(out)})
|
||||
@@ -101,7 +101,7 @@ func MustUnmarshalOpt(ctx context.Context, v types.Value, opt Opt, out interface
|
||||
set: opt.Set,
|
||||
}
|
||||
d := typeDecoder(rv.Type(), nt)
|
||||
d(ctx, v, rv)
|
||||
d(ctx, f, v, rv)
|
||||
}
|
||||
|
||||
// Unmarshaler is an interface types can implement to provide their own
|
||||
@@ -114,7 +114,7 @@ func MustUnmarshalOpt(ctx context.Context, v types.Value, opt Opt, out interface
|
||||
// func (t *MyType) UnmarshalNoms(v types.Value) error {}
|
||||
type Unmarshaler interface {
|
||||
// UnmarshalNoms decodes v, or returns an error.
|
||||
UnmarshalNoms(ctx context.Context, v types.Value) error
|
||||
UnmarshalNoms(ctx context.Context, f *types.Format, v types.Value) error
|
||||
}
|
||||
|
||||
var unmarshalerInterface = reflect.TypeOf((*Unmarshaler)(nil)).Elem()
|
||||
@@ -142,6 +142,7 @@ type UnmarshalTypeMismatchError struct {
|
||||
Value types.Value
|
||||
Type reflect.Type // type of Go value it could not be assigned to
|
||||
details string
|
||||
format *types.Format
|
||||
}
|
||||
|
||||
func (e *UnmarshalTypeMismatchError) Error() string {
|
||||
@@ -151,11 +152,11 @@ func (e *UnmarshalTypeMismatchError) Error() string {
|
||||
} else {
|
||||
ts = e.Type.String()
|
||||
}
|
||||
return fmt.Sprintf("Cannot unmarshal %s into Go value of type %s%s", types.TypeOf(e.Value).Describe(context.Background(), types.Format_7_18), ts, e.details)
|
||||
return fmt.Sprintf("Cannot unmarshal %s into Go value of type %s%s", types.TypeOf(e.Value).Describe(context.Background(), e.format), ts, e.details)
|
||||
}
|
||||
|
||||
func overflowError(v types.Float, t reflect.Type) *UnmarshalTypeMismatchError {
|
||||
return &UnmarshalTypeMismatchError{v, t, fmt.Sprintf(" (%g does not fit in %s)", v, t)}
|
||||
func overflowError(f *types.Format, v types.Float, t reflect.Type) *UnmarshalTypeMismatchError {
|
||||
return &UnmarshalTypeMismatchError{v, t, fmt.Sprintf(" (%g does not fit in %s)", v, t), f}
|
||||
}
|
||||
|
||||
// unmarshalNomsError wraps errors from Marshaler.UnmarshalNoms. These should
|
||||
@@ -168,7 +169,7 @@ func (e *unmarshalNomsError) Error() string {
|
||||
return e.err.Error()
|
||||
}
|
||||
|
||||
type decoderFunc func(ctx context.Context, v types.Value, rv reflect.Value)
|
||||
type decoderFunc func(ctx context.Context, f *types.Format, v types.Value, rv reflect.Value)
|
||||
|
||||
func typeDecoder(t reflect.Type, tags nomsTags) decoderFunc {
|
||||
if reflect.PtrTo(t).Implements(unmarshalerInterface) {
|
||||
@@ -210,51 +211,51 @@ func typeDecoder(t reflect.Type, tags nomsTags) decoderFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func boolDecoder(ctx context.Context, v types.Value, rv reflect.Value) {
|
||||
func boolDecoder(ctx context.Context, f *types.Format, v types.Value, rv reflect.Value) {
|
||||
if b, ok := v.(types.Bool); ok {
|
||||
rv.SetBool(bool(b))
|
||||
} else {
|
||||
panic(&UnmarshalTypeMismatchError{v, rv.Type(), ""})
|
||||
panic(&UnmarshalTypeMismatchError{v, rv.Type(), "", f})
|
||||
}
|
||||
}
|
||||
|
||||
func stringDecoder(ctx context.Context, v types.Value, rv reflect.Value) {
|
||||
func stringDecoder(ctx context.Context, f *types.Format, v types.Value, rv reflect.Value) {
|
||||
if s, ok := v.(types.String); ok {
|
||||
rv.SetString(string(s))
|
||||
} else {
|
||||
panic(&UnmarshalTypeMismatchError{v, rv.Type(), ""})
|
||||
panic(&UnmarshalTypeMismatchError{v, rv.Type(), "", f})
|
||||
}
|
||||
}
|
||||
|
||||
func floatDecoder(ctx context.Context, v types.Value, rv reflect.Value) {
|
||||
func floatDecoder(ctx context.Context, f *types.Format, v types.Value, rv reflect.Value) {
|
||||
if n, ok := v.(types.Float); ok {
|
||||
rv.SetFloat(float64(n))
|
||||
} else {
|
||||
panic(&UnmarshalTypeMismatchError{v, rv.Type(), ""})
|
||||
panic(&UnmarshalTypeMismatchError{v, rv.Type(), "", f})
|
||||
}
|
||||
}
|
||||
|
||||
func intDecoder(ctx context.Context, v types.Value, rv reflect.Value) {
|
||||
func intDecoder(ctx context.Context, f *types.Format, v types.Value, rv reflect.Value) {
|
||||
if n, ok := v.(types.Float); ok {
|
||||
i := int64(n)
|
||||
if rv.OverflowInt(i) {
|
||||
panic(overflowError(n, rv.Type()))
|
||||
panic(overflowError(f, n, rv.Type()))
|
||||
}
|
||||
rv.SetInt(i)
|
||||
} else {
|
||||
panic(&UnmarshalTypeMismatchError{v, rv.Type(), ""})
|
||||
panic(&UnmarshalTypeMismatchError{v, rv.Type(), "", f})
|
||||
}
|
||||
}
|
||||
|
||||
func uintDecoder(ctx context.Context, v types.Value, rv reflect.Value) {
|
||||
func uintDecoder(ctx context.Context, f *types.Format, v types.Value, rv reflect.Value) {
|
||||
if n, ok := v.(types.Float); ok {
|
||||
u := uint64(n)
|
||||
if rv.OverflowUint(u) {
|
||||
panic(overflowError(n, rv.Type()))
|
||||
panic(overflowError(f, n, rv.Type()))
|
||||
}
|
||||
rv.SetUint(u)
|
||||
} else {
|
||||
panic(&UnmarshalTypeMismatchError{v, rv.Type(), ""})
|
||||
panic(&UnmarshalTypeMismatchError{v, rv.Type(), "", f})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -337,26 +338,26 @@ func structDecoder(t reflect.Type) decoderFunc {
|
||||
|
||||
fields := structDecoderFields(t)
|
||||
|
||||
d = func(ctx context.Context, v types.Value, rv reflect.Value) {
|
||||
d = func(ctx context.Context, format *types.Format, v types.Value, rv reflect.Value) {
|
||||
s, ok := v.(types.Struct)
|
||||
if !ok {
|
||||
panic(&UnmarshalTypeMismatchError{v, rv.Type(), ", expected struct"})
|
||||
panic(&UnmarshalTypeMismatchError{v, rv.Type(), ", expected struct", format})
|
||||
}
|
||||
|
||||
for _, f := range fields {
|
||||
sf := rv.FieldByIndex(f.index)
|
||||
if f.original {
|
||||
if sf.Type() != reflect.TypeOf(s) {
|
||||
panic(&UnmarshalTypeMismatchError{v, rv.Type(), ", field with tag \"original\" must have type Struct"})
|
||||
panic(&UnmarshalTypeMismatchError{v, rv.Type(), ", field with tag \"original\" must have type Struct", format})
|
||||
}
|
||||
sf.Set(reflect.ValueOf(s))
|
||||
continue
|
||||
}
|
||||
fv, ok := s.MaybeGet(f.name)
|
||||
if ok {
|
||||
f.decoder(ctx, fv, sf)
|
||||
f.decoder(ctx, format, fv, sf)
|
||||
} else if !f.omitEmpty {
|
||||
panic(&UnmarshalTypeMismatchError{v, rv.Type(), ", missing field \"" + f.name + "\""})
|
||||
panic(&UnmarshalTypeMismatchError{v, rv.Type(), ", missing field \"" + f.name + "\"", format})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -365,17 +366,17 @@ func structDecoder(t reflect.Type) decoderFunc {
|
||||
return d
|
||||
}
|
||||
|
||||
func nomsValueDecoder(ctx context.Context, v types.Value, rv reflect.Value) {
|
||||
func nomsValueDecoder(ctx context.Context, f *types.Format, v types.Value, rv reflect.Value) {
|
||||
if !reflect.TypeOf(v).AssignableTo(rv.Type()) {
|
||||
panic(&UnmarshalTypeMismatchError{v, rv.Type(), ""})
|
||||
panic(&UnmarshalTypeMismatchError{v, rv.Type(), "", f})
|
||||
}
|
||||
rv.Set(reflect.ValueOf(v))
|
||||
}
|
||||
|
||||
func marshalerDecoder(t reflect.Type) decoderFunc {
|
||||
return func(ctx context.Context, v types.Value, rv reflect.Value) {
|
||||
return func(ctx context.Context, f *types.Format, v types.Value, rv reflect.Value) {
|
||||
ptr := reflect.New(t)
|
||||
err := ptr.Interface().(Unmarshaler).UnmarshalNoms(ctx, v)
|
||||
err := ptr.Interface().(Unmarshaler).UnmarshalNoms(ctx, f, v)
|
||||
if err != nil {
|
||||
panic(&unmarshalNomsError{err})
|
||||
}
|
||||
@@ -383,7 +384,7 @@ func marshalerDecoder(t reflect.Type) decoderFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func iterListOrSlice(ctx context.Context, v types.Value, t reflect.Type, f func(c types.Value, i uint64)) {
|
||||
func iterListOrSlice(ctx context.Context, format *types.Format, v types.Value, t reflect.Type, f func(c types.Value, i uint64)) {
|
||||
switch v := v.(type) {
|
||||
case types.List:
|
||||
v.IterAll(ctx, f)
|
||||
@@ -394,7 +395,7 @@ func iterListOrSlice(ctx context.Context, v types.Value, t reflect.Type, f func(
|
||||
i++
|
||||
})
|
||||
default:
|
||||
panic(&UnmarshalTypeMismatchError{v, t, ""})
|
||||
panic(&UnmarshalTypeMismatchError{v, t, "", format})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -408,7 +409,7 @@ func sliceDecoder(t reflect.Type) decoderFunc {
|
||||
var init sync.RWMutex
|
||||
init.Lock()
|
||||
defer init.Unlock()
|
||||
d = func(ctx context.Context, v types.Value, rv reflect.Value) {
|
||||
d = func(ctx context.Context, format *types.Format, v types.Value, rv reflect.Value) {
|
||||
var slice reflect.Value
|
||||
if rv.IsNil() {
|
||||
slice = rv
|
||||
@@ -417,9 +418,9 @@ func sliceDecoder(t reflect.Type) decoderFunc {
|
||||
}
|
||||
init.RLock()
|
||||
defer init.RUnlock()
|
||||
iterListOrSlice(ctx, v, t, func(v types.Value, _ uint64) {
|
||||
iterListOrSlice(ctx, format, v, t, func(v types.Value, _ uint64) {
|
||||
elemRv := reflect.New(t.Elem()).Elem()
|
||||
decoder(ctx, v, elemRv)
|
||||
decoder(ctx, format, v, elemRv)
|
||||
slice = reflect.Append(slice, elemRv)
|
||||
})
|
||||
rv.Set(slice)
|
||||
@@ -440,21 +441,21 @@ func arrayDecoder(t reflect.Type) decoderFunc {
|
||||
var init sync.RWMutex
|
||||
init.Lock()
|
||||
defer init.Unlock()
|
||||
d = func(ctx context.Context, v types.Value, rv reflect.Value) {
|
||||
d = func(ctx context.Context, format *types.Format, v types.Value, rv reflect.Value) {
|
||||
size := t.Len()
|
||||
list, ok := v.(types.Collection)
|
||||
if !ok {
|
||||
panic(&UnmarshalTypeMismatchError{v, t, ""})
|
||||
panic(&UnmarshalTypeMismatchError{v, t, "", format})
|
||||
}
|
||||
|
||||
l := int(list.Len())
|
||||
if l != size {
|
||||
panic(&UnmarshalTypeMismatchError{v, t, ", length does not match"})
|
||||
panic(&UnmarshalTypeMismatchError{v, t, ", length does not match", format})
|
||||
}
|
||||
init.RLock()
|
||||
defer init.RUnlock()
|
||||
iterListOrSlice(ctx, list, t, func(v types.Value, i uint64) {
|
||||
decoder(ctx, v, rv.Index(int(i)))
|
||||
iterListOrSlice(ctx, format, list, t, func(v types.Value, i uint64) {
|
||||
decoder(ctx, format, v, rv.Index(int(i)))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -473,19 +474,19 @@ func mapFromSetDecoder(t reflect.Type) decoderFunc {
|
||||
var init sync.RWMutex
|
||||
init.Lock()
|
||||
defer init.Unlock()
|
||||
d = func(ctx context.Context, v types.Value, rv reflect.Value) {
|
||||
d = func(ctx context.Context, format *types.Format, v types.Value, rv reflect.Value) {
|
||||
m := rv
|
||||
|
||||
nomsSet, ok := v.(types.Set)
|
||||
if !ok {
|
||||
panic(&UnmarshalTypeMismatchError{v, t, `, field has "set" tag`})
|
||||
panic(&UnmarshalTypeMismatchError{v, t, `, field has "set" tag`, format})
|
||||
}
|
||||
|
||||
init.RLock()
|
||||
defer init.RUnlock()
|
||||
nomsSet.IterAll(ctx, func(v types.Value) {
|
||||
keyRv := reflect.New(t.Key()).Elem()
|
||||
decoder(ctx, v, keyRv)
|
||||
decoder(ctx, format, v, keyRv)
|
||||
if m.IsNil() {
|
||||
m = reflect.MakeMap(t)
|
||||
}
|
||||
@@ -510,27 +511,27 @@ func mapDecoder(t reflect.Type, tags nomsTags) decoderFunc {
|
||||
var init sync.RWMutex
|
||||
init.Lock()
|
||||
defer init.Unlock()
|
||||
d = func(ctx context.Context, v types.Value, rv reflect.Value) {
|
||||
d = func(ctx context.Context, format *types.Format, v types.Value, rv reflect.Value) {
|
||||
m := rv
|
||||
|
||||
// Special case decoding failure if it looks like the "set" tag is missing,
|
||||
// because it's helpful.
|
||||
if _, ok := v.(types.Set); ok && !tags.set {
|
||||
panic(&UnmarshalTypeMismatchError{v, t, `, field missing "set" tag`})
|
||||
panic(&UnmarshalTypeMismatchError{v, t, `, field missing "set" tag`, format})
|
||||
}
|
||||
|
||||
nomsMap, ok := v.(types.Map)
|
||||
if !ok {
|
||||
panic(&UnmarshalTypeMismatchError{v, t, ""})
|
||||
panic(&UnmarshalTypeMismatchError{v, t, "", format})
|
||||
}
|
||||
|
||||
init.RLock()
|
||||
defer init.RUnlock()
|
||||
nomsMap.IterAll(ctx, func(k, v types.Value) {
|
||||
keyRv := reflect.New(t.Key()).Elem()
|
||||
keyDecoder(ctx, k, keyRv)
|
||||
keyDecoder(ctx, format, k, keyRv)
|
||||
valueRv := reflect.New(t.Elem()).Elem()
|
||||
valueDecoder(ctx, v, valueRv)
|
||||
valueDecoder(ctx, format, v, valueRv)
|
||||
if m.IsNil() {
|
||||
m = reflect.MakeMap(t)
|
||||
}
|
||||
@@ -554,11 +555,11 @@ func interfaceDecoder(t reflect.Type) decoderFunc {
|
||||
panic(&UnsupportedTypeError{Type: t})
|
||||
}
|
||||
|
||||
return func(ctx context.Context, v types.Value, rv reflect.Value) {
|
||||
return func(ctx context.Context, format *types.Format, v types.Value, rv reflect.Value) {
|
||||
// TODO: Go directly from value to go type
|
||||
t := getGoTypeForNomsType(types.TypeOf(v), rv.Type(), v)
|
||||
i := reflect.New(t).Elem()
|
||||
typeDecoder(t, nomsTags{})(ctx, v, i)
|
||||
typeDecoder(t, nomsTags{})(ctx, format, v, i)
|
||||
rv.Set(i)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ func TestDecode(tt *testing.T) {
|
||||
t := func(v types.Value, ptr interface{}, expected interface{}) {
|
||||
p := reflect.ValueOf(ptr)
|
||||
assert.Equal(reflect.Ptr, p.Type().Kind())
|
||||
err := Unmarshal(context.Background(), v, p.Interface())
|
||||
err := Unmarshal(context.Background(), types.Format_7_18, v, p.Interface())
|
||||
assert.NoError(err)
|
||||
if expectedValue, ok := expected.(types.Value); ok {
|
||||
assert.True(expectedValue.Equals(types.Format_7_18, p.Elem().Interface().(types.Value)))
|
||||
@@ -39,7 +39,7 @@ func TestDecode(tt *testing.T) {
|
||||
|
||||
// Also test that types.Value is passed through
|
||||
var v2 types.Value
|
||||
err = Unmarshal(context.Background(), v, &v2)
|
||||
err = Unmarshal(context.Background(), types.Format_7_18, v, &v2)
|
||||
assert.NoError(err)
|
||||
assert.True(v.Equals(types.Format_7_18, v2))
|
||||
}
|
||||
@@ -238,7 +238,7 @@ func TestDecodeStructWithNomsValue(t *testing.T) {
|
||||
"def": types.NewList(context.Background(), types.Format_7_18, vs, types.Float(42)),
|
||||
})
|
||||
var t2 T2
|
||||
MustUnmarshal(context.Background(), v, &t2)
|
||||
MustUnmarshal(context.Background(), types.Format_7_18, v, &t2)
|
||||
assert.IsType(t, T2{}, t2)
|
||||
assert.Equal(t, TestStruct{false, 1, "bye"}, t2.Abc)
|
||||
// TODO(binformat)
|
||||
@@ -256,7 +256,7 @@ func TestDecodeNonPointer(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDecodeNil(t *testing.T) {
|
||||
err := Unmarshal(context.Background(), types.Bool(true), nil)
|
||||
err := Unmarshal(context.Background(), types.Format_7_18, types.Bool(true), nil)
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, "Cannot unmarshal into Go nil value", err.Error())
|
||||
}
|
||||
@@ -289,7 +289,7 @@ func TestDecodeTypeMismatch(t *testing.T) {
|
||||
|
||||
func assertDecodeErrorMessage(t *testing.T, v types.Value, ptr interface{}, msg string) {
|
||||
p := reflect.ValueOf(ptr)
|
||||
err := Unmarshal(context.Background(), v, p.Interface())
|
||||
err := Unmarshal(context.Background(), types.Format_7_18, v, p.Interface())
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, msg, err.Error())
|
||||
}
|
||||
@@ -363,7 +363,7 @@ func TestDecodeEmbeddedStruct(tt *testing.T) {
|
||||
EmbeddedStruct
|
||||
}
|
||||
var ts TestStruct
|
||||
err := Unmarshal(context.Background(), types.NewStruct(types.Format_7_18, "S", types.StructData{
|
||||
err := Unmarshal(context.Background(), types.Format_7_18, types.NewStruct(types.Format_7_18, "S", types.StructData{
|
||||
"x": types.Float(1),
|
||||
}), &ts)
|
||||
assert.NoError(err)
|
||||
@@ -374,7 +374,7 @@ func TestDecodeEmbeddedStruct(tt *testing.T) {
|
||||
TestStruct
|
||||
}
|
||||
var ts2 OuterTest
|
||||
err = Unmarshal(context.Background(), types.NewStruct(types.Format_7_18, "S", types.StructData{
|
||||
err = Unmarshal(context.Background(), types.Format_7_18, types.NewStruct(types.Format_7_18, "S", types.StructData{
|
||||
"x": types.Float(2),
|
||||
"y": types.Bool(true),
|
||||
}), &ts2)
|
||||
@@ -393,7 +393,7 @@ func TestDecodeEmbeddedStructSkip(tt *testing.T) {
|
||||
Y int
|
||||
}
|
||||
ts := TestStruct{EmbeddedStruct: EmbeddedStruct{42}}
|
||||
err := Unmarshal(context.Background(), types.NewStruct(types.Format_7_18, "S", types.StructData{
|
||||
err := Unmarshal(context.Background(), types.Format_7_18, types.NewStruct(types.Format_7_18, "S", types.StructData{
|
||||
"y": types.Float(2),
|
||||
}), &ts)
|
||||
assert.NoError(err)
|
||||
@@ -411,7 +411,7 @@ func TestDecodeEmbeddedStructNamed(tt *testing.T) {
|
||||
Y int
|
||||
}
|
||||
ts := TestStruct{EmbeddedStruct: EmbeddedStruct{42}}
|
||||
err := Unmarshal(context.Background(), types.NewStruct(types.Format_7_18, "S", types.StructData{
|
||||
err := Unmarshal(context.Background(), types.Format_7_18, types.NewStruct(types.Format_7_18, "S", types.StructData{
|
||||
"em": types.NewStruct(types.Format_7_18, "S", types.StructData{
|
||||
"x": types.Float(1),
|
||||
}),
|
||||
@@ -435,7 +435,7 @@ func TestDecodeEmbeddedStructOriginal(tt *testing.T) {
|
||||
nomsStruct := types.NewStruct(types.Format_7_18, "S", types.StructData{
|
||||
"x": types.Float(1),
|
||||
})
|
||||
err := Unmarshal(context.Background(), nomsStruct, &ts)
|
||||
err := Unmarshal(context.Background(), types.Format_7_18, nomsStruct, &ts)
|
||||
assert.NoError(err)
|
||||
expected := TestStruct{
|
||||
EmbeddedStruct: EmbeddedStruct{
|
||||
@@ -463,21 +463,21 @@ func TestDecodeTaggingSkip(t *testing.T) {
|
||||
B bool
|
||||
}
|
||||
var s S
|
||||
err := Unmarshal(context.Background(), types.NewStruct(types.Format_7_18, "S", types.StructData{
|
||||
err := Unmarshal(context.Background(), types.Format_7_18, types.NewStruct(types.Format_7_18, "S", types.StructData{
|
||||
"b": types.Bool(true),
|
||||
}), &s)
|
||||
assert.NoError(err)
|
||||
assert.Equal(S{0, true}, s)
|
||||
|
||||
var s2 S
|
||||
Unmarshal(context.Background(), types.NewStruct(types.Format_7_18, "S", types.StructData{
|
||||
Unmarshal(context.Background(), types.Format_7_18, types.NewStruct(types.Format_7_18, "S", types.StructData{
|
||||
"a": types.Float(42),
|
||||
"b": types.Bool(true),
|
||||
}), &s2)
|
||||
assert.Equal(S{0, true}, s2)
|
||||
|
||||
s3 := S{555, true}
|
||||
err = Unmarshal(context.Background(), types.NewStruct(types.Format_7_18, "S", types.StructData{
|
||||
err = Unmarshal(context.Background(), types.Format_7_18, types.NewStruct(types.Format_7_18, "S", types.StructData{
|
||||
"a": types.Float(42),
|
||||
"b": types.Bool(false),
|
||||
}), &s3)
|
||||
@@ -494,7 +494,7 @@ func TestDecodeNamedFields(t *testing.T) {
|
||||
Ccc string
|
||||
}
|
||||
var s S
|
||||
err := Unmarshal(context.Background(), types.NewStruct(types.Format_7_18, "S", types.StructData{
|
||||
err := Unmarshal(context.Background(), types.Format_7_18, types.NewStruct(types.Format_7_18, "S", types.StructData{
|
||||
"a": types.Float(42),
|
||||
"B": types.Bool(true),
|
||||
"ccc": types.String("Hi"),
|
||||
@@ -530,7 +530,7 @@ func TestDecodeNomsTypePtr(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
testUnmarshal := func(v types.Value, dest interface{}, expected interface{}) {
|
||||
err := Unmarshal(context.Background(), v, dest)
|
||||
err := Unmarshal(context.Background(), types.Format_7_18, v, dest)
|
||||
assert.NoError(err)
|
||||
assert.Equal(expected, dest)
|
||||
}
|
||||
@@ -556,7 +556,7 @@ func ExampleUnmarshal() {
|
||||
Male bool
|
||||
}
|
||||
var rickon Person
|
||||
err := Unmarshal(context.Background(), types.NewStruct(types.Format_7_18, "Person", types.StructData{
|
||||
err := Unmarshal(context.Background(), types.Format_7_18, types.NewStruct(types.Format_7_18, "Person", types.StructData{
|
||||
"given": types.String("Rickon"),
|
||||
"male": types.Bool(true),
|
||||
}), &rickon)
|
||||
@@ -578,11 +578,11 @@ func TestDecodeSlice(t *testing.T) {
|
||||
var s []string
|
||||
|
||||
// TODO(binformat)
|
||||
err := Unmarshal(context.Background(), types.NewList(context.Background(), types.Format_7_18, vs, types.String("a"), types.String("b"), types.String("c")), &s)
|
||||
err := Unmarshal(context.Background(), types.Format_7_18, types.NewList(context.Background(), types.Format_7_18, vs, types.String("a"), types.String("b"), types.String("c")), &s)
|
||||
assert.NoError(err)
|
||||
assert.Equal([]string{"a", "b", "c"}, s)
|
||||
|
||||
err = Unmarshal(context.Background(), types.NewSet(context.Background(), types.Format_7_18, vs, types.String("a"), types.String("b"), types.String("c")), &s)
|
||||
err = Unmarshal(context.Background(), types.Format_7_18, types.NewSet(context.Background(), types.Format_7_18, vs, types.String("a"), types.String("b"), types.String("c")), &s)
|
||||
assert.NoError(err)
|
||||
assert.Equal([]string{"a", "b", "c"}, s)
|
||||
}
|
||||
@@ -596,21 +596,21 @@ func TestDecodeSliceEmpty(t *testing.T) {
|
||||
var s []string
|
||||
|
||||
// TODO(binformat)
|
||||
err := Unmarshal(context.Background(), types.NewList(context.Background(), types.Format_7_18, vs), &s)
|
||||
err := Unmarshal(context.Background(), types.Format_7_18, types.NewList(context.Background(), types.Format_7_18, vs), &s)
|
||||
assert.NoError(err)
|
||||
assert.Equal([]string(nil), s)
|
||||
|
||||
err = Unmarshal(context.Background(), types.NewSet(context.Background(), types.Format_7_18, vs), &s)
|
||||
err = Unmarshal(context.Background(), types.Format_7_18, types.NewSet(context.Background(), types.Format_7_18, vs), &s)
|
||||
assert.NoError(err)
|
||||
assert.Equal([]string(nil), s)
|
||||
|
||||
s2 := []string{}
|
||||
// TODO(binformat)
|
||||
err = Unmarshal(context.Background(), types.NewList(context.Background(), types.Format_7_18, vs), &s2)
|
||||
err = Unmarshal(context.Background(), types.Format_7_18, types.NewList(context.Background(), types.Format_7_18, vs), &s2)
|
||||
assert.NoError(err)
|
||||
assert.Equal([]string{}, s2)
|
||||
|
||||
err = Unmarshal(context.Background(), types.NewSet(context.Background(), types.Format_7_18, vs), &s2)
|
||||
err = Unmarshal(context.Background(), types.Format_7_18, types.NewSet(context.Background(), types.Format_7_18, vs), &s2)
|
||||
assert.NoError(err)
|
||||
assert.Equal([]string{}, s2)
|
||||
}
|
||||
@@ -624,13 +624,13 @@ func TestDecodeSliceReuse(t *testing.T) {
|
||||
s := []string{"A", "B", "C", "D"}
|
||||
s2 := s[1:3]
|
||||
// TODO(binformat)
|
||||
err := Unmarshal(context.Background(), types.NewList(context.Background(), types.Format_7_18, vs, types.String("a"), types.String("b")), &s)
|
||||
err := Unmarshal(context.Background(), types.Format_7_18, types.NewList(context.Background(), types.Format_7_18, vs, types.String("a"), types.String("b")), &s)
|
||||
assert.NoError(err)
|
||||
assert.Equal([]string{"a", "b"}, s)
|
||||
assert.Equal([]string{"b", "C"}, s2)
|
||||
|
||||
// TODO(binformat)
|
||||
err = Unmarshal(context.Background(), types.NewSet(context.Background(), types.Format_7_18, vs, types.String("a"), types.String("b")), &s)
|
||||
err = Unmarshal(context.Background(), types.Format_7_18, types.NewSet(context.Background(), types.Format_7_18, vs, types.String("a"), types.String("b")), &s)
|
||||
assert.NoError(err)
|
||||
assert.Equal([]string{"a", "b"}, s)
|
||||
assert.Equal([]string{"b", "C"}, s2)
|
||||
@@ -645,12 +645,12 @@ func TestDecodeArray(t *testing.T) {
|
||||
s := [3]string{"", "", ""}
|
||||
|
||||
// TODO(binformat)
|
||||
err := Unmarshal(context.Background(), types.NewList(context.Background(), types.Format_7_18, vs, types.String("a"), types.String("b"), types.String("c")), &s)
|
||||
err := Unmarshal(context.Background(), types.Format_7_18, types.NewList(context.Background(), types.Format_7_18, vs, types.String("a"), types.String("b"), types.String("c")), &s)
|
||||
assert.NoError(err)
|
||||
assert.Equal([3]string{"a", "b", "c"}, s)
|
||||
|
||||
// TODO(binformat)
|
||||
err = Unmarshal(context.Background(), types.NewSet(context.Background(), types.Format_7_18, vs, types.String("a"), types.String("b"), types.String("c")), &s)
|
||||
err = Unmarshal(context.Background(), types.Format_7_18, types.NewSet(context.Background(), types.Format_7_18, vs, types.String("a"), types.String("b"), types.String("c")), &s)
|
||||
assert.NoError(err)
|
||||
assert.Equal([3]string{"a", "b", "c"}, s)
|
||||
}
|
||||
@@ -664,11 +664,11 @@ func TestDecodeArrayEmpty(t *testing.T) {
|
||||
var s [0]string
|
||||
|
||||
// TODO(binformat)
|
||||
err := Unmarshal(context.Background(), types.NewList(context.Background(), types.Format_7_18, vs), &s)
|
||||
err := Unmarshal(context.Background(), types.Format_7_18, types.NewList(context.Background(), types.Format_7_18, vs), &s)
|
||||
assert.NoError(err)
|
||||
assert.Equal([0]string{}, s)
|
||||
|
||||
err = Unmarshal(context.Background(), types.NewSet(context.Background(), types.Format_7_18, vs), &s)
|
||||
err = Unmarshal(context.Background(), types.Format_7_18, types.NewSet(context.Background(), types.Format_7_18, vs), &s)
|
||||
assert.NoError(err)
|
||||
assert.Equal([0]string{}, s)
|
||||
}
|
||||
@@ -683,14 +683,14 @@ func TestDecodeStructWithSlice(t *testing.T) {
|
||||
List []int
|
||||
}
|
||||
var s S
|
||||
err := Unmarshal(context.Background(), types.NewStruct(types.Format_7_18, "S", types.StructData{
|
||||
err := Unmarshal(context.Background(), types.Format_7_18, types.NewStruct(types.Format_7_18, "S", types.StructData{
|
||||
// TODO(binformat)
|
||||
"list": types.NewList(context.Background(), types.Format_7_18, vs, types.Float(1), types.Float(2), types.Float(3)),
|
||||
}), &s)
|
||||
assert.NoError(err)
|
||||
assert.Equal(S{[]int{1, 2, 3}}, s)
|
||||
|
||||
err = Unmarshal(context.Background(), types.NewStruct(types.Format_7_18, "S", types.StructData{
|
||||
err = Unmarshal(context.Background(), types.Format_7_18, types.NewStruct(types.Format_7_18, "S", types.StructData{
|
||||
"list": types.NewSet(context.Background(), types.Format_7_18, vs, types.Float(1), types.Float(2), types.Float(3)),
|
||||
}), &s)
|
||||
assert.NoError(err)
|
||||
@@ -707,7 +707,7 @@ func TestDecodeStructWithArrayOfNomsValue(t *testing.T) {
|
||||
List [1]types.Set
|
||||
}
|
||||
var s S
|
||||
err := Unmarshal(context.Background(), types.NewStruct(types.Format_7_18, "S", types.StructData{
|
||||
err := Unmarshal(context.Background(), types.Format_7_18, types.NewStruct(types.Format_7_18, "S", types.StructData{
|
||||
// TODO(binformat)
|
||||
"list": types.NewList(context.Background(), types.Format_7_18, vs, types.NewSet(context.Background(), types.Format_7_18, vs, types.Bool(true))),
|
||||
}), &s)
|
||||
@@ -787,7 +787,7 @@ func TestDecodeRecursive(t *testing.T) {
|
||||
})
|
||||
|
||||
var n Node
|
||||
err := Unmarshal(context.Background(), v, &n)
|
||||
err := Unmarshal(context.Background(), types.Format_7_18, v, &n)
|
||||
assert.NoError(err)
|
||||
|
||||
assert.Equal(Node{
|
||||
@@ -813,12 +813,12 @@ func TestDecodeMap(t *testing.T) {
|
||||
types.String("b"), types.Float(2),
|
||||
types.String("c"), types.Float(3))
|
||||
expectedMap := map[string]int{"a": 1, "b": 2, "c": 3}
|
||||
err := Unmarshal(context.Background(), testMap, &m)
|
||||
err := Unmarshal(context.Background(), types.Format_7_18, testMap, &m)
|
||||
assert.NoError(err)
|
||||
assert.Equal(expectedMap, m)
|
||||
|
||||
m = map[string]int{"b": 2, "c": 333}
|
||||
err = Unmarshal(context.Background(), types.NewMap(context.Background(),
|
||||
err = Unmarshal(context.Background(), types.Format_7_18, types.NewMap(context.Background(),
|
||||
types.Format_7_18,
|
||||
vs,
|
||||
types.String("a"), types.Float(1),
|
||||
@@ -831,7 +831,7 @@ func TestDecodeMap(t *testing.T) {
|
||||
}
|
||||
|
||||
var m2 map[S]bool
|
||||
err = Unmarshal(context.Background(), types.NewMap(context.Background(),
|
||||
err = Unmarshal(context.Background(), types.Format_7_18, types.NewMap(context.Background(),
|
||||
types.Format_7_18,
|
||||
vs,
|
||||
types.NewStruct(types.Format_7_18, "S", types.StructData{"n": types.String("Yes")}), types.Bool(true),
|
||||
@@ -847,12 +847,12 @@ func TestDecodeMapEmpty(t *testing.T) {
|
||||
defer vs.Close()
|
||||
|
||||
var m map[string]int
|
||||
err := Unmarshal(context.Background(), types.NewMap(context.Background(), types.Format_7_18, vs), &m)
|
||||
err := Unmarshal(context.Background(), types.Format_7_18, types.NewMap(context.Background(), types.Format_7_18, vs), &m)
|
||||
assert.NoError(err)
|
||||
assert.Equal(map[string]int(nil), m)
|
||||
|
||||
m2 := map[string]int{}
|
||||
err = Unmarshal(context.Background(), types.NewMap(context.Background(), types.Format_7_18, vs), &m2)
|
||||
err = Unmarshal(context.Background(), types.Format_7_18, types.NewMap(context.Background(), types.Format_7_18, vs), &m2)
|
||||
assert.NoError(err)
|
||||
assert.Equal(map[string]int{}, m2)
|
||||
}
|
||||
@@ -873,34 +873,34 @@ func TestDecodeOntoInterface(t *testing.T) {
|
||||
defer vs.Close()
|
||||
|
||||
var i interface{}
|
||||
err := Unmarshal(context.Background(), types.Float(1), &i)
|
||||
err := Unmarshal(context.Background(), types.Format_7_18, types.Float(1), &i)
|
||||
assert.NoError(err)
|
||||
assert.Equal(float64(1), i)
|
||||
|
||||
err = Unmarshal(context.Background(), types.String("abc"), &i)
|
||||
err = Unmarshal(context.Background(), types.Format_7_18, types.String("abc"), &i)
|
||||
assert.NoError(err)
|
||||
assert.Equal("abc", i)
|
||||
|
||||
err = Unmarshal(context.Background(), types.Bool(true), &i)
|
||||
err = Unmarshal(context.Background(), types.Format_7_18, types.Bool(true), &i)
|
||||
assert.NoError(err)
|
||||
assert.Equal(true, i)
|
||||
|
||||
// TODO(binformat)
|
||||
err = Unmarshal(context.Background(), types.NewList(context.Background(), types.Format_7_18, vs, types.String("abc")), &i)
|
||||
err = Unmarshal(context.Background(), types.Format_7_18, types.NewList(context.Background(), types.Format_7_18, vs, types.String("abc")), &i)
|
||||
assert.NoError(err)
|
||||
assert.Equal([]string{"abc"}, i)
|
||||
|
||||
err = Unmarshal(context.Background(), types.NewMap(context.Background(), types.Format_7_18, vs, types.String("abc"), types.Float(1)), &i)
|
||||
err = Unmarshal(context.Background(), types.Format_7_18, types.NewMap(context.Background(), types.Format_7_18, vs, types.String("abc"), types.Float(1)), &i)
|
||||
assert.NoError(err)
|
||||
assert.Equal(map[string]float64{"abc": float64(1)}, i)
|
||||
|
||||
// TODO(binformat)
|
||||
err = Unmarshal(context.Background(), types.NewList(context.Background(), types.Format_7_18, vs, types.String("a"), types.Bool(true), types.Float(42)), &i)
|
||||
err = Unmarshal(context.Background(), types.Format_7_18, types.NewList(context.Background(), types.Format_7_18, vs, types.String("a"), types.Bool(true), types.Float(42)), &i)
|
||||
assert.NoError(err)
|
||||
assert.Equal([]interface{}{"a", true, float64(42)}, i)
|
||||
|
||||
// TODO(binformat)
|
||||
err = Unmarshal(context.Background(), types.NewMap(context.Background(), types.Format_7_18, vs, types.String("a"), types.Bool(true), types.Float(42), types.NewList(context.Background(), types.Format_7_18, vs)), &i)
|
||||
err = Unmarshal(context.Background(), types.Format_7_18, types.NewMap(context.Background(), types.Format_7_18, vs, types.String("a"), types.Bool(true), types.Float(42), types.NewList(context.Background(), types.Format_7_18, vs)), &i)
|
||||
assert.NoError(err)
|
||||
assert.Equal(map[interface{}]interface{}{"a": true, float64(42): []interface{}(nil)}, i)
|
||||
}
|
||||
@@ -947,7 +947,7 @@ func TestDecodeSet(t *testing.T) {
|
||||
})
|
||||
|
||||
gs := T{}
|
||||
assert.NoError(Unmarshal(context.Background(), ns, &gs))
|
||||
assert.NoError(Unmarshal(context.Background(), types.Format_7_18, ns, &gs))
|
||||
assert.Equal(T{
|
||||
A: map[int]struct{}{0: {}, 1: {}, 2: {}},
|
||||
B: map[int]struct{}{3: {}, 4: {}, 5: {}},
|
||||
@@ -971,7 +971,7 @@ func TestDecodeSet(t *testing.T) {
|
||||
gs2 := T{
|
||||
A: map[int]struct{}{},
|
||||
}
|
||||
assert.NoError(Unmarshal(context.Background(), ns2, &gs2))
|
||||
assert.NoError(Unmarshal(context.Background(), types.Format_7_18, ns2, &gs2))
|
||||
assert.Equal(T{
|
||||
A: map[int]struct{}{},
|
||||
}, gs2)
|
||||
@@ -1021,7 +1021,7 @@ func TestDecodeOpt(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, t := range tc {
|
||||
err := UnmarshalOpt(context.Background(), t.in, t.opt, t.onto)
|
||||
err := UnmarshalOpt(context.Background(), types.Format_7_18, t.in, t.opt, t.onto)
|
||||
assert.Equal(t.wantValue, t.onto)
|
||||
if t.wantError == "" {
|
||||
assert.Nil(err)
|
||||
@@ -1047,7 +1047,7 @@ func TestDecodeNamedSet(t *testing.T) {
|
||||
})
|
||||
|
||||
gs := T{}
|
||||
assert.NoError(Unmarshal(context.Background(), ns, &gs))
|
||||
assert.NoError(Unmarshal(context.Background(), types.Format_7_18, ns, &gs))
|
||||
assert.Equal(T{
|
||||
map[int]struct{}{1: {}},
|
||||
}, gs)
|
||||
@@ -1063,7 +1063,7 @@ func TestDecodeSetWrongMapType(t *testing.T) {
|
||||
A map[int]int `noms:",set"`
|
||||
}
|
||||
|
||||
err := Unmarshal(context.Background(), types.NewStruct(types.Format_7_18, "T1", types.StructData{
|
||||
err := Unmarshal(context.Background(), types.Format_7_18, types.NewStruct(types.Format_7_18, "T1", types.StructData{
|
||||
"a": types.NewSet(context.Background(), types.Format_7_18, vs, types.Float(0)),
|
||||
}), &T1{})
|
||||
assert.Error(err)
|
||||
@@ -1073,7 +1073,7 @@ func TestDecodeSetWrongMapType(t *testing.T) {
|
||||
A map[int]struct{}
|
||||
}
|
||||
|
||||
err = Unmarshal(context.Background(), types.NewStruct(types.Format_7_18, "T2", types.StructData{
|
||||
err = Unmarshal(context.Background(), types.Format_7_18, types.NewStruct(types.Format_7_18, "T2", types.StructData{
|
||||
"a": types.NewSet(context.Background(), types.Format_7_18, vs, types.Float(0)),
|
||||
}), &T2{})
|
||||
assert.Error(err)
|
||||
@@ -1083,7 +1083,7 @@ func TestDecodeSetWrongMapType(t *testing.T) {
|
||||
A map[int]struct{} `noms:",set"`
|
||||
}
|
||||
|
||||
err = Unmarshal(context.Background(), types.NewStruct(types.Format_7_18, "T3", types.StructData{
|
||||
err = Unmarshal(context.Background(), types.Format_7_18, types.NewStruct(types.Format_7_18, "T3", types.StructData{
|
||||
"a": types.NewMap(context.Background(), types.Format_7_18, vs, types.Float(0), types.EmptyStruct(types.Format_7_18)),
|
||||
}), &T3{})
|
||||
assert.Error(err)
|
||||
@@ -1109,7 +1109,7 @@ func TestDecodeOmitEmpty(t *testing.T) {
|
||||
},
|
||||
}
|
||||
var actual S
|
||||
err := Unmarshal(context.Background(), types.NewStruct(types.Format_7_18, "S", types.StructData{
|
||||
err := Unmarshal(context.Background(), types.Format_7_18, types.NewStruct(types.Format_7_18, "S", types.StructData{
|
||||
"bar": types.NewStruct(types.Format_7_18, "", types.StructData{
|
||||
"baz": types.Float(42),
|
||||
}),
|
||||
@@ -1135,7 +1135,7 @@ func TestDecodeOriginal(t *testing.T) {
|
||||
Baz: input,
|
||||
}
|
||||
var actual S
|
||||
err := Unmarshal(context.Background(), input, &actual)
|
||||
err := Unmarshal(context.Background(), types.Format_7_18, input, &actual)
|
||||
assert.NoError(err)
|
||||
assert.True(expected.Bar.Equals(types.Format_7_18, actual.Bar))
|
||||
}
|
||||
@@ -1148,7 +1148,7 @@ func TestDecodeOriginalReceiveTypeError(t *testing.T) {
|
||||
}
|
||||
input := types.NewStruct(types.Format_7_18, "S", types.StructData{})
|
||||
var actual S
|
||||
err := Unmarshal(context.Background(), input, &actual)
|
||||
err := Unmarshal(context.Background(), types.Format_7_18, input, &actual)
|
||||
assert.Error(err)
|
||||
assert.Equal(`Cannot unmarshal Struct S {} into Go value of type marshal.S, field with tag "original" must have type Struct`, err.Error())
|
||||
}
|
||||
@@ -1161,14 +1161,14 @@ func TestDecodeCanSkipUnexportedField(t *testing.T) {
|
||||
notExported bool `noms:"-"`
|
||||
}
|
||||
var s S
|
||||
err := Unmarshal(context.Background(), types.NewStruct(types.Format_7_18, "S", types.StructData{
|
||||
err := Unmarshal(context.Background(), types.Format_7_18, types.NewStruct(types.Format_7_18, "S", types.StructData{
|
||||
"abc": types.Float(42),
|
||||
}), &s)
|
||||
assert.NoError(err)
|
||||
assert.Equal(S{42, false}, s)
|
||||
}
|
||||
|
||||
func (u *primitiveType) UnmarshalNoms(ctx context.Context, v types.Value) error {
|
||||
func (u *primitiveType) UnmarshalNoms(ctx context.Context, f *types.Format, v types.Value) error {
|
||||
*u = primitiveType(v.(types.Float) - 1)
|
||||
return nil
|
||||
}
|
||||
@@ -1178,11 +1178,11 @@ func TestUnmarshalerPrimitiveType(t *testing.T) {
|
||||
|
||||
v := types.Float(43)
|
||||
u := primitiveType(0)
|
||||
assert.NoError(Unmarshal(context.Background(), v, &u))
|
||||
assert.NoError(Unmarshal(context.Background(), types.Format_7_18, v, &u))
|
||||
assert.Equal(primitiveType(42), u)
|
||||
}
|
||||
|
||||
func (u *primitiveSliceType) UnmarshalNoms(ctx context.Context, v types.Value) error {
|
||||
func (u *primitiveSliceType) UnmarshalNoms(ctx context.Context, f *types.Format, v types.Value) error {
|
||||
sv := string(v.(types.String))
|
||||
spl := strings.Split(sv, ",")
|
||||
*u = make(primitiveSliceType, len(spl))
|
||||
@@ -1197,11 +1197,11 @@ func TestUnmarshalerPrimitiveSliceType(t *testing.T) {
|
||||
|
||||
v := types.String("a,b,c")
|
||||
u := primitiveSliceType{}
|
||||
assert.NoError(Unmarshal(context.Background(), v, &u))
|
||||
assert.NoError(Unmarshal(context.Background(), types.Format_7_18, v, &u))
|
||||
assert.Equal(primitiveSliceType{"a", "b", "c"}, u)
|
||||
}
|
||||
|
||||
func (u *primitiveMapType) UnmarshalNoms(ctx context.Context, v types.Value) error {
|
||||
func (u *primitiveMapType) UnmarshalNoms(ctx context.Context, f *types.Format, v types.Value) error {
|
||||
*u = primitiveMapType{}
|
||||
v.(types.Set).IterAll(context.Background(), func(v types.Value) {
|
||||
sv := v.(types.String)
|
||||
@@ -1220,14 +1220,14 @@ func TestUnmarshalerPrimitiveMapType(t *testing.T) {
|
||||
|
||||
v := types.NewSet(context.Background(), types.Format_7_18, vs, types.String("a,foo"), types.String("b,bar"))
|
||||
u := primitiveMapType{}
|
||||
assert.NoError(Unmarshal(context.Background(), v, &u))
|
||||
assert.NoError(Unmarshal(context.Background(), types.Format_7_18, v, &u))
|
||||
assert.Equal(primitiveMapType(map[string]string{
|
||||
"a": "foo",
|
||||
"b": "bar",
|
||||
}), u)
|
||||
}
|
||||
|
||||
func (u *primitiveStructType) UnmarshalNoms(ctx context.Context, v types.Value) error {
|
||||
func (u *primitiveStructType) UnmarshalNoms(ctx context.Context, f *types.Format, v types.Value) error {
|
||||
n := int(v.(types.Float))
|
||||
u.x = n / 3
|
||||
u.y = n % 3
|
||||
@@ -1239,11 +1239,11 @@ func TestUnmarshalerPrimitiveStructType(t *testing.T) {
|
||||
|
||||
v := types.Float(10)
|
||||
u := primitiveStructType{}
|
||||
assert.NoError(Unmarshal(context.Background(), v, &u))
|
||||
assert.NoError(Unmarshal(context.Background(), types.Format_7_18, v, &u))
|
||||
assert.Equal(primitiveStructType{3, 1}, u)
|
||||
}
|
||||
|
||||
func (u *builtinType) UnmarshalNoms(ctx context.Context, v types.Value) error {
|
||||
func (u *builtinType) UnmarshalNoms(ctx context.Context, f *types.Format, v types.Value) error {
|
||||
sv := v.(types.String)
|
||||
*u = builtinType(*regexp.MustCompile(string(sv)))
|
||||
return nil
|
||||
@@ -1255,12 +1255,12 @@ func TestUnmarshalerBuiltinType(t *testing.T) {
|
||||
s := "[a-z]+$"
|
||||
v := types.String(s)
|
||||
u := builtinType{}
|
||||
assert.NoError(Unmarshal(context.Background(), v, &u))
|
||||
assert.NoError(Unmarshal(context.Background(), types.Format_7_18, v, &u))
|
||||
r := regexp.Regexp(u)
|
||||
assert.Equal(s, r.String())
|
||||
}
|
||||
|
||||
func (u *wrappedMarshalerType) UnmarshalNoms(ctx context.Context, v types.Value) error {
|
||||
func (u *wrappedMarshalerType) UnmarshalNoms(ctx context.Context, f *types.Format, v types.Value) error {
|
||||
n := v.(types.Float)
|
||||
*u = wrappedMarshalerType(int(n) - 2)
|
||||
return nil
|
||||
@@ -1271,7 +1271,7 @@ func TestUnmarshalerWrappedMarshalerType(t *testing.T) {
|
||||
|
||||
v := types.Float(44)
|
||||
u := wrappedMarshalerType(0)
|
||||
assert.NoError(Unmarshal(context.Background(), v, &u))
|
||||
assert.NoError(Unmarshal(context.Background(), types.Format_7_18, v, &u))
|
||||
assert.Equal(wrappedMarshalerType(42), u)
|
||||
}
|
||||
|
||||
@@ -1293,7 +1293,7 @@ func TestUnmarshalerComplexStructType(t *testing.T) {
|
||||
"b": types.String(s),
|
||||
})
|
||||
u := TestComplexStructType{}
|
||||
assert.NoError(Unmarshal(context.Background(), v, &u))
|
||||
assert.NoError(Unmarshal(context.Background(), types.Format_7_18, v, &u))
|
||||
assert.Equal(TestComplexStructType{
|
||||
P: 42,
|
||||
Ps: []primitiveType{1, 2},
|
||||
@@ -1311,13 +1311,13 @@ func TestUnmarshalerComplexStructType(t *testing.T) {
|
||||
}, u)
|
||||
}
|
||||
|
||||
func (u *returnsMarshalerError) UnmarshalNoms(ctx context.Context, v types.Value) error {
|
||||
func (u *returnsMarshalerError) UnmarshalNoms(ctx context.Context, f *types.Format, v types.Value) error {
|
||||
// Can't use u.err because an empty returnsMarshalerError is created for each
|
||||
// call to UnmarshalNoms.
|
||||
return errors.New("foo bar baz")
|
||||
}
|
||||
|
||||
func (u panicsMarshaler) UnmarshalNoms(ctx context.Context, v types.Value) error {
|
||||
func (u panicsMarshaler) UnmarshalNoms(ctx context.Context, f *types.Format, v types.Value) error {
|
||||
panic("panic")
|
||||
}
|
||||
|
||||
@@ -1325,18 +1325,18 @@ func TestUnmarshalerError(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
m1 := returnsMarshalerError{}
|
||||
err := Unmarshal(context.Background(), types.EmptyStruct(types.Format_7_18), &m1)
|
||||
err := Unmarshal(context.Background(), types.Format_7_18, types.EmptyStruct(types.Format_7_18), &m1)
|
||||
assert.Equal(errors.New("foo bar baz"), err)
|
||||
|
||||
m2 := panicsMarshaler{}
|
||||
assert.Panics(func() { Unmarshal(context.Background(), types.EmptyStruct(types.Format_7_18), &m2) })
|
||||
assert.Panics(func() { Unmarshal(context.Background(), types.Format_7_18, types.EmptyStruct(types.Format_7_18), &m2) })
|
||||
}
|
||||
|
||||
type notPointer struct {
|
||||
x int
|
||||
}
|
||||
|
||||
func (u notPointer) UnmarshalNoms(ctx context.Context, v types.Value) error {
|
||||
func (u notPointer) UnmarshalNoms(ctx context.Context, f *types.Format, v types.Value) error {
|
||||
u.x++
|
||||
return nil
|
||||
}
|
||||
@@ -1345,9 +1345,9 @@ func TestUnmarshalNomsNotPointerDoesNotShareState(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
u := notPointer{0}
|
||||
assert.NoError(Unmarshal(context.Background(), types.EmptyStruct(types.Format_7_18), &u))
|
||||
assert.NoError(Unmarshal(context.Background(), types.EmptyStruct(types.Format_7_18), &u))
|
||||
assert.NoError(Unmarshal(context.Background(), types.EmptyStruct(types.Format_7_18), &u))
|
||||
assert.NoError(Unmarshal(context.Background(), types.Format_7_18, types.EmptyStruct(types.Format_7_18), &u))
|
||||
assert.NoError(Unmarshal(context.Background(), types.Format_7_18, types.EmptyStruct(types.Format_7_18), &u))
|
||||
assert.NoError(Unmarshal(context.Background(), types.Format_7_18, types.EmptyStruct(types.Format_7_18), &u))
|
||||
assert.Equal(notPointer{0}, u)
|
||||
}
|
||||
|
||||
@@ -1361,8 +1361,8 @@ func TestUnmarshalMustUnmarshal(t *testing.T) {
|
||||
|
||||
v := MustMarshal(context.Background(), vs, types.Float(1))
|
||||
var out TestStruct
|
||||
a.Panics(func() { MustUnmarshal(context.Background(), v, &out) })
|
||||
a.Panics(func() { MustUnmarshal(context.Background(), types.Format_7_18, v, &out) })
|
||||
|
||||
v = MustMarshal(context.Background(), vs, TestStruct{2})
|
||||
a.NotPanics(func() { MustUnmarshal(context.Background(), v, &out) })
|
||||
a.NotPanics(func() { MustUnmarshal(context.Background(), types.Format_7_18, v, &out) })
|
||||
}
|
||||
|
||||
+17
-17
@@ -257,14 +257,14 @@ func typeEncoder(format *types.Format, t reflect.Type, seenStructs map[string]re
|
||||
return structEncoder(format, t, seenStructs)
|
||||
case reflect.Slice, reflect.Array:
|
||||
if shouldEncodeAsSet(t, tags) {
|
||||
return setFromListEncoder(t, seenStructs)
|
||||
return setFromListEncoder(format, t, seenStructs)
|
||||
}
|
||||
return listEncoder(t, seenStructs)
|
||||
return listEncoder(format, t, seenStructs)
|
||||
case reflect.Map:
|
||||
if shouldEncodeAsSet(t, tags) {
|
||||
return setEncoder(t, seenStructs)
|
||||
return setEncoder(format, t, seenStructs)
|
||||
}
|
||||
return mapEncoder(t, seenStructs)
|
||||
return mapEncoder(format, t, seenStructs)
|
||||
case reflect.Interface:
|
||||
return func(ctx context.Context, v reflect.Value, vrw types.ValueReadWriter) types.Value {
|
||||
// Get the dynamic type.
|
||||
@@ -523,7 +523,7 @@ func typeFields(format *types.Format, t reflect.Type, seenStructs map[string]ref
|
||||
return
|
||||
}
|
||||
|
||||
func listEncoder(t reflect.Type, seenStructs map[string]reflect.Type) encoderFunc {
|
||||
func listEncoder(f *types.Format, t reflect.Type, seenStructs map[string]reflect.Type) encoderFunc {
|
||||
e := encoderCache.get(t)
|
||||
if e != nil {
|
||||
return e
|
||||
@@ -542,16 +542,16 @@ func listEncoder(t reflect.Type, seenStructs map[string]reflect.Type) encoderFun
|
||||
values[i] = elemEncoder(ctx, v.Index(i), vrw)
|
||||
}
|
||||
// TODO(binformat)
|
||||
return types.NewList(ctx, types.Format_7_18, vrw, values...)
|
||||
return types.NewList(ctx, f, vrw, values...)
|
||||
}
|
||||
|
||||
encoderCache.set(t, e)
|
||||
elemEncoder = typeEncoder(types.Format_7_18, t.Elem(), seenStructs, nomsTags{})
|
||||
elemEncoder = typeEncoder(f, t.Elem(), seenStructs, nomsTags{})
|
||||
return e
|
||||
}
|
||||
|
||||
// Encode set from array or slice
|
||||
func setFromListEncoder(t reflect.Type, seenStructs map[string]reflect.Type) encoderFunc {
|
||||
func setFromListEncoder(f *types.Format, t reflect.Type, seenStructs map[string]reflect.Type) encoderFunc {
|
||||
e := setEncoderCache.get(t)
|
||||
if e != nil {
|
||||
return e
|
||||
@@ -569,15 +569,15 @@ func setFromListEncoder(t reflect.Type, seenStructs map[string]reflect.Type) enc
|
||||
for i := 0; i < v.Len(); i++ {
|
||||
values[i] = elemEncoder(ctx, v.Index(i), vrw)
|
||||
}
|
||||
return types.NewSet(ctx, types.Format_7_18, vrw, values...)
|
||||
return types.NewSet(ctx, f, vrw, values...)
|
||||
}
|
||||
|
||||
setEncoderCache.set(t, e)
|
||||
elemEncoder = typeEncoder(types.Format_7_18, t.Elem(), seenStructs, nomsTags{})
|
||||
elemEncoder = typeEncoder(f, t.Elem(), seenStructs, nomsTags{})
|
||||
return e
|
||||
}
|
||||
|
||||
func setEncoder(t reflect.Type, seenStructs map[string]reflect.Type) encoderFunc {
|
||||
func setEncoder(f *types.Format, t reflect.Type, seenStructs map[string]reflect.Type) encoderFunc {
|
||||
e := setEncoderCache.get(t)
|
||||
if e != nil {
|
||||
return e
|
||||
@@ -595,15 +595,15 @@ func setEncoder(t reflect.Type, seenStructs map[string]reflect.Type) encoderFunc
|
||||
for i, k := range v.MapKeys() {
|
||||
values[i] = encoder(ctx, k, vrw)
|
||||
}
|
||||
return types.NewSet(ctx, types.Format_7_18, vrw, values...)
|
||||
return types.NewSet(ctx, f, vrw, values...)
|
||||
}
|
||||
|
||||
setEncoderCache.set(t, e)
|
||||
encoder = typeEncoder(types.Format_7_18, t.Key(), seenStructs, nomsTags{})
|
||||
encoder = typeEncoder(f, t.Key(), seenStructs, nomsTags{})
|
||||
return e
|
||||
}
|
||||
|
||||
func mapEncoder(t reflect.Type, seenStructs map[string]reflect.Type) encoderFunc {
|
||||
func mapEncoder(f *types.Format, t reflect.Type, seenStructs map[string]reflect.Type) encoderFunc {
|
||||
e := encoderCache.get(t)
|
||||
if e != nil {
|
||||
return e
|
||||
@@ -624,12 +624,12 @@ func mapEncoder(t reflect.Type, seenStructs map[string]reflect.Type) encoderFunc
|
||||
kvs[2*i] = keyEncoder(ctx, k, vrw)
|
||||
kvs[2*i+1] = valueEncoder(ctx, v.MapIndex(k), vrw)
|
||||
}
|
||||
return types.NewMap(ctx, types.Format_7_18, vrw, kvs...)
|
||||
return types.NewMap(ctx, f, vrw, kvs...)
|
||||
}
|
||||
|
||||
encoderCache.set(t, e)
|
||||
keyEncoder = typeEncoder(types.Format_7_18, t.Key(), seenStructs, nomsTags{})
|
||||
valueEncoder = typeEncoder(types.Format_7_18, t.Elem(), seenStructs, nomsTags{})
|
||||
keyEncoder = typeEncoder(f, t.Key(), seenStructs, nomsTags{})
|
||||
valueEncoder = typeEncoder(f, t.Elem(), seenStructs, nomsTags{})
|
||||
return e
|
||||
}
|
||||
|
||||
|
||||
@@ -882,21 +882,21 @@ func TestEncodeOriginal(t *testing.T) {
|
||||
orig := types.NewStruct(types.Format_7_18, "S", types.StructData{
|
||||
"foo": types.Float(42),
|
||||
})
|
||||
err = Unmarshal(context.Background(), orig, &s)
|
||||
err = Unmarshal(context.Background(), types.Format_7_18, orig, &s)
|
||||
assert.NoError(err)
|
||||
s.Foo = 43
|
||||
assert.True(MustMarshal(context.Background(), vs, s).Equals(types.Format_7_18, orig.Set("foo", types.Float(43))))
|
||||
|
||||
// New field extends old struct
|
||||
orig = types.NewStruct(types.Format_7_18, "S", types.StructData{})
|
||||
err = Unmarshal(context.Background(), orig, &s)
|
||||
err = Unmarshal(context.Background(), types.Format_7_18, orig, &s)
|
||||
assert.NoError(err)
|
||||
s.Foo = 43
|
||||
assert.True(MustMarshal(context.Background(), vs, s).Equals(types.Format_7_18, orig.Set("foo", types.Float(43))))
|
||||
|
||||
// Old struct name always used
|
||||
orig = types.NewStruct(types.Format_7_18, "Q", types.StructData{})
|
||||
err = Unmarshal(context.Background(), orig, &s)
|
||||
err = Unmarshal(context.Background(), types.Format_7_18, orig, &s)
|
||||
assert.NoError(err)
|
||||
s.Foo = 43
|
||||
assert.True(MustMarshal(context.Background(), vs, s).Equals(types.Format_7_18, orig.Set("foo", types.Float(43))))
|
||||
@@ -905,7 +905,7 @@ func TestEncodeOriginal(t *testing.T) {
|
||||
orig = types.NewStruct(types.Format_7_18, "S", types.StructData{
|
||||
"foo": types.Float(42),
|
||||
})
|
||||
err = Unmarshal(context.Background(), orig, &s)
|
||||
err = Unmarshal(context.Background(), types.Format_7_18, orig, &s)
|
||||
assert.NoError(err)
|
||||
s.Foo = 43
|
||||
out := MustMarshal(context.Background(), vs, s)
|
||||
|
||||
@@ -62,11 +62,11 @@ func (dt DateTime) MarshalNomsType() (*types.Type, error) {
|
||||
// UnmarshalNoms makes DateTime implement marshal.Unmarshaler and it allows
|
||||
// Noms struct with type DateTimeType able to be unmarshaled onto a DateTime
|
||||
// Go struct
|
||||
func (dt *DateTime) UnmarshalNoms(ctx context.Context, v types.Value) error {
|
||||
func (dt *DateTime) UnmarshalNoms(ctx context.Context, f *types.Format, v types.Value) error {
|
||||
strct := struct {
|
||||
SecSinceEpoch float64
|
||||
}{}
|
||||
err := marshal.Unmarshal(ctx, v, &strct)
|
||||
err := marshal.Unmarshal(ctx, f, v, &strct)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -85,7 +85,7 @@ func (c DateTimeCommenter) Comment(ctx context.Context, f *types.Format, v types
|
||||
return ""
|
||||
}
|
||||
var dt DateTime
|
||||
marshal.MustUnmarshal(ctx, v, &dt)
|
||||
marshal.MustUnmarshal(ctx, f, v, &dt)
|
||||
return dt.In(c.tz).Format(time.RFC3339)
|
||||
}
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ func TestBasics(t *testing.T) {
|
||||
assert.NoError(err)
|
||||
|
||||
var dt2 DateTime
|
||||
err = marshal.Unmarshal(context.Background(), nomsValue, &dt2)
|
||||
err = marshal.Unmarshal(context.Background(), types.Format_7_18, nomsValue, &dt2)
|
||||
assert.NoError(err)
|
||||
|
||||
assert.True(dt.Equal(dt2.Time))
|
||||
@@ -40,7 +40,7 @@ func TestUnmarshal(t *testing.T) {
|
||||
|
||||
test := func(v types.Struct, t time.Time) {
|
||||
var dt DateTime
|
||||
err := marshal.Unmarshal(context.Background(), v, &dt)
|
||||
err := marshal.Unmarshal(context.Background(), types.Format_7_18, v, &dt)
|
||||
assert.NoError(err)
|
||||
assert.True(dt.Equal(t))
|
||||
}
|
||||
@@ -62,7 +62,7 @@ func TestUnmarshalInvalid(t *testing.T) {
|
||||
|
||||
test := func(v types.Value) {
|
||||
var dt DateTime
|
||||
err := marshal.Unmarshal(context.Background(), v, &dt)
|
||||
err := marshal.Unmarshal(context.Background(), types.Format_7_18, v, &dt)
|
||||
assert.Error(err)
|
||||
}
|
||||
|
||||
@@ -134,11 +134,11 @@ func TestZeroValues(t *testing.T) {
|
||||
nomsDate, _ := dt1.MarshalNoms(vs)
|
||||
|
||||
dt2 := DateTime{}
|
||||
marshal.Unmarshal(context.Background(), nomsDate, &dt2)
|
||||
marshal.Unmarshal(context.Background(), types.Format_7_18, nomsDate, &dt2)
|
||||
assert.True(dt2.IsZero())
|
||||
|
||||
dt3 := DateTime{}
|
||||
dt3.UnmarshalNoms(context.Background(), nomsDate)
|
||||
dt3.UnmarshalNoms(context.Background(), types.Format_7_18, nomsDate)
|
||||
assert.True(dt3.IsZero())
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user