From c13286cdb4c6fd98c732a65bb0505247ab9a8363 Mon Sep 17 00:00:00 2001 From: Lyra Naeseth Date: Tue, 25 Jan 2022 15:09:36 -0800 Subject: [PATCH 1/3] fix: Ignore struct fields with `inline` tag flag (#9) --- tagliatelle.go | 26 ++++++++++++++++++++------ testdata/src/a/sample.go | 5 +++++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/tagliatelle.go b/tagliatelle.go index 801f9d3..b0e589a 100644 --- a/tagliatelle.go +++ b/tagliatelle.go @@ -85,7 +85,7 @@ func analyze(pass *analysis.Pass, config Config, n *ast.StructType, field *ast.F continue } - value, ok := lookupTagValue(field.Tag, key) + value, flags, ok := lookupTagValue(field.Tag, key) if !ok { // skip when no struct tag for the key continue @@ -95,6 +95,10 @@ func analyze(pass *analysis.Pass, config Config, n *ast.StructType, field *ast.F // skip when skipped :) continue } + if hasTagFlag(flags, "inline") { + // skip for inline children (no name to lint) + continue + } if value == "" { value = fieldName @@ -142,25 +146,35 @@ func getTypeName(exp ast.Expr) (string, error) { return getTypeName(typ.Sel) default: bytes, _ := json.Marshal(exp) - return "", fmt.Errorf("unexpected eror: type %T: %s", typ, string(bytes)) + return "", fmt.Errorf("unexpected error: type %T: %s", typ, string(bytes)) } } -func lookupTagValue(tag *ast.BasicLit, key string) (string, bool) { +func lookupTagValue(tag *ast.BasicLit, key string) (name string, flags []string, ok bool) { raw := strings.Trim(tag.Value, "`") value, ok := reflect.StructTag(raw).Lookup(key) if !ok { - return value, ok + return value, nil, ok } values := strings.Split(value, ",") if len(values) < 1 { - return "", true + return "", nil, true + } + + return values[0], values[1:], true +} + +func hasTagFlag(flags []string, query string) bool { + for _, flag := range flags { + if flag == query { + return true + } } - return values[0], true + return false } func getConverter(c string) (func(s string) string, error) { diff --git a/testdata/src/a/sample.go b/testdata/src/a/sample.go index f2c1565..82fa75d 100644 --- a/testdata/src/a/sample.go +++ b/testdata/src/a/sample.go @@ -7,6 +7,7 @@ type Foo struct { Value string `json:"value,omitempty"` Bar Bar `json:"bar"` Bur `json:"bur"` + Quux Quux `json:",inline"` } type Bar struct { @@ -28,3 +29,7 @@ type Bur struct { Also string `json:",omitempty"` // want `json\(camel\): got 'Also' want 'also'` ReqPerS string `avro:"req_per_s"` } + +type Quux struct { + Data []byte `json:"data"` +} From d05302088d321cf2ae583251b4ab47d7ce9b6837 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Wed, 26 Jan 2022 00:26:15 +0100 Subject: [PATCH 2/3] chore: add explanation and test case --- tagliatelle.go | 5 +++++ testdata/src/a/sample.go | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/tagliatelle.go b/tagliatelle.go index b0e589a..53e77d1 100644 --- a/tagliatelle.go +++ b/tagliatelle.go @@ -95,6 +95,11 @@ func analyze(pass *analysis.Pass, config Config, n *ast.StructType, field *ast.F // skip when skipped :) continue } + + // TODO(ldez): need to be rethink. + // This is an exception because of a bug. + // https://github.com/ldez/tagliatelle/issues/8 + // For now, tagliatelle should try to remain neutral in terms of format. if hasTagFlag(flags, "inline") { // skip for inline children (no name to lint) continue diff --git a/testdata/src/a/sample.go b/testdata/src/a/sample.go index 82fa75d..4c079aa 100644 --- a/testdata/src/a/sample.go +++ b/testdata/src/a/sample.go @@ -7,7 +7,9 @@ type Foo struct { Value string `json:"value,omitempty"` Bar Bar `json:"bar"` Bur `json:"bur"` - Quux Quux `json:",inline"` + + Qiix Quux `json:",inline"` + Quux `json:",inline"` } type Bar struct { From 1b48d2045f9bf54addefe6663ac83b0f95d99e67 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Thu, 27 Jan 2022 00:04:48 +0100 Subject: [PATCH 3/3] chore: update linter --- .github/workflows/main.yml | 2 +- .golangci.yml | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index f37c7de..5321c9a 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -15,7 +15,7 @@ jobs: runs-on: ubuntu-latest env: GO_VERSION: 1.17 - GOLANGCI_LINT_VERSION: v1.43.0 + GOLANGCI_LINT_VERSION: v1.44.0 CGO_ENABLED: 0 steps: diff --git a/.golangci.yml b/.golangci.yml index 8cdb9ef..53313e3 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -10,8 +10,6 @@ linters-settings: - fieldalignment gocyclo: min-complexity: 15 - maligned: - suggest-new: true goconst: min-len: 5 min-occurrences: 3 @@ -72,6 +70,7 @@ linters: - forcetypeassert - varnamelen - nilnil + - errchkjson issues: exclude-use-default: false