gojay

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

commit c60a101913909d4969bb17bf7da824f2e197317d
parent 57fd99d065deccaa976ff5ccc4bc03e121cd8a08
Author: francoispqt <francois@parquet.ninja>
Date:   Sat, 26 Jan 2019 00:54:26 +0800

update codegen tool add support for time.Time and SQLNull slices

Diffstat:
Rgojay/codegen/README.md -> gojay/README.md | 0
Mgojay/codegen/field.go | 4+++-
Mgojay/codegen/generator.go | 28++++++++++++++++++++++++++++
Mgojay/codegen/helper.go | 5+++--
Mgojay/codegen/struct.go | 92+++++++++++++++++++++++++++++++++++++++++++++++++------------------------------
Mgojay/codegen/template.go | 49+++++++++++++++++++++++++++++++++++++++++++++++--
Mgojay/codegen/test/annotated_struct/encoding.go | 27+++++++++++++++------------
Mgojay/codegen/test/annotated_struct/encoding_test.go | 8++++----
Mgojay/codegen/test/basic_struct/encoding.go | 23+++++++++++++----------
Mgojay/codegen/test/embedded_struct/encoding.go | 110++++++++++++++++++++++++++++++++++++++++++-------------------------------------
Mgojay/codegen/test/pooled_struct/encoding.go | 129++++++++++++++++++++++++++++++++++++++++---------------------------------------
11 files changed, 294 insertions(+), 181 deletions(-)

