From e25e66fa75d55ee635651fd6a5d36cb4706ee18d Mon Sep 17 00:00:00 2001
From: Pavel Parshin
Date: Sun, 10 May 2020 13:34:43 +0300
Subject: [PATCH 1/2] Added pragma easyjson:skip to exclude structs from
generating stage
It allows manually control which struct should be skipped when using option `-all`.
---
README.md | 10 +++++++++-
parser/parser.go | 8 +++++++-
tests/basic_test.go | 4 ++--
tests/type_declaration.go | 5 +++++
4 files changed, 23 insertions(+), 4 deletions(-)
diff --git a/README.md b/README.md
index 7153930..2f4287a 100644
--- a/README.md
+++ b/README.md
@@ -62,7 +62,15 @@ Usage of easyjson:
```
Using `-all` will generate marshalers/unmarshalers for all Go structs in the
-file. If `-all` is not provided, then only those structs whose preceding
+file excluding those structs whose preceding comment starts with `easyjson:skip`.
+For example:
+
+```go
+//easyjson:skip
+type A struct {}
+```
+
+If `-all` is not provided, then only those structs whose preceding
comment starts with `easyjson:json` will have marshalers/unmarshalers
generated. For example:
diff --git a/parser/parser.go b/parser/parser.go
index 13875cc..09dbbd6 100644
--- a/parser/parser.go
+++ b/parser/parser.go
@@ -8,7 +8,10 @@ import (
"strings"
)
-const structComment = "easyjson:json"
+const (
+ structComment = "easyjson:json"
+ structSkipComment = "easyjson:skip"
+)
type Parser struct {
PkgPath string
@@ -25,6 +28,9 @@ type visitor struct {
func (p *Parser) needType(comments string) bool {
for _, v := range strings.Split(comments, "\n") {
+ if strings.HasPrefix(v, structSkipComment) {
+ return false
+ }
if strings.HasPrefix(v, structComment) {
return true
}
diff --git a/tests/basic_test.go b/tests/basic_test.go
index fe635ff..e2d6ad4 100644
--- a/tests/basic_test.go
+++ b/tests/basic_test.go
@@ -2,12 +2,11 @@ package tests
import (
"bytes"
+ "encoding/json"
"net/http/httptest"
"reflect"
"testing"
- "encoding/json"
-
"github.com/mailru/easyjson"
"github.com/mailru/easyjson/jwriter"
)
@@ -253,6 +252,7 @@ func TestDisallowUnknown(t *testing.T) {
var testNotGeneratedTypeCases = []interface{}{
TypeNotDeclared{},
+ TypeSkipped{},
}
func TestMethodsNoGenerated(t *testing.T) {
diff --git a/tests/type_declaration.go b/tests/type_declaration.go
index e638455..84553a3 100644
--- a/tests/type_declaration.go
+++ b/tests/type_declaration.go
@@ -23,6 +23,11 @@ type (
}
)
+//easyjson:skip
+type TypeSkipped struct {
+ Value string
+}
+
var (
myGenDeclaredValue = TypeDeclared{Value: "GenDeclared"}
myGenDeclaredString = `{"Value":"GenDeclared"}`
From 0c9f71dfd2b4ecfe6615aa4fee55a51b77026948 Mon Sep 17 00:00:00 2001
From: Pavel Parshin
Date: Sun, 10 May 2020 14:08:06 +0300
Subject: [PATCH 2/2] Fixed skip detection and added correct test
---
Makefile | 2 ++
parser/parser.go | 28 +++++++++++++++-------------
tests/basic_test.go | 1 +
tests/type_declaration.go | 5 -----
tests/type_declaration_skip.go | 15 +++++++++++++++
5 files changed, 33 insertions(+), 18 deletions(-)
create mode 100644 tests/type_declaration_skip.go
diff --git a/Makefile b/Makefile
index 85de543..ec37cc4 100644
--- a/Makefile
+++ b/Makefile
@@ -21,6 +21,7 @@ generate: build
./tests/html.go \
./tests/unknown_fields.go \
./tests/type_declaration.go \
+ ./tests/type_declaration_skip.go \
./tests/members_escaped.go \
./tests/members_unescaped.go \
./tests/intern.go \
@@ -43,6 +44,7 @@ generate: build
bin/easyjson -disallow_unknown_fields ./tests/disallow_unknown.go
bin/easyjson ./tests/unknown_fields.go
bin/easyjson ./tests/type_declaration.go
+ bin/easyjson -all ./tests/type_declaration_skip.go
bin/easyjson ./tests/members_escaped.go
bin/easyjson -disable_members_unescape ./tests/members_unescaped.go
bin/easyjson ./tests/intern.go
diff --git a/parser/parser.go b/parser/parser.go
index 09dbbd6..1d96026 100644
--- a/parser/parser.go
+++ b/parser/parser.go
@@ -26,16 +26,16 @@ type visitor struct {
name string
}
-func (p *Parser) needType(comments string) bool {
+func (p *Parser) needType(comments string) (skip, explicit bool) {
for _, v := range strings.Split(comments, "\n") {
if strings.HasPrefix(v, structSkipComment) {
- return false
+ return true, false
}
if strings.HasPrefix(v, structComment) {
- return true
+ return false, true
}
}
- return false
+ return false, false
}
func (v *visitor) Visit(n ast.Node) (w ast.Visitor) {
@@ -47,21 +47,23 @@ func (v *visitor) Visit(n ast.Node) (w ast.Visitor) {
return v
case *ast.GenDecl:
- explicit := v.needType(n.Doc.Text())
- if !explicit {
- return v
- }
+ skip, explicit := v.needType(n.Doc.Text())
- for _, nc := range n.Specs {
- switch nct := nc.(type) {
- case *ast.TypeSpec:
- nct.Doc = n.Doc
+ if skip || explicit {
+ for _, nc := range n.Specs {
+ switch nct := nc.(type) {
+ case *ast.TypeSpec:
+ nct.Doc = n.Doc
+ }
}
}
return v
case *ast.TypeSpec:
- explicit := v.needType(n.Doc.Text())
+ skip, explicit := v.needType(n.Doc.Text())
+ if skip {
+ return nil
+ }
if !explicit && !v.AllStructs {
return nil
}
diff --git a/tests/basic_test.go b/tests/basic_test.go
index e2d6ad4..11013c8 100644
--- a/tests/basic_test.go
+++ b/tests/basic_test.go
@@ -57,6 +57,7 @@ var testCases = []struct {
{&myGenDeclaredValue, myGenDeclaredString},
{&myGenDeclaredWithCommentValue, myGenDeclaredWithCommentString},
{&myTypeDeclaredValue, myTypeDeclaredString},
+ {&myTypeNotSkippedValue, myTypeNotSkippedString},
{&intern, internString},
}
diff --git a/tests/type_declaration.go b/tests/type_declaration.go
index 84553a3..e638455 100644
--- a/tests/type_declaration.go
+++ b/tests/type_declaration.go
@@ -23,11 +23,6 @@ type (
}
)
-//easyjson:skip
-type TypeSkipped struct {
- Value string
-}
-
var (
myGenDeclaredValue = TypeDeclared{Value: "GenDeclared"}
myGenDeclaredString = `{"Value":"GenDeclared"}`
diff --git a/tests/type_declaration_skip.go b/tests/type_declaration_skip.go
new file mode 100644
index 0000000..5400795
--- /dev/null
+++ b/tests/type_declaration_skip.go
@@ -0,0 +1,15 @@
+package tests
+
+//easyjson:skip
+type TypeSkipped struct {
+ Value string
+}
+
+type TypeNotSkipped struct {
+ Value string
+}
+
+var (
+ myTypeNotSkippedValue = TypeDeclared{Value: "TypeNotSkipped"}
+ myTypeNotSkippedString = `{"Value":"TypeNotSkipped"}`
+)