From f60ef3823047b5739dad0a0658b3f868d4a06d2f Mon Sep 17 00:00:00 2001 From: Kenneth Shaw Date: Tue, 18 Dec 2018 09:33:22 -0700 Subject: [PATCH 1/6] Fixes a concurrency issue with package path cache The `chromedp-gen` tool, which makes use of the raw code, would occassionally encounter a concurrent map write (on slow systems). This adds a simple `sync.RWMutex` to the map causing problems. --- parser/pkgpath.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/parser/pkgpath.go b/parser/pkgpath.go index 489b392..155d168 100644 --- a/parser/pkgpath.go +++ b/parser/pkgpath.go @@ -10,6 +10,7 @@ import ( "path/filepath" "strconv" "strings" + "sync" ) func getPkgPath(fname string, isDir bool) (string, error) { @@ -34,9 +35,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) { @@ -45,13 +49,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") From 0e998db2a90f37511fc79aeb428b6744826a0448 Mon Sep 17 00:00:00 2001 From: Phil Pearl Date: Thu, 10 Jan 2019 18:43:09 +0000 Subject: [PATCH 2/6] Stdlib json handles embedded structs with json tags differently. See https://play.golang.org/p/KXZbjG0JBle for an example --- gen/decoder.go | 6 ++++-- tests/embedded_type.go | 10 ++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/gen/decoder.go b/gen/decoder.go index 606602f..49a020d 100644 --- a/gen/decoder.go +++ b/gen/decoder.go @@ -343,7 +343,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 +363,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 } diff --git a/tests/embedded_type.go b/tests/embedded_type.go index 66470b6..346cf4a 100644 --- a/tests/embedded_type.go +++ b/tests/embedded_type.go @@ -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}` From 4dd12460d3c3df033deb1429466e7f0e60d24b06 Mon Sep 17 00:00:00 2001 From: Phil Pearl Date: Thu, 10 Jan 2019 18:56:11 +0000 Subject: [PATCH 3/6] Fix golint issue --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 884f8bb..7789141 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 From f9215f98e179b999033a1f69d8a1a2b00e64af8e Mon Sep 17 00:00:00 2001 From: Sayan Nandan <17377258+sntdevco@users.noreply.github.com> Date: Wed, 20 Feb 2019 17:47:27 +0530 Subject: [PATCH 4/6] Fix a little typo --- bootstrap/bootstrap.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bootstrap/bootstrap.go b/bootstrap/bootstrap.go index 1116a91..a461bf1 100644 --- a/bootstrap/bootstrap.go +++ b/bootstrap/bootstrap.go @@ -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) From b27fccced4e8e517111d0b298b43c68e526e9d6b Mon Sep 17 00:00:00 2001 From: Aleksandr Petrukhin Date: Wed, 20 Feb 2019 21:52:23 +0000 Subject: [PATCH 5/6] [decoder] remove unnecessary reference to a pointer --- Makefile | 4 +++- gen/decoder.go | 7 ++++++- tests/reference_to_pointer.go | 10 ++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 tests/reference_to_pointer.go diff --git a/Makefile b/Makefile index 7cfec87..18c6687 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/gen/decoder.go b/gen/decoder.go index 606602f..d89dd31 100644 --- a/gen/decoder.go +++ b/gen/decoder.go @@ -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() {") diff --git a/tests/reference_to_pointer.go b/tests/reference_to_pointer.go new file mode 100644 index 0000000..6768e53 --- /dev/null +++ b/tests/reference_to_pointer.go @@ -0,0 +1,10 @@ +package tests + +type Struct1 struct { +} + +//easyjson:json +type Struct2 struct { + From *Struct1 `json:"from,omitempty"` + Through *Struct1 `json:"through,omitempty"` +} From aced9b46ed7ad110246024d229b76327e9169de4 Mon Sep 17 00:00:00 2001 From: Kirill Motkov Date: Tue, 12 Mar 2019 16:16:26 +0300 Subject: [PATCH 6/6] Rewrite some if-else-if-else chains as a switch --- gen/generator.go | 7 ++++--- jlexer/lexer.go | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/gen/generator.go b/gen/generator.go index a34a852..13c54c4 100644 --- a/gen/generator.go +++ b/gen/generator.go @@ -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 { diff --git a/jlexer/lexer.go b/jlexer/lexer.go index 51f0566..ef64075 100644 --- a/jlexer/lexer.go +++ b/jlexer/lexer.go @@ -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 }