From cd4d995e327bfd1d7fa0953f3f9639dac3d03780 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Mon, 8 Jan 2018 13:20:52 +0100 Subject: [PATCH] use pointer-wrapping for slices and maps This is important because named slice types might have a custom decoder that doesn't allow null. --- internal/tests/mapconv/output.go | 60 ++++++++++++------------- internal/tests/sliceconv/output.go | 72 +++++++++++++++--------------- typeutil.go | 2 +- 3 files changed, 67 insertions(+), 67 deletions(-) diff --git a/internal/tests/mapconv/output.go b/internal/tests/mapconv/output.go index 98bb3f7..4924846 100644 --- a/internal/tests/mapconv/output.go +++ b/internal/tests/mapconv/output.go @@ -43,32 +43,32 @@ func (x X) MarshalJSON() ([]byte, error) { func (x *X) UnmarshalJSON(input []byte) error { type X struct { - Map map[replacedString]replacedInt - Named namedMap2 - NoConv map[string]int - NoConvNamed namedMap + Map *map[replacedString]replacedInt + Named *namedMap2 + NoConv *map[string]int + NoConvNamed *namedMap } var dec X if err := json.Unmarshal(input, &dec); err != nil { return err } if dec.Map != nil { - x.Map = make(map[string]int, len(dec.Map)) - for k, v := range dec.Map { + x.Map = make(map[string]int, len(*dec.Map)) + for k, v := range *dec.Map { x.Map[string(k)] = int(v) } } if dec.Named != nil { - x.Named = make(namedMap, len(dec.Named)) - for k, v := range dec.Named { + x.Named = make(namedMap, len(*dec.Named)) + for k, v := range *dec.Named { x.Named[string(k)] = int(v) } } if dec.NoConv != nil { - x.NoConv = dec.NoConv + x.NoConv = *dec.NoConv } if dec.NoConvNamed != nil { - x.NoConvNamed = dec.NoConvNamed + x.NoConvNamed = *dec.NoConvNamed } return nil } @@ -108,32 +108,32 @@ func (x X) MarshalYAML() (interface{}, error) { func (x *X) UnmarshalYAML(unmarshal func(interface{}) error) error { type X struct { - Map map[replacedString]replacedInt - Named namedMap2 - NoConv map[string]int - NoConvNamed namedMap + Map *map[replacedString]replacedInt + Named *namedMap2 + NoConv *map[string]int + NoConvNamed *namedMap } var dec X if err := unmarshal(&dec); err != nil { return err } if dec.Map != nil { - x.Map = make(map[string]int, len(dec.Map)) - for k, v := range dec.Map { + x.Map = make(map[string]int, len(*dec.Map)) + for k, v := range *dec.Map { x.Map[string(k)] = int(v) } } if dec.Named != nil { - x.Named = make(namedMap, len(dec.Named)) - for k, v := range dec.Named { + x.Named = make(namedMap, len(*dec.Named)) + for k, v := range *dec.Named { x.Named[string(k)] = int(v) } } if dec.NoConv != nil { - x.NoConv = dec.NoConv + x.NoConv = *dec.NoConv } if dec.NoConvNamed != nil { - x.NoConvNamed = dec.NoConvNamed + x.NoConvNamed = *dec.NoConvNamed } return nil } @@ -173,32 +173,32 @@ func (x X) MarshalTOML() (interface{}, error) { func (x *X) UnmarshalTOML(unmarshal func(interface{}) error) error { type X struct { - Map map[replacedString]replacedInt - Named namedMap2 - NoConv map[string]int - NoConvNamed namedMap + Map *map[replacedString]replacedInt + Named *namedMap2 + NoConv *map[string]int + NoConvNamed *namedMap } var dec X if err := unmarshal(&dec); err != nil { return err } if dec.Map != nil { - x.Map = make(map[string]int, len(dec.Map)) - for k, v := range dec.Map { + x.Map = make(map[string]int, len(*dec.Map)) + for k, v := range *dec.Map { x.Map[string(k)] = int(v) } } if dec.Named != nil { - x.Named = make(namedMap, len(dec.Named)) - for k, v := range dec.Named { + x.Named = make(namedMap, len(*dec.Named)) + for k, v := range *dec.Named { x.Named[string(k)] = int(v) } } if dec.NoConv != nil { - x.NoConv = dec.NoConv + x.NoConv = *dec.NoConv } if dec.NoConvNamed != nil { - x.NoConvNamed = dec.NoConvNamed + x.NoConvNamed = *dec.NoConvNamed } return nil } diff --git a/internal/tests/sliceconv/output.go b/internal/tests/sliceconv/output.go index d0e26d8..8fd4207 100644 --- a/internal/tests/sliceconv/output.go +++ b/internal/tests/sliceconv/output.go @@ -45,36 +45,36 @@ func (x X) MarshalJSON() ([]byte, error) { func (x *X) UnmarshalJSON(input []byte) error { type X struct { - Slice []replacedInt - Named namedSlice2 - ByteString []byte - NoConv []int - NoConvNamed namedSlice + Slice *[]replacedInt + Named *namedSlice2 + ByteString *[]byte + NoConv *[]int + NoConvNamed *namedSlice } var dec X if err := json.Unmarshal(input, &dec); err != nil { return err } if dec.Slice != nil { - x.Slice = make([]int, len(dec.Slice)) - for k, v := range dec.Slice { + x.Slice = make([]int, len(*dec.Slice)) + for k, v := range *dec.Slice { x.Slice[k] = int(v) } } if dec.Named != nil { - x.Named = make(namedSlice, len(dec.Named)) - for k, v := range dec.Named { + x.Named = make(namedSlice, len(*dec.Named)) + for k, v := range *dec.Named { x.Named[k] = int(v) } } if dec.ByteString != nil { - x.ByteString = string(dec.ByteString) + x.ByteString = string(*dec.ByteString) } if dec.NoConv != nil { - x.NoConv = dec.NoConv + x.NoConv = *dec.NoConv } if dec.NoConvNamed != nil { - x.NoConvNamed = dec.NoConvNamed + x.NoConvNamed = *dec.NoConvNamed } return nil } @@ -116,36 +116,36 @@ func (x X) MarshalYAML() (interface{}, error) { func (x *X) UnmarshalYAML(unmarshal func(interface{}) error) error { type X struct { - Slice []replacedInt - Named namedSlice2 - ByteString []byte - NoConv []int - NoConvNamed namedSlice + Slice *[]replacedInt + Named *namedSlice2 + ByteString *[]byte + NoConv *[]int + NoConvNamed *namedSlice } var dec X if err := unmarshal(&dec); err != nil { return err } if dec.Slice != nil { - x.Slice = make([]int, len(dec.Slice)) - for k, v := range dec.Slice { + x.Slice = make([]int, len(*dec.Slice)) + for k, v := range *dec.Slice { x.Slice[k] = int(v) } } if dec.Named != nil { - x.Named = make(namedSlice, len(dec.Named)) - for k, v := range dec.Named { + x.Named = make(namedSlice, len(*dec.Named)) + for k, v := range *dec.Named { x.Named[k] = int(v) } } if dec.ByteString != nil { - x.ByteString = string(dec.ByteString) + x.ByteString = string(*dec.ByteString) } if dec.NoConv != nil { - x.NoConv = dec.NoConv + x.NoConv = *dec.NoConv } if dec.NoConvNamed != nil { - x.NoConvNamed = dec.NoConvNamed + x.NoConvNamed = *dec.NoConvNamed } return nil } @@ -187,36 +187,36 @@ func (x X) MarshalTOML() (interface{}, error) { func (x *X) UnmarshalTOML(unmarshal func(interface{}) error) error { type X struct { - Slice []replacedInt - Named namedSlice2 - ByteString []byte - NoConv []int - NoConvNamed namedSlice + Slice *[]replacedInt + Named *namedSlice2 + ByteString *[]byte + NoConv *[]int + NoConvNamed *namedSlice } var dec X if err := unmarshal(&dec); err != nil { return err } if dec.Slice != nil { - x.Slice = make([]int, len(dec.Slice)) - for k, v := range dec.Slice { + x.Slice = make([]int, len(*dec.Slice)) + for k, v := range *dec.Slice { x.Slice[k] = int(v) } } if dec.Named != nil { - x.Named = make(namedSlice, len(dec.Named)) - for k, v := range dec.Named { + x.Named = make(namedSlice, len(*dec.Named)) + for k, v := range *dec.Named { x.Named[k] = int(v) } } if dec.ByteString != nil { - x.ByteString = string(dec.ByteString) + x.ByteString = string(*dec.ByteString) } if dec.NoConv != nil { - x.NoConv = dec.NoConv + x.NoConv = *dec.NoConv } if dec.NoConvNamed != nil { - x.NoConvNamed = dec.NoConvNamed + x.NoConvNamed = *dec.NoConvNamed } return nil } diff --git a/typeutil.go b/typeutil.go index 73f73ea..774272e 100644 --- a/typeutil.go +++ b/typeutil.go @@ -102,7 +102,7 @@ func ensureNilCheckable(typ types.Type) types.Type { switch typ.(type) { case *types.Named: typ = typ.Underlying() - case *types.Map, *types.Slice, *types.Pointer, *types.Interface: + case *types.Pointer, *types.Interface: return orig default: return types.NewPointer(orig)