Skip to content
Snippets Groups Projects
Commit cdd7f820 authored by Christophe de Vienne's avatar Christophe de Vienne
Browse files

Fix handling of unnamed fields with 'db' tags

parent 62b58f8e
No related branches found
No related tags found
No related merge requests found
...@@ -30,7 +30,9 @@ ...@@ -30,7 +30,9 @@
for _, field := range structType.Fields.List { for _, field := range structType.Fields.List {
if len(field.Names) == 0 { if len(field.Names) == 0 {
if ident, ok := field.Type.(*ast.Ident); ok { if name, _, _ := getFieldTag(field, "db"); name == "" {
if sub, ok := p.AllStructs[ident.String()]; ok { if ident, ok := field.Type.(*ast.Ident); ok {
fields = append(fields, p.getAllFields(sub)...) if sub, ok := p.AllStructs[ident.String()]; ok {
fields = append(fields, p.getAllFields(sub)...)
}
} }
...@@ -36,4 +38,6 @@ ...@@ -36,4 +38,6 @@
} }
} else {
fields = append(fields, field)
} }
} else { } else {
fields = append(fields, field) fields = append(fields, field)
...@@ -64,9 +68,10 @@ ...@@ -64,9 +68,10 @@
// DBField is a field of a DBStruct. // DBField is a field of a DBStruct.
type DBField struct { type DBField struct {
Name string Name string
Column string Column string
IsPKey bool IsPKey bool
IsStruct bool
} }
func main() { func main() {
...@@ -243,8 +248,26 @@ ...@@ -243,8 +248,26 @@
return &dbstruct, nil return &dbstruct, nil
} }
func getFieldTag(field *ast.Field, name string) (string, []string, error) {
if field.Tag == nil || len(field.Tag.Value) < 2 {
return "", nil, os.ErrNotExist
}
tags, err := structtag.Parse(
field.Tag.Value[1 : len(field.Tag.Value)-1])
if err != nil {
return "", nil, err
}
tag, err := tags.Get(name)
if err != nil {
return "", nil, err
}
return tag.Name, tag.Options, nil
}
func getDBFields(topLevel *Package, structType *ast.StructType) ([]DBField, error) { func getDBFields(topLevel *Package, structType *ast.StructType) ([]DBField, error) {
allFields := topLevel.getAllFields(structType) allFields := topLevel.getAllFields(structType)
dbfields := make([]DBField, 0, len(allFields)) dbfields := make([]DBField, 0, len(allFields))
for _, field := range allFields { for _, field := range allFields {
...@@ -246,11 +269,7 @@ ...@@ -246,11 +269,7 @@
func getDBFields(topLevel *Package, structType *ast.StructType) ([]DBField, error) { func getDBFields(topLevel *Package, structType *ast.StructType) ([]DBField, error) {
allFields := topLevel.getAllFields(structType) allFields := topLevel.getAllFields(structType)
dbfields := make([]DBField, 0, len(allFields)) dbfields := make([]DBField, 0, len(allFields))
for _, field := range allFields { for _, field := range allFields {
if field.Tag == nil || len(field.Tag.Value) < 2 { tagname, _, err := getFieldTag(field, "db")
continue
}
tags, err := structtag.Parse(
field.Tag.Value[1 : len(field.Tag.Value)-1])
if err != nil { if err != nil {
...@@ -256,4 +275,8 @@ ...@@ -256,4 +275,8 @@
if err != nil { if err != nil {
if errors.Is(err, os.ErrNotExist) {
continue
}
return nil, err return nil, err
} }
...@@ -257,7 +280,25 @@ ...@@ -257,7 +280,25 @@
return nil, err return nil, err
} }
dbtag, err := tags.Get("db") var (
if err != nil { name string
continue isStruct bool
)
if len(field.Names) == 0 {
var ident *ast.Ident
switch v := field.Type.(type) {
case *ast.SelectorExpr:
ident = v.Sel
isStruct = true
case *ast.Ident:
ident = v
isStruct = true
}
if ident != nil {
name = ident.Name
} else {
continue
}
} else {
name = field.Names[0].String()
} }
...@@ -263,3 +304,2 @@ ...@@ -263,3 +304,2 @@
} }
dbfields = append(dbfields, DBField{ dbfields = append(dbfields, DBField{
...@@ -265,6 +305,7 @@ ...@@ -265,6 +305,7 @@
dbfields = append(dbfields, DBField{ dbfields = append(dbfields, DBField{
Name: field.Names[0].String(), Name: name,
Column: dbtag.Name, Column: tagname,
IsStruct: isStruct,
}) })
} }
...@@ -495,7 +536,7 @@ ...@@ -495,7 +536,7 @@
switch column { switch column {
{{- range .Fields}} {{- range .Fields}}
case "{{.Column}}": case "{{.Column}}":
values[i] = s.{{.Name}} values[i] = {{if .IsStruct}}&{{ end }}s.{{.Name}}
{{- end}} {{- end}}
} }
} }
...@@ -510,7 +551,7 @@ ...@@ -510,7 +551,7 @@
switch column { switch column {
{{- range .Fields}} {{- range .Fields}}
case "{{.Column}}": case "{{.Column}}":
values["{{.Column}}"] = s.{{.Name}} values["{{.Column}}"] = {{if .IsStruct}}&{{ end }}s.{{.Name}}
{{- end}} {{- end}}
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment