feat: foreignKeys check

+fix: "unnamed" field bug
This commit is contained in:
2024-12-30 23:44:24 +07:00
parent ece3f3c801
commit 03ccaff375
10 changed files with 123 additions and 21 deletions

View File

@@ -32,12 +32,18 @@ func ParseModels(pass *analysis.Pass, models *map[string]Model) {
for _, field := range structure.Fields.List {
var structField Field
if err := CheckUnnamedField(typeSpec.Name.Name, *field); err != nil {
pass.Reportf(field.Pos(), err.Error())
return false
if len(field.Names) == 0 {
fieldType := ResolveBaseType(field.Type)
if fieldType == nil {
pass.Reportf(field.Pos(), "Failed to resolve model \"%s\" field type: %s", model.Name, field.Type)
} else {
structField.Name = *fieldType
}
} else {
structField.Name = field.Names[0].Name
}
structField.Name = field.Names[0].Name
structField.Pos = field.Pos()
structField.Comment = field.Comment.Text()
structField.Type = field.Type

View File

@@ -43,7 +43,7 @@ func isGormValueNullable(tags *structtag.Tags) (*bool, error) {
}
}
func CheckFieldNullConsistency(field ast.Field, structName string, structTags string) error {
func CheckFieldNullConsistency(field ast.Field, fieldName string, structName string, structTags string) error {
tags, err := structtag.Parse(structTags)
if err != nil {
return errors.New(fmt.Sprintf("Invalid structure tag: %s", err))
@@ -64,7 +64,7 @@ func CheckFieldNullConsistency(field ast.Field, structName string, structTags st
}
if isFieldNullable != *isColumnNullable {
return errors.New(fmt.Sprintf("Null safety error in \"%s\" model, field \"%s\": column nullable policy doesn't match to tag nullable policy", structName, field.Names[0].Name))
return errors.New(fmt.Sprintf("Null safety error in \"%s\" model, field \"%s\": column nullable policy doesn't match to tag nullable policy", structName, fieldName))
}
return nil
}

View File

@@ -12,10 +12,3 @@ func CheckUnnamedModel(typeSpec ast.TypeSpec) error {
}
return nil
}
func CheckUnnamedField(structName string, field ast.Field) error {
if len(field.Names) == 0 {
return errors.New(fmt.Sprintf("Struct \"%s\" has unnamed field", structName))
}
return nil
}