Merge pull request #215 from boekkooi-fresh/patch/typespec-docs

Fix TypeSpec docs being ignored
This commit is contained in:
GoWebProd
2020-03-30 23:29:59 +03:00
committed by GitHub
4 changed files with 76 additions and 7 deletions
+2
View File
@@ -20,6 +20,7 @@ generate: build
./tests/reference_to_pointer.go \
./tests/html.go \
./tests/unknown_fields.go \
./tests/type_declaration.go
bin/easyjson -all ./tests/data.go
bin/easyjson -all ./tests/nothing.go
@@ -36,6 +37,7 @@ generate: build
bin/easyjson ./tests/key_marshaler_map.go
bin/easyjson -disallow_unknown_fields ./tests/disallow_unknown.go
bin/easyjson ./tests/unknown_fields.go
bin/easyjson ./tests/type_declaration.go
test: generate
go test \
+19 -7
View File
@@ -22,8 +22,7 @@ type Parser struct {
type visitor struct {
*Parser
name string
explicit bool
name string
}
func (p *Parser) needType(comments string) bool {
@@ -44,20 +43,33 @@ func (v *visitor) Visit(n ast.Node) (w ast.Visitor) {
return v
case *ast.GenDecl:
v.explicit = v.needType(n.Doc.Text())
if !v.explicit && !v.AllStructs {
return nil
explicit := v.needType(n.Doc.Text())
if !explicit {
return v
}
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())
if !explicit && !v.AllStructs {
return nil
}
v.name = n.Name.String()
// Allow to specify non-structs explicitly independent of '-all' flag.
if v.explicit {
if explicit {
v.StructNames = append(v.StructNames, v.name)
return nil
}
return v
case *ast.StructType:
v.StructNames = append(v.StructNames, v.name)
+22
View File
@@ -53,6 +53,9 @@ var testCases = []struct {
{&myUInt8SliceValue, myUInt8SliceString},
{&myUInt8ArrayValue, myUInt8ArrayString},
{&mapWithEncodingMarshaler, mapWithEncodingMarshalerString},
{&myGenDeclaredValue, myGenDeclaredString},
{&myGenDeclaredWithCommentValue, myGenDeclaredWithCommentString},
{&myTypeDeclaredValue, myTypeDeclaredString},
}
func TestMarshal(t *testing.T) {
@@ -243,3 +246,22 @@ func TestDisallowUnknown(t *testing.T) {
t.Error("want error, got nil")
}
}
var testNotGeneratedTypeCases = []interface{}{
TypeNotDeclared{},
}
func TestMethodsNoGenerated(t *testing.T) {
var ok bool
for i, instance := range testNotGeneratedTypeCases {
_, ok = instance.(json.Marshaler)
if ok {
t.Errorf("[%d, %T] Unexpected MarshalJSON()", i, instance)
}
_, ok = instance.(json.Unmarshaler)
if ok {
t.Errorf("[%d, %T] Unexpected Unmarshaler()", i, instance)
}
}
}
+33
View File
@@ -0,0 +1,33 @@
package tests
//easyjson:json
type (
GenDeclared1 struct {
Value string
}
// A gen declared easyjson struct with a comment
GenDeclaredWithComment struct {
Value string
}
)
type (
//easyjson:json
TypeDeclared struct {
Value string
}
TypeNotDeclared struct {
Value string
}
)
var (
myGenDeclaredValue = TypeDeclared{Value: "GenDeclared"}
myGenDeclaredString = `{"Value":"GenDeclared"}`
myGenDeclaredWithCommentValue = TypeDeclared{Value: "GenDeclaredWithComment"}
myGenDeclaredWithCommentString = `{"Value":"GenDeclaredWithComment"}`
myTypeDeclaredValue = TypeDeclared{Value: "TypeDeclared"}
myTypeDeclaredString = `{"Value":"TypeDeclared"}`
)