From d3fc79bc1b6b3d5016fa356accc48a345f1ef2d8 Mon Sep 17 00:00:00 2001 From: warnar boekkooi Date: Thu, 28 Mar 2019 15:25:01 +0100 Subject: [PATCH 1/3] Show TypeSpec docs are ignored --- Makefile | 4 +++- tests/basic_test.go | 22 ++++++++++++++++++++++ tests/type_declaration.go | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 tests/type_declaration.go diff --git a/Makefile b/Makefile index 18c6687..6bf5fad 100644 --- a/Makefile +++ b/Makefile @@ -26,7 +26,8 @@ generate: root build .root/src/$(PKG)/tests/named_type.go \ .root/src/$(PKG)/tests/custom_map_key_type.go \ .root/src/$(PKG)/tests/embedded_type.go \ - .root/src/$(PKG)/tests/reference_to_pointer.go + .root/src/$(PKG)/tests/reference_to_pointer.go \ + .root/src/$(PKG)/tests/type_declaration.go .root/bin/easyjson -all .root/src/$(PKG)/tests/data.go .root/bin/easyjson -all .root/src/$(PKG)/tests/nothing.go @@ -40,6 +41,7 @@ generate: root build .root/bin/easyjson .root/src/$(PKG)/tests/embedded_type.go .root/bin/easyjson .root/src/$(PKG)/tests/reference_to_pointer.go .root/bin/easyjson -disallow_unknown_fields .root/src/$(PKG)/tests/disallow_unknown.go + .root/bin/easyjson .root/src/$(PKG)/tests/type_declaration.go test: generate root go test \ diff --git a/tests/basic_test.go b/tests/basic_test.go index 3b1cc65..958c0a8 100644 --- a/tests/basic_test.go +++ b/tests/basic_test.go @@ -52,6 +52,9 @@ var testCases = []struct { {&intArrayStructValue, intArrayStructValueString}, {&myUInt8SliceValue, myUInt8SliceString}, {&myUInt8ArrayValue, myUInt8ArrayString}, + {&myGenDeclaredValue, myGenDeclaredString}, + {&myGenDeclaredWithCommentValue, myGenDeclaredWithCommentString}, + {&myTypeDeclaredValue, myTypeDeclaredString}, } func TestMarshal(t *testing.T) { @@ -242,3 +245,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"}` +) From fc8e2d7bfe02773d41ca9a3ff6fc2b5b26084fab Mon Sep 17 00:00:00 2001 From: warnar boekkooi Date: Thu, 28 Mar 2019 15:30:05 +0100 Subject: [PATCH 2/3] Add support for TypeSpec docs Since doc's can be added on both the generic declaration (GenDecl) as well as on the type declaration (TypeSpec) we need to check both. In order to check both declaration we copy the GenDecl docs to the TypeSpec docs if the TypeSpec has no documentation. --- parser/parser.go | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/parser/parser.go b/parser/parser.go index 3639ed0..acfdd26 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -21,8 +21,7 @@ type Parser struct { type visitor struct { *Parser - name string - explicit bool + name string } func (p *Parser) needType(comments string) bool { @@ -43,20 +42,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) From 39cd2e35dc183b914a63118409ebdf6c8b38071f Mon Sep 17 00:00:00 2001 From: GoWebProd Date: Mon, 30 Mar 2020 23:27:47 +0300 Subject: [PATCH 3/3] fix makefile --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index e272dee..ce30363 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,7 @@ generate: build ./tests/reference_to_pointer.go \ ./tests/html.go \ ./tests/unknown_fields.go \ - ./tests/type_declaration.go + ./tests/type_declaration.go bin/easyjson -all ./tests/data.go bin/easyjson -all ./tests/nothing.go @@ -37,7 +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 + bin/easyjson ./tests/type_declaration.go test: generate go test \