structs for models

This commit is contained in:
2024-12-29 23:49:46 +07:00
parent a7f82bf8f2
commit b222a44833
5 changed files with 127 additions and 5 deletions

View File

@@ -1 +1,23 @@
package common
import (
"go/ast"
"go/token"
)
type Field struct {
Name string
Type ast.Expr
Tags *string
Options []string // contains options like "autoCreateTime" or "null"
Params []string // contains params like "foreignKey:CustomerId" or "constrain:OnDelete:Cascade"
Position token.Pos
Comment string
}
type Model struct {
Name string
Fields map[string]Field
Position token.Pos
Comment string
}

View File

@@ -1 +1,10 @@
package common
import "strings"
func NormalizeStructTags(tags string) string {
// todo: process case with check with ';' literal
tagWithoutQuotes := tags[1 : len(tags)-1]
tagWithoutSemicolons := strings.ReplaceAll(tagWithoutQuotes, ";", ",")
return tagWithoutSemicolons
}

View File

@@ -21,12 +21,12 @@ func isGormValueNullable(tags *structtag.Tags) (*bool, error) {
return nil, nil
}
gormTag.Options = append([]string{gormTag.Name}, gormTag.Options...)
if err != nil {
return nil, nil
}
gormTag.Options = append([]string{gormTag.Name}, gormTag.Options...)
nullTagExist := gormTag.HasOption("null")
notNullTagExist := gormTag.HasOption("not null")
@@ -47,8 +47,8 @@ func CheckFieldNullConsistency(field ast.Field, structName string, structTags st
tags, err := structtag.Parse(structTags)
if err != nil {
return errors.New(fmt.Sprintf("Invalid structure tag: %s", err))
}
if tags == nil {
return nil
}