mirror of
https://github.com/netbirdio/easyjson.git
synced 2026-05-22 18:44:42 -07:00
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:
@@ -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
@@ -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
@@ -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) {
|
||||
|
||||
@@ -23,6 +23,11 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
//easyjson:skip
|
||||
type TypeSkipped struct {
|
||||
Value string
|
||||
}
|
||||
|
||||
var (
|
||||
myGenDeclaredValue = TypeDeclared{Value: "GenDeclared"}
|
||||
myGenDeclaredString = `{"Value":"GenDeclared"}`
|
||||
|
||||
Reference in New Issue
Block a user