Added pragma easyjson:skip to exclude structs from generating stage

It allows manually control which struct should be skipped when using option `-all`.
This commit is contained in:
Pavel Parshin
2020-05-10 13:34:43 +03:00
parent f0a000e7a8
commit e25e66fa75
4 changed files with 23 additions and 4 deletions
+9 -1
View File
@@ -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:
+7 -1
View File
@@ -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
}
+2 -2
View File
@@ -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) {
+5
View File
@@ -23,6 +23,11 @@ type (
}
)
//easyjson:skip
type TypeSkipped struct {
Value string
}
var (
myGenDeclaredValue = TypeDeclared{Value: "GenDeclared"}
myGenDeclaredString = `{"Value":"GenDeclared"}`