Changes
2 changed files (+93/-1)
-
-
@@ -196,6 +196,35 @@ func (l *LangRefValue) UnmarshalText(data []byte) error {return nil } func encodeGobStringLikeType(g *gob.Encoder, s []byte) error { if err := g.Encode(s); err != nil { return err } return nil } func (l LangRef) GobEncode() ([]byte, error) { b := new(bytes.Buffer) gg := gob.NewEncoder(b) if err := encodeGobStringLikeType(gg, []byte(l)); err != nil { return nil, err } return b.Bytes(), nil } func (l *LangRef) GobDecode(data []byte) error { if len(data) == 0 { // NOTE(marius): this behaviour diverges from vanilla gob package return nil } var bb []byte if err := gob.NewDecoder(bytes.NewReader(data)).Decode(&bb); err != nil { return err } *l = LangRef(bb) return nil } // MarshalJSON encodes the receiver object to a JSON document. func (l LangRefValue) MarshalJSON() ([]byte, error) { buf := bytes.Buffer{}
-
@@ -278,7 +307,7 @@ func (c *Content) UnmarshalText(data []byte) error {func (c Content) GobEncode() ([]byte, error) { b := new(bytes.Buffer) gg := gob.NewEncoder(b) if err := gg.Encode([]byte(c)); err != nil { if err := encodeGobStringLikeType(gg, []byte(c)); err != nil { return nil, err } return b.Bytes(), nil
-
-
-
@@ -625,3 +625,66 @@ func TestContent_GobDecode(t *testing.T) {}) } } func TestLangRef_GobDecode(t *testing.T) { tests := []struct { name string l LangRef data []byte wantErr bool }{ { name: "empty", l: "", data: []byte{}, wantErr: false, }, { name: "some text", l: LangRef("ana are"), data: gobValue([]byte{'a', 'n', 'a', ' ', 'a', 'r', 'e'}), wantErr: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := tt.l.GobDecode(tt.data); (err != nil) != tt.wantErr { t.Errorf("GobDecode() error = %v, wantErr %v", err, tt.wantErr) } }) } } func TestLangRef_GobEncode(t *testing.T) { tests := []struct { name string l LangRef want []byte wantErr bool }{ { name: "empty", l: "", want: gobValue([]byte{}), wantErr: false, }, { name: "some text", l: LangRef("ana are"), want: gobValue([]byte{'a', 'n', 'a', ' ', 'a', 'r', 'e'}), wantErr: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := tt.l.GobEncode() if (err != nil) != tt.wantErr { t.Errorf("GobEncode() error = %v, wantErr %v", err, tt.wantErr) return } if !reflect.DeepEqual(got, tt.want) { t.Errorf("GobEncode() got = %v, want %v", got, tt.want) } }) } }
-