gojay

high performance JSON encoder/decoder with stream API for Golang
git clone git://git.lair.cx/gojay
Log | Files | Refs | README | LICENSE

commit 784b1a24f18898b9e31be38609a27a739cb399cc
parent 28447855779c4df0f19d1840af686fe53b9e3838
Author: francoispqt <francois@parquet.ninja>
Date:   Sun, 17 Mar 2019 23:36:50 +0800

fix codegen tests and readme

Diffstat:
Mgojay/README.md | 14+++++++-------
Mgojay/codegen/generator.go | 1+
Mgojay/codegen/struct.go | 1+
Mgojay/codegen/template.go | 4+++-
Mgojay/codegen/test/annotated_struct/encoding.go | 173++++++++++++++++++++++++++++++++++++++++++-------------------------------------
Mgojay/codegen/test/annotated_struct/encoding_test.go | 216+++++++++++++++++++++++++------------------------------------------------------
Mgojay/codegen/test/annotated_struct/message.go | 29++++++++++++++++-------------
Mgojay/codegen/test/basic_struct/encoding.go | 115++++++++++++++++++++++++++++++++++++++++++++-----------------------------------
Mgojay/codegen/test/basic_struct/encoding_test.go | 134+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mgojay/codegen/test/basic_struct/message.go | 25++++++++++++++-----------
Mgojay/codegen/test/embedded_struct/encoding.go | 81++++++++++++++++++++++++++++++++++++++++---------------------------------------
Mgojay/codegen/test/embedded_struct/message.go | 2+-
Mgojay/codegen/test/pooled_struct/encoding.go | 33+++++++++++++++++----------------
Mgojay/codegen/test/pooled_struct/message.go | 2+-
Agojay/gojay.go | 23+++++++++++++++++++++++
Dgojay/gojaygen.go | 23-----------------------
16 files changed, 415 insertions(+), 461 deletions(-)

