From e25e66fa75d55ee635651fd6a5d36cb4706ee18d Mon Sep 17 00:00:00 2001
From: Pavel Parshin
Date: Sun, 10 May 2020 13:34:43 +0300
Subject: [PATCH] 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"}`