Skip to content

Commit

Permalink
Ignore struct fields with inline tag flag
Browse files Browse the repository at this point in the history
Fixes ldez#8.
  • Loading branch information
silverlyra committed Jan 25, 2022
1 parent 7b21925 commit 52b6491
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
26 changes: 20 additions & 6 deletions tagliatelle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions testdata/src/a/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"`
}

0 comments on commit 52b6491

Please sign in to comment.