mirror of
https://github.com/netbirdio/easyjson.git
synced 2026-05-22 18:44:42 -07:00
Merge branch 'master' into default_gopath
This commit is contained in:
+1
-1
@@ -6,4 +6,4 @@ install:
|
||||
- go get github.com/ugorji/go/codec
|
||||
- go get github.com/pquerna/ffjson/fflib/v1
|
||||
- go get github.com/json-iterator/go
|
||||
- go get github.com/golang/lint/golint
|
||||
- go get golang.org/x/lint/golint
|
||||
|
||||
@@ -25,7 +25,8 @@ generate: root build
|
||||
.root/src/$(PKG)/tests/nothing.go \
|
||||
.root/src/$(PKG)/tests/named_type.go \
|
||||
.root/src/$(PKG)/tests/custom_map_key_type.go \
|
||||
.root/src/$(PKG)/tests/embedded_type.go
|
||||
.root/src/$(PKG)/tests/embedded_type.go \
|
||||
.root/src/$(PKG)/tests/reference_to_pointer.go
|
||||
|
||||
.root/bin/easyjson -all .root/src/$(PKG)/tests/data.go
|
||||
.root/bin/easyjson -all .root/src/$(PKG)/tests/nothing.go
|
||||
@@ -37,6 +38,7 @@ generate: root build
|
||||
.root/bin/easyjson .root/src/$(PKG)/tests/named_type.go
|
||||
.root/bin/easyjson .root/src/$(PKG)/tests/custom_map_key_type.go
|
||||
.root/bin/easyjson .root/src/$(PKG)/tests/embedded_type.go
|
||||
.root/bin/easyjson .root/src/$(PKG)/tests/reference_to_pointer.go
|
||||
.root/bin/easyjson -disallow_unknown_fields .root/src/$(PKG)/tests/disallow_unknown.go
|
||||
|
||||
test: generate root
|
||||
|
||||
@@ -36,7 +36,7 @@ type Generator struct {
|
||||
NoFormat bool
|
||||
}
|
||||
|
||||
// writeStub outputs an initial stubs for marshalers/unmarshalers so that the package
|
||||
// writeStub outputs an initial stub for marshalers/unmarshalers so that the package
|
||||
// using marshalers/unmarshales compiles correctly for boostrapping code.
|
||||
func (g *Generator) writeStub() error {
|
||||
f, err := os.Create(g.OutName)
|
||||
|
||||
+10
-3
@@ -198,7 +198,12 @@ func (g *Generator) genTypeDecoderNoCheck(t reflect.Type, out string, tags field
|
||||
dec := g.getDecoderName(t)
|
||||
g.addType(t)
|
||||
|
||||
fmt.Fprintln(g.out, ws+dec+"(in, &"+out+")")
|
||||
if len(out) > 0 && out[0] == '*' {
|
||||
// NOTE: In order to remove an extra reference to a pointer
|
||||
fmt.Fprintln(g.out, ws+dec+"(in, "+out[1:]+")")
|
||||
} else {
|
||||
fmt.Fprintln(g.out, ws+dec+"(in, &"+out+")")
|
||||
}
|
||||
|
||||
case reflect.Ptr:
|
||||
fmt.Fprintln(g.out, ws+"if in.IsNull() {")
|
||||
@@ -343,7 +348,8 @@ func getStructFields(t reflect.Type) ([]reflect.StructField, error) {
|
||||
var efields []reflect.StructField
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
f := t.Field(i)
|
||||
if !f.Anonymous {
|
||||
tags := parseFieldTags(f)
|
||||
if !f.Anonymous || tags.name != "" {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -362,7 +368,8 @@ func getStructFields(t reflect.Type) ([]reflect.StructField, error) {
|
||||
var fields []reflect.StructField
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
f := t.Field(i)
|
||||
if f.Anonymous {
|
||||
tags := parseFieldTags(f)
|
||||
if f.Anonymous && tags.name == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
+4
-3
@@ -427,9 +427,10 @@ func lowerFirst(s string) string {
|
||||
for i := range s {
|
||||
ch := s[i]
|
||||
if isUpper(ch) {
|
||||
if i == 0 {
|
||||
switch {
|
||||
case i == 0:
|
||||
str += string(ch + 32)
|
||||
} else if !foundLower { // Currently just a stream of capitals, eg JSONRESTS[erver]
|
||||
case !foundLower: // Currently just a stream of capitals, eg JSONRESTS[erver]
|
||||
if strlen > (i+1) && isLower(s[i+1]) {
|
||||
// Next char is lower, keep this a capital
|
||||
str += string(ch)
|
||||
@@ -437,7 +438,7 @@ func lowerFirst(s string) string {
|
||||
// Either at end of string or next char is capital
|
||||
str += string(ch + 32)
|
||||
}
|
||||
} else {
|
||||
default:
|
||||
str += string(ch)
|
||||
}
|
||||
} else {
|
||||
|
||||
+4
-3
@@ -521,11 +521,12 @@ func (r *Lexer) SkipRecursive() {
|
||||
r.scanToken()
|
||||
var start, end byte
|
||||
|
||||
if r.token.delimValue == '{' {
|
||||
switch r.token.delimValue {
|
||||
case '{':
|
||||
start, end = '{', '}'
|
||||
} else if r.token.delimValue == '[' {
|
||||
case '[':
|
||||
start, end = '[', ']'
|
||||
} else {
|
||||
default:
|
||||
r.consume()
|
||||
return
|
||||
}
|
||||
|
||||
+13
-5
@@ -10,6 +10,7 @@ import (
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"go/build"
|
||||
)
|
||||
|
||||
@@ -35,9 +36,12 @@ func getPkgPath(fname string, isDir bool) (string, error) {
|
||||
return getPkgPathFromGOPATH(fname, isDir)
|
||||
}
|
||||
|
||||
var (
|
||||
goModPathCache = make(map[string]string)
|
||||
)
|
||||
var goModPathCache = struct {
|
||||
paths map[string]string
|
||||
sync.RWMutex
|
||||
}{
|
||||
paths: make(map[string]string),
|
||||
}
|
||||
|
||||
// empty if no go.mod, GO111MODULE=off or go without go modules support
|
||||
func goModPath(fname string, isDir bool) (string, error) {
|
||||
@@ -46,13 +50,17 @@ func goModPath(fname string, isDir bool) (string, error) {
|
||||
root = filepath.Dir(fname)
|
||||
}
|
||||
|
||||
goModPath, ok := goModPathCache[root]
|
||||
goModPathCache.RLock()
|
||||
goModPath, ok := goModPathCache.paths[root]
|
||||
goModPathCache.RUnlock()
|
||||
if ok {
|
||||
return goModPath, nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
goModPathCache[root] = goModPath
|
||||
goModPathCache.Lock()
|
||||
goModPathCache.paths[root] = goModPath
|
||||
goModPathCache.Unlock()
|
||||
}()
|
||||
|
||||
cmd := exec.Command("go", "env", "GOMOD")
|
||||
|
||||
@@ -6,19 +6,25 @@ type EmbeddedType struct {
|
||||
Inner struct {
|
||||
EmbeddedInnerType
|
||||
}
|
||||
Field2 int
|
||||
Field2 int
|
||||
EmbeddedInnerType2 `json:"named"`
|
||||
}
|
||||
|
||||
type EmbeddedInnerType struct {
|
||||
Field1 int
|
||||
}
|
||||
|
||||
type EmbeddedInnerType2 struct {
|
||||
Field3 int
|
||||
}
|
||||
|
||||
var embeddedTypeValue EmbeddedType
|
||||
|
||||
func init() {
|
||||
embeddedTypeValue.Field1 = 1
|
||||
embeddedTypeValue.Field2 = 2
|
||||
embeddedTypeValue.Inner.Field1 = 3
|
||||
embeddedTypeValue.Field3 = 4
|
||||
}
|
||||
|
||||
var embeddedTypeValueString = `{"Inner":{"Field1":3},"Field2":2,"Field1":1}`
|
||||
var embeddedTypeValueString = `{"Inner":{"Field1":3},"Field2":2,"named":{"Field3":4},"Field1":1}`
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package tests
|
||||
|
||||
type Struct1 struct {
|
||||
}
|
||||
|
||||
//easyjson:json
|
||||
type Struct2 struct {
|
||||
From *Struct1 `json:"from,omitempty"`
|
||||
Through *Struct1 `json:"through,omitempty"`
|
||||
}
|
||||
Reference in New Issue
Block a user