diff --git a/Makefile b/Makefile index 80449f0..ce30363 100644 --- a/Makefile +++ b/Makefile @@ -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 \ diff --git a/parser/parser.go b/parser/parser.go index 6f7cc22..1aa42d7 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -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) diff --git a/tests/basic_test.go b/tests/basic_test.go index 938add0..004d278 100644 --- a/tests/basic_test.go +++ b/tests/basic_test.go @@ -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) + } + } +} diff --git a/tests/type_declaration.go b/tests/type_declaration.go new file mode 100644 index 0000000..e638455 --- /dev/null +++ b/tests/type_declaration.go @@ -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"}` +)