diff --git a/gojay/codegen/README.md b/gojay/README.md diff --git a/gojay/codegen/field.go b/gojay/codegen/field.go @@ -42,6 +42,8 @@ type Field struct { IsAnonymous bool IsPointer bool IsSlice bool + + GojayMethod string } //NewField returns a new field @@ -95,7 +97,7 @@ func NewField(owner *Struct, field *toolbox.FieldInfo, fieldType *toolbox.TypeIn } if owner.options.PoolObjects { - if field.IsPointer && !strings.HasSuffix(field.TypeName, ".Time") { + if field.IsPointer && !strings.HasSuffix(field.TypeName, ".Time") && !strings.Contains(field.TypeName, "sql.Null") { poolName := getPoolName(field.TypeName) result.Init = fmt.Sprintf(`%v.Get().(*%v)`, poolName, field.TypeName) } diff --git a/gojay/codegen/generator.go b/gojay/codegen/generator.go @@ -94,6 +94,8 @@ func (g *Generator) writeCode() error { return err } + fmt.Println(string(expandedCode)) + code, err := format.Source([]byte(expandedCode)) if err != nil { @@ -134,6 +136,32 @@ func (g *Generator) generateObjectArray(field *Field) error { return err } +func (g *Generator) generateTimeArray(field *Field) error { + if _, ok := g.sliceTypes[field.RawComponentType]; ok { + return nil + } + + code, err := expandBlockTemplate(timeSlice, field) + if err != nil { + return err + } + g.sliceTypes[field.RawComponentType] = code + return err +} + +func (g *Generator) generateTypedArray(field *Field) error { + if _, ok := g.sliceTypes[field.RawComponentType]; ok { + return nil + } + + code, err := expandBlockTemplate(typeSlice, field) + if err != nil { + return err + } + g.sliceTypes[field.RawComponentType] = code + return err +} + func (g *Generator) generatePool(structType string) error { if !g.options.PoolObjects { return nil diff --git a/gojay/codegen/helper.go b/gojay/codegen/helper.go @@ -43,11 +43,12 @@ func getSliceHelperTypeName(typeName string, isPointer bool) string { if typeName == "" { return "" } + var pluralName = firstLetterToUppercase(typeName) + "s" if isPointer { pluralName += "Ptr" } - return pluralName + return strings.Replace(pluralName, ".", "", -1) } func isSkipable(options *Options, field *toolbox.FieldInfo) bool { @@ -70,7 +71,7 @@ func wrapperIfNeeded(text, wrappingChar string) string { func getPoolName(typeName string) string { typeName = strings.Replace(typeName, "*", "", 1) - return typeName + "Pool" + return strings.Replace(typeName+"Pool", ".", "", -1) } func getJSONKey(options *Options, field *toolbox.FieldInfo) string { diff --git a/gojay/codegen/struct.go b/gojay/codegen/struct.go @@ -2,8 +2,9 @@ package codegen import ( "fmt" - "github.com/viant/toolbox" "strings" + + "github.com/viant/toolbox" ) type Struct struct { @@ -178,29 +179,20 @@ func (s *Struct) generateFieldDecoding(fields []*toolbox.FieldInfo) (string, []s break main } else if field.IsSlice { - templateKey = decodeStructSlice - if err = s.generateObjectArray(field); err != nil { - return "", nil, err - } - // if is time.Time - } else if strings.HasSuffix(field.Type, "time.Time") { - templateKey = decodeTime - s.addImport("time") - // if is sql.NullXxx - } else if strings.Contains(field.Type, "sql.Null") { - templateKey = decodeSQLNull - s.addImport("database/sql") - if strings.Contains(field.Type, "Bool") { - field.NullType = "Bool" - } else if strings.Contains(field.Type, "Float64") { - field.NullType = "Float64" - } else if strings.Contains(field.Type, "Int64") { - field.NullType = "Int64" - } else if strings.Contains(field.Type, "String") { - field.NullType = "String" + if f, _, ok := s.typedFieldDecode(field, field.ComponentType); ok { + templateKey = decodeStructSlice + if err = f(field); err != nil { + return "", nil, err + } } else { - templateKey = decodeUnknown + templateKey = decodeStructSlice + if err = s.generateObjectArray(field); err != nil { + return "", nil, err + } } + // if is time.Time + } else if _, k, ok := s.typedFieldDecode(field, field.Type); ok { + templateKey = k } else { templateKey = decodeUnknown } @@ -283,20 +275,10 @@ func (s *Struct) generateFieldEncoding(fields []*toolbox.FieldInfo) ([]string, e templateKey = encodeStructSlice break main } else if field.IsSlice { + s.typedFieldDecode(field, field.ComponentType) templateKey = encodeStructSlice - } else if strings.HasSuffix(field.Type, "time.Time") { - templateKey = encodeTime - } else if strings.Contains(field.Type, "sql.Null") { - templateKey = encodeSQLNull - if strings.Contains(field.Type, "Bool") { - field.NullType = "Bool" - } else if strings.Contains(field.Type, "Float64") { - field.NullType = "Float64" - } else if strings.Contains(field.Type, "Int64") { - field.NullType = "Int64" - } else if strings.Contains(field.Type, "String") { - field.NullType = "String" - } + } else if _, k, ok := s.typedFieldEncode(field, field.Type); ok { + templateKey = k } else { templateKey = encodeUnknown } @@ -313,6 +295,46 @@ func (s *Struct) generateFieldEncoding(fields []*toolbox.FieldInfo) ([]string, e return fieldCases, nil } +var sqlNullTypes = []string{ + "Bool", + "Float64", + "Int64", + "String", + "Time", +} + +func (s *Struct) typedFieldEncode(field *Field, typeName string) (func(*Field) error, int, bool) { + if strings.Contains(typeName, "time.Time") { + return s.generateTimeArray, encodeTime, true + } else if strings.Contains(typeName, "sql.Null") { + for _, nullType := range sqlNullTypes { + if strings.Contains(typeName, nullType) { + field.NullType = nullType + field.GojayMethod = "SQLNull" + nullType + } + } + return s.generateTypedArray, encodeSQLNull, true + } + return nil, 0, false +} + +func (s *Struct) typedFieldDecode(field *Field, typeName string) (func(*Field) error, int, bool) { + if strings.Contains(typeName, "time.Time") { + s.addImport("time") + return s.generateTimeArray, decodeTime, true + } else if strings.Contains(typeName, "sql.Null") { + for _, nullType := range sqlNullTypes { + if strings.Contains(typeName, nullType) { + field.NullType = nullType + field.GojayMethod = "SQLNull" + nullType + } + } + s.addImport("database/sql") + return s.generateTypedArray, decodeSQLNull, true + } + return nil, 0, false +} + func NewStruct(info *toolbox.TypeInfo, generator *Generator) *Struct { return &Struct{ TypeInfo: info, diff --git a/gojay/codegen/template.go b/gojay/codegen/template.go @@ -94,7 +94,7 @@ var fieldTemplate = map[int]string{ decodeTime: ` case "{{.Key}}": var format = {{.TimeLayout}} var value = {{.Init}} - err := dec.DecodeTime({{.PointerModifier}}value, format) + err := dec.Time({{.PointerModifier}}value, format) if err == nil { {{.Mutator}} = value } @@ -106,7 +106,7 @@ var fieldTemplate = map[int]string{ }{{else}} enc.TimeKey("{{.Key}}", {{.PointerModifier}}{{.Accessor}}, {{.TimeLayout}}){{end}}`, decodeSQLNull: ` case "{{.Key}}": var value = {{.Init}} - err := dec.DecodeSQLNull{{.NullType}}({{.PointerModifier}}value) + err := dec.SQLNull{{.NullType}}({{.PointerModifier}}value) if err == nil { {{.Mutator}} = value } @@ -144,6 +144,8 @@ const ( poolVar poolInit embeddedStructInit + timeSlice + typeSlice ) var blockTemplate = map[int]string{ @@ -235,7 +237,50 @@ func (s {{.HelperType}}) IsNil() bool { return len(s) == 0 } `, + typeSlice: ` +type {{.HelperType}} {{.RawType}} + +func (s *{{.HelperType}}) UnmarshalJSONArray(dec *gojay.Decoder) error { + var value = {{.ComponentInit}} + if err := dec.{{.GojayMethod}}({{.ComponentPointerModifier}}value); err != nil { + return err + } + *s = append(*s, value) + return nil +} +func (s {{.HelperType}}) MarshalJSONArray(enc *gojay.Encoder) { + for i := range s { + enc.{{.GojayMethod}}({{.ComponentPointerModifier}}s[i]) + } +} + +func (s {{.HelperType}}) IsNil() bool { + return len(s) == 0 +} +`, + timeSlice: ` +type {{.HelperType}} {{.RawType}} + +func (s *{{.HelperType}}) UnmarshalJSONArray(dec *gojay.Decoder) error { + var value = {{.ComponentInit}} + if err := dec.Time({{.ComponentPointerModifier}}value, {{.TimeLayout}}); err != nil { + return err + } + *s = append(*s, value) + return nil +} + +func (s {{.HelperType}}) MarshalJSONArray(enc *gojay.Encoder) { + for i := range s { + enc.Time({{.ComponentPointerModifier}}s[i], {{.TimeLayout}}) + } +} + +func (s {{.HelperType}}) IsNil() bool { + return len(s) == 0 +} +`, resetStruct: ` // Reset reset fields func ({{.Receiver}}) Reset() { diff --git a/gojay/codegen/test/annotated_struct/encoding.go b/gojay/codegen/test/annotated_struct/encoding.go @@ -8,7 +8,7 @@ import ( type Ints []int -//UnmarshalJSONArray decodes JSON array elements into slice +// 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 { @@ -18,21 +18,21 @@ func (a *Ints) UnmarshalJSONArray(dec *gojay.Decoder) error { return nil } -//MarshalJSONArray encodes arrays into JSON +// MarshalJSONArray encodes arrays into JSON func (a Ints) MarshalJSONArray(enc *gojay.Encoder) { for _, item := range a { enc.Int(item) } } -//IsNil checks if array is nil +// IsNil checks if array is nil func (a Ints) IsNil() bool { return len(a) == 0 } type Float32sPtr []*float32 -//UnmarshalJSONArray decodes JSON array elements into slice +// 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 { @@ -42,14 +42,14 @@ func (a *Float32sPtr) UnmarshalJSONArray(dec *gojay.Decoder) error { return nil } -//MarshalJSONArray encodes arrays into JSON +// MarshalJSONArray encodes arrays into JSON func (a Float32sPtr) MarshalJSONArray(enc *gojay.Encoder) { for _, item := range a { enc.Float32(*item) } } -//IsNil checks if array is nil +// IsNil checks if array is nil func (a Float32sPtr) IsNil() bool { return len(a) == 0 } @@ -96,7 +96,7 @@ func (s SubMessages) IsNil() bool { return len(s) == 0 } -//MarshalJSONObject implements MarshalerJSONObject +// MarshalJSONObject implements MarshalerJSONObject func (m *SubMessage) MarshalJSONObject(enc *gojay.Encoder) { enc.IntKey("id", m.Id) enc.StringKey("description", m.Description) @@ -106,7 +106,7 @@ func (m *SubMessage) MarshalJSONObject(enc *gojay.Encoder) { } } -//IsNil checks if instance is nil +// IsNil checks if instance is nil func (m *SubMessage) IsNil() bool { return m == nil } @@ -125,6 +125,9 @@ func (m *SubMessage) UnmarshalJSONObject(dec *gojay.Decoder, k string) error { var format = "2006-01-02 15:04:05" var value = time.Time{} err := dec.DecodeTime(&value, format) + if err == nil { + m.StartTime = value + } return err case "endDate": @@ -143,12 +146,12 @@ func (m *SubMessage) UnmarshalJSONObject(dec *gojay.Decoder, k string) error { // NKeys returns the number of keys to unmarshal func (m *SubMessage) NKeys() int { return 4 } -//MarshalJSONObject implements MarshalerJSONObject +// MarshalJSONObject implements MarshalerJSONObject func (p *Paylod) MarshalJSONObject(enc *gojay.Encoder) { } -//IsNil checks if instance is nil +// IsNil checks if instance is nil func (p *Paylod) IsNil() bool { return p == nil } @@ -165,7 +168,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 } -//MarshalJSONObject implements MarshalerJSONObject +// MarshalJSONObject implements MarshalerJSONObject func (m *Message) MarshalJSONObject(enc *gojay.Encoder) { enc.IntKey("id", m.Id) enc.StringKey("name", m.Name) @@ -185,7 +188,7 @@ func (m *Message) MarshalJSONObject(enc *gojay.Encoder) { enc.AddEmbeddedJSONKey("data", &payloadSlice) } -//IsNil checks if instance is nil +// IsNil checks if instance is nil func (m *Message) IsNil() bool { return m == nil } diff --git a/gojay/codegen/test/annotated_struct/encoding_test.go b/gojay/codegen/test/annotated_struct/encoding_test.go @@ -53,7 +53,7 @@ func TestMessage_Unmarshal(t *testing.T) { "data": "123" }` - expacted := `{ + expected := `{ "Id": 1022, "Name": "name acc", "Price": 13.3, @@ -102,7 +102,7 @@ func TestMessage_Unmarshal(t *testing.T) { if !assert.Nil(t, err) { log.Fatal(err) } - assertly.AssertValues(t, expacted, message) + assertly.AssertValues(t, expected, message) } func TestMessage_Marshal(t *testing.T) { @@ -149,7 +149,7 @@ func TestMessage_Marshal(t *testing.T) { "data": "123" }` - expacted := `{ + expected := `{ "Id": 1022, "Name": "name acc", "Price": 13.3, @@ -198,7 +198,7 @@ func TestMessage_Marshal(t *testing.T) { if !assert.Nil(t, err) { log.Fatal(err) } - assertly.AssertValues(t, expacted, message) + assertly.AssertValues(t, expected, message) var writer = new(bytes.Buffer) encoder := gojay.NewEncoder(writer) err = encoder.Encode(message) diff --git a/gojay/codegen/test/basic_struct/encoding.go b/gojay/codegen/test/basic_struct/encoding.go @@ -8,7 +8,7 @@ import ( type Ints []int -//UnmarshalJSONArray decodes JSON array elements into slice +// 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 { @@ -18,21 +18,21 @@ func (a *Ints) UnmarshalJSONArray(dec *gojay.Decoder) error { return nil } -//MarshalJSONArray encodes arrays into JSON +// MarshalJSONArray encodes arrays into JSON func (a Ints) MarshalJSONArray(enc *gojay.Encoder) { for _, item := range a { enc.Int(item) } } -//IsNil checks if array is nil +// IsNil checks if array is nil func (a Ints) IsNil() bool { return len(a) == 0 } type Float32sPtr []*float32 -//UnmarshalJSONArray decodes JSON array elements into slice +// 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 { @@ -42,14 +42,14 @@ func (a *Float32sPtr) UnmarshalJSONArray(dec *gojay.Decoder) error { return nil } -//MarshalJSONArray encodes arrays into JSON +// MarshalJSONArray encodes arrays into JSON func (a Float32sPtr) MarshalJSONArray(enc *gojay.Encoder) { for _, item := range a { enc.Float32(*item) } } -//IsNil checks if array is nil +// IsNil checks if array is nil func (a Float32sPtr) IsNil() bool { return len(a) == 0 } @@ -96,7 +96,7 @@ func (s SubMessages) IsNil() bool { return len(s) == 0 } -//MarshalJSONObject implements MarshalerJSONObject +// MarshalJSONObject implements MarshalerJSONObject func (m *SubMessage) MarshalJSONObject(enc *gojay.Encoder) { enc.IntKey("Id", m.Id) enc.StringKey("Description", m.Description) @@ -106,7 +106,7 @@ func (m *SubMessage) MarshalJSONObject(enc *gojay.Encoder) { } } -//IsNil checks if instance is nil +// IsNil checks if instance is nil func (m *SubMessage) IsNil() bool { return m == nil } @@ -125,6 +125,9 @@ func (m *SubMessage) UnmarshalJSONObject(dec *gojay.Decoder, k string) error { var format = time.RFC3339 var value = time.Time{} err := dec.DecodeTime(&value, format) + if err == nil { + m.StartTime = value + } return err case "EndTime": @@ -143,7 +146,7 @@ func (m *SubMessage) UnmarshalJSONObject(dec *gojay.Decoder, k string) error { // NKeys returns the number of keys to unmarshal func (m *SubMessage) NKeys() int { return 4 } -//MarshalJSONObject implements MarshalerJSONObject +// MarshalJSONObject implements MarshalerJSONObject func (m *Message) MarshalJSONObject(enc *gojay.Encoder) { enc.IntKey("Id", m.Id) enc.StringKey("Name", m.Name) @@ -163,7 +166,7 @@ func (m *Message) MarshalJSONObject(enc *gojay.Encoder) { enc.AddEmbeddedJSONKey("Payload", &payloadSlice) } -//IsNil checks if instance is nil +// IsNil checks if instance is nil func (m *Message) IsNil() bool { return m == nil } diff --git a/gojay/codegen/test/embedded_struct/encoding.go b/gojay/codegen/test/embedded_struct/encoding.go @@ -6,27 +6,6 @@ import ( "time" ) -type SubMessagesPtr []*SubMessage - -func (s *SubMessagesPtr) 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 SubMessagesPtr) MarshalJSONArray(enc *gojay.Encoder) { - for i := range s { - enc.Object(s[i]) - } -} - -func (s SubMessagesPtr) IsNil() bool { - return len(s) == 0 -} - type SubMessages []SubMessage func (s *SubMessages) UnmarshalJSONArray(dec *gojay.Decoder) error { @@ -50,7 +29,7 @@ func (s SubMessages) IsNil() bool { type Ints []int -//UnmarshalJSONArray decodes JSON array elements into slice +// 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 { @@ -60,21 +39,21 @@ func (a *Ints) UnmarshalJSONArray(dec *gojay.Decoder) error { return nil } -//MarshalJSONArray encodes arrays into JSON +// MarshalJSONArray encodes arrays into JSON func (a Ints) MarshalJSONArray(enc *gojay.Encoder) { for _, item := range a { enc.Int(item) } } -//IsNil checks if array is nil +// IsNil checks if array is nil func (a Ints) IsNil() bool { return len(a) == 0 } type Float32sPtr []*float32 -//UnmarshalJSONArray decodes JSON array elements into slice +// 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 { @@ -84,47 +63,40 @@ func (a *Float32sPtr) UnmarshalJSONArray(dec *gojay.Decoder) error { return nil } -//MarshalJSONArray encodes arrays into JSON +// MarshalJSONArray encodes arrays into JSON func (a Float32sPtr) MarshalJSONArray(enc *gojay.Encoder) { for _, item := range a { enc.Float32(*item) } } -//IsNil checks if array is nil +// IsNil checks if array is nil func (a Float32sPtr) IsNil() bool { return len(a) == 0 } -//MarshalJSONObject implements MarshalerJSONObject -func (i *BaseId) MarshalJSONObject(enc *gojay.Encoder) { - enc.IntKey("Id", i.Id) - enc.StringKey("Name", i.Name) -} +type SubMessagesPtr []*SubMessage -//IsNil checks if instance is nil -func (i *BaseId) IsNil() bool { - return i == nil +func (s *SubMessagesPtr) UnmarshalJSONArray(dec *gojay.Decoder) error { + var value = &SubMessage{} + if err := dec.Object(value); err != nil { + return err + } + *s = append(*s, value) + return 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) - +func (s SubMessagesPtr) MarshalJSONArray(enc *gojay.Encoder) { + for i := range s { + enc.Object(s[i]) } - return nil } -// NKeys returns the number of keys to unmarshal -func (i *BaseId) NKeys() int { return 2 } +func (s SubMessagesPtr) IsNil() bool { + return len(s) == 0 +} -//MarshalJSONObject implements MarshalerJSONObject +// MarshalJSONObject implements MarshalerJSONObject func (m *SubMessage) MarshalJSONObject(enc *gojay.Encoder) { enc.StringKey("Description", m.Description) enc.TimeKey("StartTime", &m.StartTime, time.RFC3339) @@ -133,7 +105,7 @@ func (m *SubMessage) MarshalJSONObject(enc *gojay.Encoder) { } } -//IsNil checks if instance is nil +// IsNil checks if instance is nil func (m *SubMessage) IsNil() bool { return m == nil } @@ -149,6 +121,9 @@ func (m *SubMessage) UnmarshalJSONObject(dec *gojay.Decoder, k string) error { var format = time.RFC3339 var value = time.Time{} err := dec.DecodeTime(&value, format) + if err == nil { + m.StartTime = value + } return err case "EndTime": @@ -167,7 +142,7 @@ func (m *SubMessage) UnmarshalJSONObject(dec *gojay.Decoder, k string) error { // NKeys returns the number of keys to unmarshal func (m *SubMessage) NKeys() int { return 3 } -//MarshalJSONObject implements MarshalerJSONObject +// MarshalJSONObject implements MarshalerJSONObject func (m *Message) MarshalJSONObject(enc *gojay.Encoder) { if m.BaseId != nil { enc.IntKey("Id", m.Id) @@ -194,7 +169,7 @@ func (m *Message) MarshalJSONObject(enc *gojay.Encoder) { enc.AddEmbeddedJSONKey("Payload", &payloadSlice) } -//IsNil checks if instance is nil +// IsNil checks if instance is nil func (m *Message) IsNil() bool { return m == nil } @@ -218,6 +193,9 @@ func (m *Message) UnmarshalJSONObject(dec *gojay.Decoder, k string) error { var format = time.RFC3339 var value = time.Time{} err := dec.DecodeTime(&value, format) + if err == nil { + m.StartTime = value + } return err case "EndTime": @@ -300,3 +278,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/pooled_struct/encoding.go b/gojay/codegen/test/pooled_struct/encoding.go @@ -20,12 +20,12 @@ func init() { } } -var MessagePool *sync.Pool var SubMessagePool *sync.Pool +var MessagePool *sync.Pool type Ints []int -//UnmarshalJSONArray decodes JSON array elements into slice +// 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 { @@ -35,21 +35,21 @@ func (a *Ints) UnmarshalJSONArray(dec *gojay.Decoder) error { return nil } -//MarshalJSONArray encodes arrays into JSON +// MarshalJSONArray encodes arrays into JSON func (a Ints) MarshalJSONArray(enc *gojay.Encoder) { for _, item := range a { enc.Int(item) } } -//IsNil checks if array is nil +// IsNil checks if array is nil func (a Ints) IsNil() bool { return len(a) == 0 } type Float32sPtr []*float32 -//UnmarshalJSONArray decodes JSON array elements into slice +// 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 { @@ -59,14 +59,14 @@ func (a *Float32sPtr) UnmarshalJSONArray(dec *gojay.Decoder) error { return nil } -//MarshalJSONArray encodes arrays into JSON +// MarshalJSONArray encodes arrays into JSON func (a Float32sPtr) MarshalJSONArray(enc *gojay.Encoder) { for _, item := range a { enc.Float32(*item) } } -//IsNil checks if array is nil +// IsNil checks if array is nil func (a Float32sPtr) IsNil() bool { return len(a) == 0 } @@ -113,61 +113,7 @@ func (s SubMessages) 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("StartTime", &m.StartTime, time.RFC3339) - if m.EndTime != nil { - enc.TimeKey("EndTime", m.EndTime, time.RFC3339) - } -} - -//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 "StartTime": - var format = time.RFC3339 - var value = time.Time{} - err := dec.DecodeTime(&value, format) - return err - - case "EndTime": - var format = time.RFC3339 - var value = &time.Time{} - err := dec.DecodeTime(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 } - -// Reset reset fields -func (m *SubMessage) Reset() { - m.Id = 0 - m.Description = "" - m.EndTime = nil -} - -//MarshalJSONObject implements MarshalerJSONObject +// MarshalJSONObject implements MarshalerJSONObject func (m *Message) MarshalJSONObject(enc *gojay.Encoder) { enc.IntKey("Id", m.Id) enc.StringKey("Name", m.Name) @@ -187,7 +133,7 @@ func (m *Message) MarshalJSONObject(enc *gojay.Encoder) { enc.AddEmbeddedJSONKey("Payload", &payloadSlice) } -//IsNil checks if instance is nil +// IsNil checks if instance is nil func (m *Message) IsNil() bool { return m == nil } @@ -296,3 +242,60 @@ func (m *Message) Reset() { m.IsTrue = nil m.Payload = nil } + +// MarshalJSONObject implements MarshalerJSONObject +func (m *SubMessage) MarshalJSONObject(enc *gojay.Encoder) { + enc.IntKey("Id", m.Id) + enc.StringKey("Description", m.Description) + enc.TimeKey("StartTime", &m.StartTime, time.RFC3339) + if m.EndTime != nil { + enc.TimeKey("EndTime", m.EndTime, time.RFC3339) + } +} + +// 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 "StartTime": + var format = time.RFC3339 + var value = time.Time{} + err := dec.DecodeTime(&value, format) + if err == nil { + m.StartTime = value + } + return err + + case "EndTime": + var format = time.RFC3339 + var value = &time.Time{} + err := dec.DecodeTime(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 } + +// Reset reset fields +func (m *SubMessage) Reset() { + m.Id = 0 + m.Description = "" + m.EndTime = nil +}