Skip to content

Commit

Permalink
Merge pull request #64 from LeGEC/gv/extra-cases
Browse files Browse the repository at this point in the history
Detect extra cases in struct declarations
  • Loading branch information
fatih authored Nov 4, 2020
2 parents d4b34d8 + 2ede847 commit c502265
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 0 deletions.
37 changes: 37 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ func (c *config) addTags(fieldName string, tags *structtag.Tags) (*structtag.Tag
// collectStructs collects and maps structType nodes to their positions
func collectStructs(node ast.Node) map[token.Pos]*structType {
structs := make(map[token.Pos]*structType, 0)

collectStructs := func(n ast.Node) bool {
var t ast.Expr
var structName string
Expand All @@ -475,8 +476,30 @@ func collectStructs(node ast.Node) map[token.Pos]*structType {
case *ast.ValueSpec:
structName = x.Names[0].Name
t = x.Type
case *ast.Field:
// this case also catches struct fields and the structName
// therefore might contain the field name (which is wrong)
// because `x.Type` in this case is not a *ast.StructType.
//
// We're OK with it, because, in our case *ast.Field represents
// a parameter declaration, i.e:
//
// func test(arg struct {
// Field int
// }) {
// }
//
// and hence the struct name will be `arg`.
if len(x.Names) != 0 {
structName = x.Names[0].Name
}
t = x.Type
}

// if expression is in form "*T" or "[]T", dereference to check if "T"
// contains a struct expression
t = deref(t)

x, ok := t.(*ast.StructType)
if !ok {
return true
Expand All @@ -488,6 +511,7 @@ func collectStructs(node ast.Node) map[token.Pos]*structType {
}
return true
}

ast.Inspect(node, collectStructs)
return structs
}
Expand Down Expand Up @@ -866,3 +890,16 @@ func split(line string) (int, error) {

return 0, fmt.Errorf("couldn't parse line: '%s'", line)
}

// deref takes an expression, and removes all its leading "*" and "[]"
// operator. Uuse case : if found expression is a "*t" or "[]t", we need to
// check if "t" contains a struct expression.
func deref(x ast.Expr) ast.Expr {
switch t := x.(type) {
case *ast.StarExpr:
return deref(t.X)
case *ast.ArrayType:
return deref(t.Elt)
}
return x
}
27 changes: 27 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,33 @@ func TestRewrite(t *testing.T) {
fieldName: "bar",
},
},
{
file: "offset_anonymous_struct",
cfg: &config{
add: []string{"json"},
output: "source",
offset: 45,
transform: "camelcase",
},
},
{
file: "offset_star_struct",
cfg: &config{
add: []string{"json"},
output: "source",
offset: 35,
transform: "camelcase",
},
},
{
file: "offset_array_struct",
cfg: &config{
add: []string{"json"},
output: "source",
offset: 35,
transform: "camelcase",
},
},
}

for _, ts := range test {
Expand Down
6 changes: 6 additions & 0 deletions test-fixtures/offset_anonymous_struct.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package main

func test(arg struct {
Field int `json:"field"`
}) {
}
6 changes: 6 additions & 0 deletions test-fixtures/offset_anonymous_struct.input
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package main

func test(arg struct {
Field int
}) {
}
5 changes: 5 additions & 0 deletions test-fixtures/offset_array_struct.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

var x []struct {
Field int `json:"field"`
}
5 changes: 5 additions & 0 deletions test-fixtures/offset_array_struct.input
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

var x []struct {
Field int
}
5 changes: 5 additions & 0 deletions test-fixtures/offset_star_struct.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

var x *struct {
Field int `json:"field"`
}
5 changes: 5 additions & 0 deletions test-fixtures/offset_star_struct.input
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

var x *struct {
Field int
}

0 comments on commit c502265

Please sign in to comment.