Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ jobs:
with:
version: v1.61
args: --timeout=30m
- name: custom linters
run: go run ./cmd/lint ./...
- name: validate generated files
run: |
go generate || exit $?
Expand Down
10 changes: 10 additions & 0 deletions cmd/lint/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import (
"github.com/Azure/dalec/linters"
"golang.org/x/tools/go/analysis/singlechecker"
)

func main() {
singlechecker.Main(linters.YamlJSONTagsMatch)
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ require (
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c
golang.org/x/sync v0.10.0
golang.org/x/sys v0.28.0
golang.org/x/tools v0.26.0
google.golang.org/grpc v1.68.2
gotest.tools/v3 v3.5.2
)
Expand Down Expand Up @@ -88,6 +89,7 @@ require (
go.opentelemetry.io/otel/trace v1.31.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/time v0.6.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.26.0 h1:v/60pFQmzmT9ExmjDv2gGIfi3OqfKoEP6I5+umXlbnQ=
golang.org/x/tools v0.26.0/go.mod h1:TPVVj70c7JJ3WCazhD8OdXcZg/og+b9+tH/KxylGwH0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
81 changes: 81 additions & 0 deletions linters/yaml_json_tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package linters

import (
"go/ast"
"strings"

"golang.org/x/tools/go/analysis"
)

var YamlJSONTagsMatch = &analysis.Analyzer{
Name: "yaml_json_names_match",
Doc: "check that struct tags for json and yaml use the same name",
Run: structTagLinter{}.Run,
}

type structTagLinter struct{}

func (l structTagLinter) Run(pass *analysis.Pass) (interface{}, error) {
for _, file := range pass.Files {
ast.Inspect(file, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.TypeSpec:
if structType, ok := x.Type.(*ast.StructType); ok {
l.checkStructTags(structType, pass)
}
}
return true
})
}
return nil, nil
}

func (structTagLinter) checkStructTags(structType *ast.StructType, pass *analysis.Pass) {
for _, field := range structType.Fields.List {
if field.Tag != nil {
tag := field.Tag.Value

v := getYamlJSONNames(tag)

var checkTags bool
if v[0] != "" || v[1] != "" {
checkTags = true
}

if checkTags && v[0] != v[1] {
pass.Reportf(field.Pos(), "mismatch in struct tags: json=%s, yaml=%s", v[0], v[1])
}
}
}
}

func getYamlJSONNames(tag string) [2]string {
const (
yaml = "yaml"
json = "json"
)

tag = strings.Trim(tag, "`")

var out [2]string
for _, tag := range strings.Fields(tag) {
key, tag, _ := strings.Cut(tag, ":")

value := strings.Trim(tag, `"`)

switch key {
case json:
t, _, _ := strings.Cut(value, ",")
out[0] = t
case yaml:
t, _, _ := strings.Cut(value, ",")
out[1] = t
}

if out[0] != "" && out[1] != "" {
break
}
}

return out
}
180 changes: 180 additions & 0 deletions linters/yaml_json_tags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
package linters

import (
"go/ast"
"go/parser"
"go/token"
"testing"

"golang.org/x/tools/go/analysis"
"gotest.tools/v3/assert"
"gotest.tools/v3/assert/cmp"
)

func TestCheckStructTags(t *testing.T) {
tests := []struct {
name string
src string
expected []string
}{
{
name: "matching tags",
src: `
package test
type Test struct {
Field1 string ` + "`json:\"field1\" yaml:\"field1\"`" + `
}
`,
expected: nil,
},
{
name: "mismatched tags",
src: `
package test
type Test struct {
Field1 string ` + "`json:\"field1\" yaml:\"field2\"`" + `
}
`,
expected: []string{"mismatch in struct tags: json=field1, yaml=field2"},
},
{
name: "missing json tag",
src: `
package test
type Test struct {
Field1 string ` + "`yaml:\"field1\"`" + `
}
`,
expected: []string{"mismatch in struct tags: json=, yaml=field1"},
},
{
name: "missing yaml tag",
src: `
package test
type Test struct {
Field1 string ` + "`json:\"field1\"`" + `
}
`,
expected: []string{"mismatch in struct tags: json=field1, yaml="},
},
{
name: "no tags",
src: `
package test
type Test struct {
Field1 string
}
`,
expected: nil,
},
{
name: "extra spaces",
src: `
package test
type Test struct {
Field1 string ` + "`json:\"field1\" yaml:\"field1\"`" + `
}
`,
expected: nil,
},
{
name: "reversed order",
src: `
package test
type Test struct {
Field1 string ` + "`yaml:\"field1\" json:\"field1\"`" + `
}
`,
expected: nil,
},
{
name: "extra spaces and mismatched tags",
src: `
package test
type Test struct {
Field1 string ` + "`json:\"field1\" yaml:\"field2\"`" + `
}
`,
expected: []string{"mismatch in struct tags: json=field1, yaml=field2"},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fset := token.NewFileSet()
node, err := parser.ParseFile(fset, "test.go", tt.src, parser.ParseComments)
if err != nil {
t.Fatalf("failed to parse source: %v", err)
}

var reports []string
pass := &analysis.Pass{
Fset: fset,
Files: []*ast.File{node},
Report: func(d analysis.Diagnostic) {
reports = append(reports, d.Message)
},
}

linter := structTagLinter{}
_, err = linter.Run(pass)
assert.NilError(t, err)

assert.Assert(t, cmp.Len(reports, len(tt.expected)))
assert.Assert(t, cmp.DeepEqual(reports, tt.expected))
})
}
}

func TestGetYamlJSONNames(t *testing.T) {
tests := []struct {
tag string
expected [2]string
}{
{
tag: "`json:\"field1\" yaml:\"field1\"`",
expected: [2]string{"field1", "field1"},
},
{
tag: "`json:\"field1\" yaml:\"field2\"`",
expected: [2]string{"field1", "field2"},
},
{
tag: "`json:\"field1\"`",
expected: [2]string{"field1", ""},
},
{
tag: "`yaml:\"field1\"`",
expected: [2]string{"", "field1"},
},
{
tag: "`json:\"field1,omitempty\" yaml:\"field1\"`",
expected: [2]string{"field1", "field1"},
},
{
tag: "`json:\"field1\" yaml:\"field1,omitempty\"`",
expected: [2]string{"field1", "field1"},
},
{
tag: "`json:\"field1\" yaml:\"field1\"`",
expected: [2]string{"field1", "field1"},
},
{
tag: "`yaml:\"field1\" json:\"field1\"`",
expected: [2]string{"field1", "field1"},
},
{
tag: "`json:\"field1\" yaml:\"field2\"`",
expected: [2]string{"field1", "field2"},
},
}

for _, tt := range tests {
t.Run(tt.tag, func(t *testing.T) {
result := getYamlJSONNames(tt.tag)
if result != tt.expected {
t.Errorf("expected %v, got %v", tt.expected, result)
}
})
}
}