diff --git a/gojay/README.md b/gojay/README.md @@ -19,20 +19,20 @@ gojaygen -s . -p true -t MyType -o output.go If you just want to the output to stdout, omit the -o flag. ### Using flags -- s file/dir path, can be a relative or absolute path -- t root types to generate with all its dependencies (comma separated) -- a annotation tag used to read meta data (default: json) -- o output file (relative or absolute path) -- p reuse object witt sync.Pool +- s Source file/dir path, can be a relative or absolute path +- t Types to generate with all its dependencies (comma separated) +- a Annotation tag used to read metadata (default: json) +- o Output file (relative or absolute path) +- p Pool to reuse object (using sync.Pool) Examples: -- Specific type in a go package, write to a file: +- Generate `SomeType` type in `/tmp/myproj` go package, write to file `output.go`: ```sh gojay -s /tmp/myproj -t SomeType -o output.go ``` -- Specific types in a file, with custom tag, write to stdout +- Generate type `SomeType` in file `somegofile.go`, with custom tag `gojay`, write to stdout: ```sh gojay -s somegofile.go -a gojay -t SomeType ``` diff --git a/gojay/codegen/generator.go b/gojay/codegen/generator.go @@ -120,6 +120,7 @@ func (g *Generator) writeCode() error { return err } + // code destination is empty, we just print to stdout if g.options.Dest == "" { fmt.Print(string(code)) return nil diff --git a/gojay/codegen/struct.go b/gojay/codegen/struct.go @@ -101,6 +101,7 @@ func (s *Struct) generateFieldReset(fields []*toolbox.FieldInfo) ([]string, erro } func (s *Struct) generateFieldDecoding(fields []*toolbox.FieldInfo) (string, []string, error) { + fieldCases := []string{} var initCode = "" for i := range fields { diff --git a/gojay/codegen/template.go b/gojay/codegen/template.go @@ -149,7 +149,9 @@ const ( ) var blockTemplate = map[int]string{ - fileCode: `// Code generated by GoJayGen. DO NOT EDIT.\n\n + fileCode: `// Code generated by Gojay. DO NOT EDIT. + + package {{.Pkg}} import ( diff --git a/gojay/codegen/test/annotated_struct/encoding.go b/gojay/codegen/test/annotated_struct/encoding.go @@ -1,32 +1,13 @@ -// Code generated by GoJayGen. DO NOT EDIT.\n\n +// Code generated by Gojay. DO NOT EDIT. + package annotated_struct import ( + "database/sql" "github.com/francoispqt/gojay" "time" ) -type SubMessages []SubMessage - -func (s *SubMessages) UnmarshalJSONArray(dec *gojay.Decoder) error { - var value = SubMessage{} - if err := dec.Object(&value); err != nil { - return err - } - *s = append(*s, value) - return nil -} - -func (s SubMessages) MarshalJSONArray(enc *gojay.Encoder) { - for i := range s { - enc.Object(&s[i]) - } -} - -func (s SubMessages) IsNil() bool { - return len(s) == 0 -} - type Ints []int // UnmarshalJSONArray decodes JSON array elements into slice @@ -51,27 +32,27 @@ func (a Ints) IsNil() bool { return len(a) == 0 } -type Float32sPtr []*float32 +type Float32s []float32 // UnmarshalJSONArray decodes JSON array elements into slice -func (a *Float32sPtr) UnmarshalJSONArray(dec *gojay.Decoder) error { +func (a *Float32s) UnmarshalJSONArray(dec *gojay.Decoder) error { var value float32 if err := dec.Float32(&value); err != nil { return err } - *a = append(*a, &value) + *a = append(*a, value) return nil } // MarshalJSONArray encodes arrays into JSON -func (a Float32sPtr) MarshalJSONArray(enc *gojay.Encoder) { +func (a Float32s) MarshalJSONArray(enc *gojay.Encoder) { for _, item := range a { - enc.Float32(*item) + enc.Float32(item) } } // IsNil checks if array is nil -func (a Float32sPtr) IsNil() bool { +func (a Float32s) IsNil() bool { return len(a) == 0 } @@ -96,68 +77,39 @@ func (s SubMessagesPtr) IsNil() bool { return len(s) == 0 } -// MarshalJSONObject implements MarshalerJSONObject -func (m *SubMessage) MarshalJSONObject(enc *gojay.Encoder) { - enc.IntKey("id", m.Id) - enc.StringKey("description", m.Description) - enc.TimeKey("startDate", &m.StartTime, "2006-01-02 15:04:05") - if m.EndTime != nil { - enc.TimeKey("endDate", m.EndTime, "2006-01-02 15:04:05") - } -} - -// IsNil checks if instance is nil -func (m *SubMessage) IsNil() bool { - return m == nil -} - -// UnmarshalJSONObject implements gojay's UnmarshalerJSONObject -func (m *SubMessage) UnmarshalJSONObject(dec *gojay.Decoder, k string) error { - - switch k { - case "id": - return dec.Int(&m.Id) - - case "description": - return dec.String(&m.Description) - - case "startDate": - var format = "2006-01-02 15:04:05" - var value = time.Time{} - err := dec.Time(&value, format) - if err == nil { - m.StartTime = value - } - return err +type SubMessages []SubMessage - case "endDate": - var format = "2006-01-02 15:04:05" - var value = &time.Time{} - err := dec.Time(value, format) - if err == nil { - m.EndTime = value - } +func (s *SubMessages) UnmarshalJSONArray(dec *gojay.Decoder) error { + var value = SubMessage{} + if err := dec.Object(&value); err != nil { return err - } + *s = append(*s, value) return nil } -// NKeys returns the number of keys to unmarshal -func (m *SubMessage) NKeys() int { return 4 } +func (s SubMessages) MarshalJSONArray(enc *gojay.Encoder) { + for i := range s { + enc.Object(&s[i]) + } +} + +func (s SubMessages) IsNil() bool { + return len(s) == 0 +} // MarshalJSONObject implements MarshalerJSONObject -func (p *Paylod) MarshalJSONObject(enc *gojay.Encoder) { +func (p *Payload) MarshalJSONObject(enc *gojay.Encoder) { } // IsNil checks if instance is nil -func (p *Paylod) IsNil() bool { +func (p *Payload) IsNil() bool { return p == nil } // UnmarshalJSONObject implements gojay's UnmarshalerJSONObject -func (p *Paylod) UnmarshalJSONObject(dec *gojay.Decoder, k string) error { +func (p *Payload) UnmarshalJSONObject(dec *gojay.Decoder, k string) error { switch k { @@ -166,7 +118,7 @@ func (p *Paylod) UnmarshalJSONObject(dec *gojay.Decoder, k string) error { } // NKeys returns the number of keys to unmarshal -func (p *Paylod) NKeys() int { return 0 } +func (p *Payload) NKeys() int { return 0 } // MarshalJSONObject implements MarshalerJSONObject func (m *Message) MarshalJSONObject(enc *gojay.Encoder) { @@ -175,7 +127,7 @@ func (m *Message) MarshalJSONObject(enc *gojay.Encoder) { enc.Float64Key("price", m.Price) var intsSlice = Ints(m.Ints) enc.ArrayKey("ints", intsSlice) - var floatsSlice = Float32sPtr(m.Floats) + var floatsSlice = Float32s(m.Floats) enc.ArrayKey("floats", floatsSlice) enc.ObjectKey("subMessageX", m.SubMessageX) var messagesXSlice = SubMessagesPtr(m.MessagesX) @@ -186,6 +138,9 @@ func (m *Message) MarshalJSONObject(enc *gojay.Encoder) { enc.BoolKey("enabled", *m.IsTrue) var payloadSlice = gojay.EmbeddedJSON(m.Payload) enc.AddEmbeddedJSONKey("data", &payloadSlice) + if m.SQLNullString != nil { + enc.SQLNullStringKey("sqlNullString", m.SQLNullString) + } } // IsNil checks if instance is nil @@ -215,10 +170,10 @@ func (m *Message) UnmarshalJSONObject(dec *gojay.Decoder, k string) error { return err case "floats": - var aSlice = Float32sPtr{} + var aSlice = Float32s{} err := dec.Array(&aSlice) if err == nil && len(aSlice) > 0 { - m.Floats = []*float32(aSlice) + m.Floats = []float32(aSlice) } return err @@ -264,7 +219,15 @@ func (m *Message) UnmarshalJSONObject(dec *gojay.Decoder, k string) error { var value = gojay.EmbeddedJSON{} err := dec.AddEmbeddedJSON(&value) if err == nil && len(value) > 0 { - m.Payload = Paylod(value) + m.Payload = Payload(value) + } + return err + + case "sqlNullString": + var value = &sql.NullString{} + err := dec.SQLNullString(value) + if err == nil { + m.SQLNullString = value } return err @@ -273,4 +236,54 @@ func (m *Message) UnmarshalJSONObject(dec *gojay.Decoder, k string) error { } // NKeys returns the number of keys to unmarshal -func (m *Message) NKeys() int { return 11 } +func (m *Message) NKeys() int { return 12 } + +// MarshalJSONObject implements MarshalerJSONObject +func (m *SubMessage) MarshalJSONObject(enc *gojay.Encoder) { + enc.IntKey("id", m.Id) + enc.StringKey("description", m.Description) + enc.TimeKey("startDate", &m.StartTime, "2006-01-02 15:04:05") + if m.EndTime != nil { + enc.TimeKey("endDate", m.EndTime, "2006-01-02 15:04:05") + } +} + +// IsNil checks if instance is nil +func (m *SubMessage) IsNil() bool { + return m == nil +} + +// UnmarshalJSONObject implements gojay's UnmarshalerJSONObject +func (m *SubMessage) UnmarshalJSONObject(dec *gojay.Decoder, k string) error { + + switch k { + case "id": + return dec.Int(&m.Id) + + case "description": + return dec.String(&m.Description) + + case "startDate": + var format = "2006-01-02 15:04:05" + var value = time.Time{} + err := dec.Time(&value, format) + if err == nil { + m.StartTime = value + } + return err + + case "endDate": + var format = "2006-01-02 15:04:05" + var value = &time.Time{} + err := dec.Time(value, format) + if err == nil { + m.EndTime = value + } + return err + + } + return nil +} + +// NKeys returns the number of keys to unmarshal +func (m *SubMessage) NKeys() int { return 4 } diff --git a/gojay/codegen/test/annotated_struct/encoding_test.go b/gojay/codegen/test/annotated_struct/encoding_test.go @@ -2,16 +2,56 @@ package annotated_struct import ( "bytes" - "github.com/francoispqt/gojay" - "github.com/stretchr/testify/assert" - "github.com/viant/assertly" + "database/sql" "log" "testing" + + "github.com/davecgh/go-spew/spew" + "github.com/francoispqt/gojay" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) -func TestMessage_Unmarshal(t *testing.T) { +var isTrue = true +var msg = &Message{ + Id: 1022, + Name: "name acc", + Price: 13.3, + Ints: []int{1, 2, 5}, + Floats: []float32{2.3, 4.6, 7.4}, + SubMessageX: &SubMessage{ + Id: 102, + Description: "abcd", + }, + MessagesX: []*SubMessage{ + &SubMessage{ + Id: 2102, + Description: "abce", + }, + }, + SubMessageY: SubMessage{ + Id: 3102, + Description: "abcf", + }, + MessagesY: []SubMessage{ + SubMessage{ + Id: 5102, + Description: "abcg", + }, + SubMessage{ + Id: 5106, + Description: "abcgg", + }, + }, + IsTrue: &isTrue, + Payload: []byte(`"123"`), + SQLNullString: &sql.NullString{ + String: "test", + Valid: true, + }, +} - input := `{ +var jsonData = `{ "id": 1022, "name": "name acc", "price": 13.3, @@ -27,183 +67,61 @@ func TestMessage_Unmarshal(t *testing.T) { ], "subMessageX": { "id": 102, - "description": "abcd" + "description": "abcd", + "startDate": "0001-01-01 00:00:00" }, "messagesX": [ { "id": 2102, - "description": "abce" + "description": "abce", + "startDate": "0001-01-01 00:00:00" } ], "SubMessageY": { "id": 3102, - "description": "abcf" + "description": "abcf", + "startDate": "0001-01-01 00:00:00" }, "MessagesY": [ { "id": 5102, - "description": "abcg" - }, + "description": "abcg", + "startDate": "0001-01-01 00:00:00" + }, { "id": 5106, - "description": "abcgg" + "description": "abcgg", + "startDate": "0001-01-01 00:00:00" } ], "enabled": true, - "data": "123" + "data": "123", + "sqlNullString": "test" }` - expected := `{ - "Id": 1022, - "Name": "name acc", - "Price": 13.3, - "Ints": [ - 1, - 2, - 5 - ], - "Floats": [ - 2.3, - 4.6, - 7.4 - ], - "SubMessageX": { - "Id": 102, - "Description": "abcd" - }, - "MessagesX": [ - { - "Id": 2102, - "Description": "abce" - } - ], - "SubMessageY": { - "Id": 3102, - "Description": "abcf" - }, - "MessagesY": [ - { - "Id": 5102, - "Description": "abcg" - }, - { - "Id": 5106, - "Description": "abcgg" - } - ], - "IsTrue": true, - "Payload": "\"123\"" -}` +func TestMessage_Unmarshal(t *testing.T) { var err error - var data = []byte(input) + var data = []byte(jsonData) message := &Message{} err = gojay.UnmarshalJSONObject(data, message) if !assert.Nil(t, err) { log.Fatal(err) } - assertly.AssertValues(t, expected, message) + require.Equal( + t, + msg, + message, + ) } func TestMessage_Marshal(t *testing.T) { - - input := `{ - "id": 1022, - "name": "name acc", - "price": 13.3, - "ints": [ - 1, - 2, - 5 - ], - "floats": [ - 2.3, - 4.6, - 7.4 - ], - "subMessageX": { - "id": 102, - "description": "abcd" - }, - "messagesX": [ - { - "id": 2102, - "description": "abce" - } - ], - "SubMessageY": { - "id": 3102, - "description": "abcf" - }, - "MessagesY": [ - { - "id": 5102, - "description": "abcg" - }, - { - "id": 5106, - "description": "abcgg" - } - ], - "enabled": true, - "data": "123" -}` - - expected := `{ - "Id": 1022, - "Name": "name acc", - "Price": 13.3, - "Ints": [ - 1, - 2, - 5 - ], - "Floats": [ - 2.3, - 4.6, - 7.4 - ], - "SubMessageX": { - "Id": 102, - "Description": "abcd" - }, - "MessagesX": [ - { - "Id": 2102, - "Description": "abce" - } - ], - "SubMessageY": { - "Id": 3102, - "Description": "abcf" - }, - "MessagesY": [ - { - "Id": 5102, - "Description": "abcg" - }, - { - "Id": 5106, - "Description": "abcgg" - } - ], - "IsTrue": true, - "Payload": "\"123\"" -}` - var err error - var data = []byte(input) - message := &Message{} - err = gojay.UnmarshalJSONObject(data, message) - if !assert.Nil(t, err) { - log.Fatal(err) - } - assertly.AssertValues(t, expected, message) var writer = new(bytes.Buffer) encoder := gojay.NewEncoder(writer) - err = encoder.Encode(message) + err = encoder.Encode(msg) assert.Nil(t, err) var JSON = writer.String() - assertly.AssertValues(t, input, JSON) + require.JSONEq(t, jsonData, JSON) } diff --git a/gojay/codegen/test/annotated_struct/message.go b/gojay/codegen/test/annotated_struct/message.go @@ -1,18 +1,21 @@ package annotated_struct -type Paylod []byte +import "database/sql" + +type Payload []byte type Message struct { - Id int `json:"id"` - Name string `json:"name"` - Price float64 `json:"price"` - Ints []int `json:"ints"` - Floats []*float32 `json:"floats"` - SubMessageX *SubMessage `json:"subMessageX"` - MessagesX []*SubMessage `json:"messagesX"` - SubMessageY SubMessage - MessagesY []SubMessage - IsTrue *bool `json:"enabled"` - Payload Paylod `json:"data"` - Ignore string `json:"-"` + Id int `json:"id"` + Name string `json:"name"` + Price float64 `json:"price"` + Ints []int `json:"ints"` + Floats []float32 `json:"floats"` + SubMessageX *SubMessage `json:"subMessageX"` + MessagesX []*SubMessage `json:"messagesX"` + SubMessageY SubMessage + MessagesY []SubMessage + IsTrue *bool `json:"enabled"` + Payload Payload `json:"data"` + Ignore string `json:"-"` + SQLNullString *sql.NullString `json:"sqlNullString"` } diff --git a/gojay/codegen/test/basic_struct/encoding.go b/gojay/codegen/test/basic_struct/encoding.go @@ -1,99 +1,101 @@ -// Code generated by GoJayGen. DO NOT EDIT.\n\n +// Code generated by Gojay. DO NOT EDIT. + package basic_struct import ( + "database/sql" "github.com/francoispqt/gojay" "time" ) -type Ints []int +type SubMessagesPtr []*SubMessage -// UnmarshalJSONArray decodes JSON array elements into slice -func (a *Ints) UnmarshalJSONArray(dec *gojay.Decoder) error { - var value int - if err := dec.Int(&value); err != nil { +func (s *SubMessagesPtr) UnmarshalJSONArray(dec *gojay.Decoder) error { + var value = &SubMessage{} + if err := dec.Object(value); err != nil { return err } - *a = append(*a, value) + *s = append(*s, value) return nil } -// MarshalJSONArray encodes arrays into JSON -func (a Ints) MarshalJSONArray(enc *gojay.Encoder) { - for _, item := range a { - enc.Int(item) +func (s SubMessagesPtr) MarshalJSONArray(enc *gojay.Encoder) { + for i := range s { + enc.Object(s[i]) } } -// IsNil checks if array is nil -func (a Ints) IsNil() bool { - return len(a) == 0 +func (s SubMessagesPtr) IsNil() bool { + return len(s) == 0 } -type Float32sPtr []*float32 +type SubMessages []SubMessage -// UnmarshalJSONArray decodes JSON array elements into slice -func (a *Float32sPtr) UnmarshalJSONArray(dec *gojay.Decoder) error { - var value float32 - if err := dec.Float32(&value); err != nil { +func (s *SubMessages) UnmarshalJSONArray(dec *gojay.Decoder) error { + var value = SubMessage{} + if err := dec.Object(&value); err != nil { return err } - *a = append(*a, &value) + *s = append(*s, value) return nil } -// MarshalJSONArray encodes arrays into JSON -func (a Float32sPtr) MarshalJSONArray(enc *gojay.Encoder) { - for _, item := range a { - enc.Float32(*item) +func (s SubMessages) MarshalJSONArray(enc *gojay.Encoder) { + for i := range s { + enc.Object(&s[i]) } } -// IsNil checks if array is nil -func (a Float32sPtr) IsNil() bool { - return len(a) == 0 +func (s SubMessages) IsNil() bool { + return len(s) == 0 } -type SubMessagesPtr []*SubMessage +type Ints []int -func (s *SubMessagesPtr) UnmarshalJSONArray(dec *gojay.Decoder) error { - var value = &SubMessage{} - if err := dec.Object(value); err != nil { +// UnmarshalJSONArray decodes JSON array elements into slice +func (a *Ints) UnmarshalJSONArray(dec *gojay.Decoder) error { + var value int + if err := dec.Int(&value); err != nil { return err } - *s = append(*s, value) + *a = append(*a, value) return nil } -func (s SubMessagesPtr) MarshalJSONArray(enc *gojay.Encoder) { - for i := range s { - enc.Object(s[i]) +// MarshalJSONArray encodes arrays into JSON +func (a Ints) MarshalJSONArray(enc *gojay.Encoder) { + for _, item := range a { + enc.Int(item) } } -func (s SubMessagesPtr) IsNil() bool { - return len(s) == 0 +// IsNil checks if array is nil +func (a Ints) IsNil() bool { + return len(a) == 0 } -type SubMessages []SubMessage +type Float32s []float32 -func (s *SubMessages) UnmarshalJSONArray(dec *gojay.Decoder) error { - var value = SubMessage{} - if err := dec.Object(&value); err != nil { +// UnmarshalJSONArray decodes JSON array elements into slice +func (a *Float32s) UnmarshalJSONArray(dec *gojay.Decoder) error { + var value float32 + if err := dec.Float32(&value); err != nil { return err } - *s = append(*s, value) + *a = append(*a, value) return nil } -func (s SubMessages) MarshalJSONArray(enc *gojay.Encoder) { - for i := range s { - enc.Object(&s[i]) +// MarshalJSONArray encodes arrays into JSON +func (a Float32s) MarshalJSONArray(enc *gojay.Encoder) { + for _, item := range a { + enc.Float32(item) } } -func (s SubMessages) IsNil() bool { - return len(s) == 0 +// IsNil checks if array is nil +func (a Float32s) IsNil() bool { + return len(a) == 0 } // MarshalJSONObject implements MarshalerJSONObject @@ -153,7 +155,7 @@ func (m *Message) MarshalJSONObject(enc *gojay.Encoder) { enc.Float64Key("Price", m.Price) var intsSlice = Ints(m.Ints) enc.ArrayKey("Ints", intsSlice) - var floatsSlice = Float32sPtr(m.Floats) + var floatsSlice = Float32s(m.Floats) enc.ArrayKey("Floats", floatsSlice) enc.ObjectKey("SubMessageX", m.SubMessageX) var messagesXSlice = SubMessagesPtr(m.MessagesX) @@ -164,6 +166,9 @@ func (m *Message) MarshalJSONObject(enc *gojay.Encoder) { enc.BoolKey("IsTrue", *m.IsTrue) var payloadSlice = gojay.EmbeddedJSON(m.Payload) enc.AddEmbeddedJSONKey("Payload", &payloadSlice) + if m.SQLNullString != nil { + enc.SQLNullStringKey("SQLNullString", m.SQLNullString) + } } // IsNil checks if instance is nil @@ -193,10 +198,10 @@ func (m *Message) UnmarshalJSONObject(dec *gojay.Decoder, k string) error { return err case "Floats": - var aSlice = Float32sPtr{} + var aSlice = Float32s{} err := dec.Array(&aSlice) if err == nil && len(aSlice) > 0 { - m.Floats = []*float32(aSlice) + m.Floats = []float32(aSlice) } return err @@ -246,9 +251,17 @@ func (m *Message) UnmarshalJSONObject(dec *gojay.Decoder, k string) error { } return err + case "SQLNullString": + var value = &sql.NullString{} + err := dec.SQLNullString(value) + if err == nil { + m.SQLNullString = value + } + return err + } return nil } // NKeys returns the number of keys to unmarshal -func (m *Message) NKeys() int { return 11 } +func (m *Message) NKeys() int { return 12 } diff --git a/gojay/codegen/test/basic_struct/encoding_test.go b/gojay/codegen/test/basic_struct/encoding_test.go @@ -2,15 +2,53 @@ package basic_struct import ( "bytes" - "github.com/francoispqt/gojay" - "github.com/stretchr/testify/assert" - "github.com/viant/assertly" + "database/sql" "testing" + + "github.com/francoispqt/gojay" + "github.com/stretchr/testify/require" ) -func TestMessage_Unmarshal(t *testing.T) { +var isTrue = true +var msg = &Message{ + Id: 1022, + Name: "name acc", + Price: 13.3, + Ints: []int{1, 2, 5}, + Floats: []float32{2.3, 4.6, 7.4}, + SubMessageX: &SubMessage{ + Id: 102, + Description: "abcd", + }, + MessagesX: []*SubMessage{ + &SubMessage{ + Id: 2102, + Description: "abce", + }, + }, + SubMessageY: SubMessage{ + Id: 3102, + Description: "abcf", + }, + MessagesY: []SubMessage{ + SubMessage{ + Id: 5102, + Description: "abcg", + }, + SubMessage{ + Id: 5106, + Description: "abcgg", + }, + }, + IsTrue: &isTrue, + Payload: []byte(`"123"`), + SQLNullString: &sql.NullString{ + String: "test", + Valid: true, + }, +} - input := `{ +var jsonData = `{ "Id": 1022, "Name": "name acc", "Price": 13.3, @@ -26,95 +64,55 @@ func TestMessage_Unmarshal(t *testing.T) { ], "SubMessageX": { "Id": 102, - "Description": "abcd" + "Description": "abcd", + "StartTime": "0001-01-01T00:00:00Z" }, "MessagesX": [ { "Id": 2102, - "Description": "abce" - } + "Description": "abce", + "StartTime": "0001-01-01T00:00:00Z" + } ], "SubMessageY": { "Id": 3102, - "Description": "abcf" + "Description": "abcf", + "StartTime": "0001-01-01T00:00:00Z" }, "MessagesY": [ { "Id": 5102, - "Description": "abcg" - }, + "Description": "abcg", + "StartTime": "0001-01-01T00:00:00Z" + }, { "Id": 5106, - "Description": "abcgg" + "Description": "abcgg", + "StartTime": "0001-01-01T00:00:00Z" } ], "IsTrue": true, - "Payload": "" + "Payload": "123", + "SQLNullString": "test" }` +func TestMessage_Unmarshal(t *testing.T) { var err error - var data = []byte(input) + var data = []byte(jsonData) message := &Message{} err = gojay.UnmarshalJSONObject(data, message) - assert.Nil(t, err) - assertly.AssertValues(t, input, message) + require.Nil(t, err) + require.Equal(t, msg, message) } func TestMessage_Marshal(t *testing.T) { - - input := `{ - "Id": 1022, - "Name": "name acc", - "Price": 13.3, - "Ints": [ - 1, - 2, - 5 - ], - "Floats": [ - 2.3, - 4.6, - 7.4 - ], - "SubMessageX": { - "Id": 102, - "Description": "abcd" - }, - "MessagesX": [ - { - "Id": 2102, - "Description": "abce" - } - ], - "SubMessageY": { - "Id": 3102, - "Description": "abcf" - }, - "MessagesY": [ - { - "Id": 5102, - "Description": "abcg" - }, - { - "Id": 5106, - "Description": "abcgg" - } - ], - "IsTrue": true, - "Payload": "" -}` - - var err error - var data = []byte(input) - message := &Message{} - err = gojay.UnmarshalJSONObject(data, message) - assert.Nil(t, err) - var writer = new(bytes.Buffer) encoder := gojay.NewEncoder(writer) - err = encoder.Encode(message) - assert.Nil(t, err) + var err = encoder.Encode(msg) + + require.Nil(t, err) var JSON = writer.String() - assertly.AssertValues(t, input, JSON) + + require.JSONEq(t, jsonData, JSON) } diff --git a/gojay/codegen/test/basic_struct/message.go b/gojay/codegen/test/basic_struct/message.go @@ -1,15 +1,18 @@ package basic_struct +import "database/sql" + type Message struct { - Id int - Name string - Price float64 - Ints []int - Floats []*float32 - SubMessageX *SubMessage - MessagesX []*SubMessage - SubMessageY SubMessage - MessagesY []SubMessage - IsTrue *bool - Payload []byte + Id int + Name string + Price float64 + Ints []int + Floats []float32 + SubMessageX *SubMessage + MessagesX []*SubMessage + SubMessageY SubMessage + MessagesY []SubMessage + IsTrue *bool + Payload []byte + SQLNullString *sql.NullString } diff --git a/gojay/codegen/test/embedded_struct/encoding.go b/gojay/codegen/test/embedded_struct/encoding.go @@ -1,4 +1,5 @@ -// Code generated by GoJayGen. DO NOT EDIT.\n\n +// Code generated by Gojay. DO NOT EDIT. + package embedded_struct import ( @@ -30,27 +31,27 @@ func (a Ints) IsNil() bool { return len(a) == 0 } -type Float32sPtr []*float32 +type Float64s []float64 // UnmarshalJSONArray decodes JSON array elements into slice -func (a *Float32sPtr) UnmarshalJSONArray(dec *gojay.Decoder) error { - var value float32 - if err := dec.Float32(&value); err != nil { +func (a *Float64s) UnmarshalJSONArray(dec *gojay.Decoder) error { + var value float64 + if err := dec.Float64(&value); err != nil { return err } - *a = append(*a, &value) + *a = append(*a, value) return nil } // MarshalJSONArray encodes arrays into JSON -func (a Float32sPtr) MarshalJSONArray(enc *gojay.Encoder) { +func (a Float64s) MarshalJSONArray(enc *gojay.Encoder) { for _, item := range a { - enc.Float32(*item) + enc.Float64(item) } } // IsNil checks if array is nil -func (a Float32sPtr) IsNil() bool { +func (a Float64s) IsNil() bool { return len(a) == 0 } @@ -97,34 +98,6 @@ func (s SubMessages) IsNil() bool { } // MarshalJSONObject implements MarshalerJSONObject -func (i *BaseId) MarshalJSONObject(enc *gojay.Encoder) { - enc.IntKey("Id", i.Id) - enc.StringKey("Name", i.Name) -} - -// IsNil checks if instance is nil -func (i *BaseId) IsNil() bool { - return i == nil -} - -// UnmarshalJSONObject implements gojay's UnmarshalerJSONObject -func (i *BaseId) UnmarshalJSONObject(dec *gojay.Decoder, k string) error { - - switch k { - case "Id": - return dec.Int(&i.Id) - - case "Name": - return dec.String(&i.Name) - - } - return nil -} - -// NKeys returns the number of keys to unmarshal -func (i *BaseId) NKeys() int { return 2 } - -// MarshalJSONObject implements MarshalerJSONObject func (m *SubMessage) MarshalJSONObject(enc *gojay.Encoder) { enc.StringKey("Description", m.Description) enc.TimeKey("StartTime", &m.StartTime, time.RFC3339) @@ -184,7 +157,7 @@ func (m *Message) MarshalJSONObject(enc *gojay.Encoder) { enc.Float64Key("Price", m.Price) var intsSlice = Ints(m.Ints) enc.ArrayKey("Ints", intsSlice) - var floatsSlice = Float32sPtr(m.Floats) + var floatsSlice = Float64s(m.Floats) enc.ArrayKey("Floats", floatsSlice) enc.ObjectKey("SubMessageX", m.SubMessageX) var messagesXSlice = SubMessagesPtr(m.MessagesX) @@ -247,10 +220,10 @@ func (m *Message) UnmarshalJSONObject(dec *gojay.Decoder, k string) error { return err case "Floats": - var aSlice = Float32sPtr{} + var aSlice = Float64s{} err := dec.Array(&aSlice) if err == nil && len(aSlice) > 0 { - m.Floats = []*float32(aSlice) + m.Floats = []float64(aSlice) } return err @@ -306,3 +279,31 @@ func (m *Message) UnmarshalJSONObject(dec *gojay.Decoder, k string) error { // NKeys returns the number of keys to unmarshal func (m *Message) NKeys() int { return 14 } + +// MarshalJSONObject implements MarshalerJSONObject +func (i *BaseId) MarshalJSONObject(enc *gojay.Encoder) { + enc.IntKey("Id", i.Id) + enc.StringKey("Name", i.Name) +} + +// IsNil checks if instance is nil +func (i *BaseId) IsNil() bool { + return i == nil +} + +// UnmarshalJSONObject implements gojay's UnmarshalerJSONObject +func (i *BaseId) UnmarshalJSONObject(dec *gojay.Decoder, k string) error { + + switch k { + case "Id": + return dec.Int(&i.Id) + + case "Name": + return dec.String(&i.Name) + + } + return nil +} + +// NKeys returns the number of keys to unmarshal +func (i *BaseId) NKeys() int { return 2 } diff --git a/gojay/codegen/test/embedded_struct/message.go b/gojay/codegen/test/embedded_struct/message.go @@ -10,7 +10,7 @@ type Message struct { SubMessage Price float64 Ints []int - Floats []*float32 + Floats []float64 SubMessageX *SubMessage MessagesX []*SubMessage SubMessageY SubMessage diff --git a/gojay/codegen/test/pooled_struct/encoding.go b/gojay/codegen/test/pooled_struct/encoding.go @@ -1,4 +1,5 @@ -// Code generated by GoJayGen. DO NOT EDIT.\n\n +// Code generated by Gojay. DO NOT EDIT. + package pooled_struct import ( @@ -8,14 +9,14 @@ import ( ) func init() { - SubMessagePool = &sync.Pool{ + MessagePool = &sync.Pool{ New: func() interface{} { - return &SubMessage{} + return &Message{} }, } - MessagePool = &sync.Pool{ + SubMessagePool = &sync.Pool{ New: func() interface{} { - return &Message{} + return &SubMessage{} }, } } @@ -47,27 +48,27 @@ func (a Ints) IsNil() bool { return len(a) == 0 } -type Float32sPtr []*float32 +type Float64s []float64 // UnmarshalJSONArray decodes JSON array elements into slice -func (a *Float32sPtr) UnmarshalJSONArray(dec *gojay.Decoder) error { - var value float32 - if err := dec.Float32(&value); err != nil { +func (a *Float64s) UnmarshalJSONArray(dec *gojay.Decoder) error { + var value float64 + if err := dec.Float64(&value); err != nil { return err } - *a = append(*a, &value) + *a = append(*a, value) return nil } // MarshalJSONArray encodes arrays into JSON -func (a Float32sPtr) MarshalJSONArray(enc *gojay.Encoder) { +func (a Float64s) MarshalJSONArray(enc *gojay.Encoder) { for _, item := range a { - enc.Float32(*item) + enc.Float64(item) } } // IsNil checks if array is nil -func (a Float32sPtr) IsNil() bool { +func (a Float64s) IsNil() bool { return len(a) == 0 } @@ -177,7 +178,7 @@ func (m *Message) MarshalJSONObject(enc *gojay.Encoder) { enc.Float64Key("Price", m.Price) var intsSlice = Ints(m.Ints) enc.ArrayKey("Ints", intsSlice) - var floatsSlice = Float32sPtr(m.Floats) + var floatsSlice = Float64s(m.Floats) enc.ArrayKey("Floats", floatsSlice) enc.ObjectKey("SubMessageX", m.SubMessageX) var messagesXSlice = SubMessagesPtr(m.MessagesX) @@ -217,10 +218,10 @@ func (m *Message) UnmarshalJSONObject(dec *gojay.Decoder, k string) error { return err case "Floats": - var aSlice = Float32sPtr{} + var aSlice = Float64s{} err := dec.Array(&aSlice) if err == nil && len(aSlice) > 0 { - m.Floats = []*float32(aSlice) + m.Floats = []float64(aSlice) } return err diff --git a/gojay/codegen/test/pooled_struct/message.go b/gojay/codegen/test/pooled_struct/message.go @@ -5,7 +5,7 @@ type Message struct { Name string Price float64 Ints []int - Floats []*float32 + Floats []float64 SubMessageX *SubMessage MessagesX []*SubMessage SubMessageY SubMessage diff --git a/gojay/gojay.go b/gojay/gojay.go @@ -0,0 +1,23 @@ +package main + +import ( + "flag" + "github.com/francoispqt/gojay/gojay/codegen" + "log" +) + +var pkg = flag.String("pkg", "", "the package name of the generated file") +var dst = flag.String("o", "", "destination file to output generated code") +var src = flag.String("s", "", "source dir or file (absolute or relative path)") +var types = flag.String("t", "", "types to generate") +var annotation = flag.String("a", "json", "annotation tag (default json)") +var poolObjects = flag.String("p", "", "generate code to reuse objects using sync.Pool") + +func main() { + flag.Parse() + options := codegen.NewOptionsWithFlagSet(flag.CommandLine) + gen := codegen.NewGenerator(options) + if err := gen.Generate(); err != nil { + log.Fatal(err) + } +} diff --git a/gojay/gojaygen.go b/gojay/gojaygen.go @@ -1,23 +0,0 @@ -package main - -import ( - "flag" - "github.com/francoispqt/gojay/gojay/codegen" - "log" -) - -var pkg = flag.String("pkg", "", "the package name of the generated file") -var dst = flag.String("o", "", "destination file to output generated implementations") -var src = flag.String("s", "", "source dir or file (absolute or relative path)") -var types = flag.String("t", "", "types to generate") -var annotation = flag.String("a", "json", "annotation tag (default json)") -var poolObjects = flag.String("p", "", "generate code to reuse objects using sync.Pool") - -func main() { - flag.Parse() - options := codegen.NewOptionsWithFlagSet(flag.CommandLine) - gen := codegen.NewGenerator(options) - if err := gen.Generate(); err != nil { - log.Fatal(err) - } -}