gitee.com/eden-framework/sqlx@v0.0.3/generator/model.go (about)

     1  package generator
     2  
     3  import (
     4  	"gitee.com/eden-framework/codegen"
     5  	"gitee.com/eden-framework/packagex"
     6  	"go/types"
     7  	"strings"
     8  
     9  	"gitee.com/eden-framework/sqlx/builder"
    10  )
    11  
    12  func NewModel(pkg *packagex.Package, typeName *types.TypeName, comments string, cfg *Config) *Model {
    13  	m := Model{}
    14  	m.Config = cfg
    15  	m.Config.SetDefaults()
    16  
    17  	m.TypeName = typeName
    18  
    19  	m.Table = builder.T(cfg.TableName)
    20  
    21  	p := pkg.Pkg(typeName.Pkg().Path())
    22  
    23  	forEachStructField(typeName.Type().Underlying().Underlying().(*types.Struct), func(structVal *types.Var, columnName string, tagValue string) {
    24  		col := builder.Col(columnName).Field(structVal.Name()).Type("", tagValue)
    25  
    26  		for id, o := range p.TypesInfo.Defs {
    27  			if o == structVal {
    28  				doc := pkg.CommentsOf(id)
    29  
    30  				rel, lines := parseColRelFromComment(doc)
    31  
    32  				if rel != "" {
    33  					relPath := strings.Split(rel, ".")
    34  
    35  					if len(relPath) != 2 {
    36  						continue
    37  					}
    38  
    39  					col.Relation = relPath
    40  				}
    41  
    42  				if len(lines) > 0 {
    43  					col.Comment = lines[0]
    44  					col.Description = lines
    45  				}
    46  			}
    47  		}
    48  
    49  		m.addColumn(col, structVal)
    50  	})
    51  
    52  	m.HasDeletedAt = m.Table.F(m.FieldKeyDeletedAt) != nil
    53  	m.HasCreatedAt = m.Table.F(m.FieldKeyCreatedAt) != nil
    54  	m.HasUpdatedAt = m.Table.F(m.FieldKeyUpdatedAt) != nil
    55  
    56  	keys, lines := parseKeysFromDoc(comments)
    57  	m.Keys = keys
    58  
    59  	if len(lines) > 0 {
    60  		m.Description = lines
    61  	}
    62  
    63  	if m.HasDeletedAt {
    64  		m.Keys.PatchUniqueIndexesWithSoftDelete(m.FieldKeyDeletedAt)
    65  	}
    66  	m.Keys.Bind(m.Table)
    67  
    68  	if autoIncrementCol := m.Table.AutoIncrement(); autoIncrementCol != nil {
    69  		m.HasAutoIncrement = true
    70  		m.FieldKeyAutoIncrement = autoIncrementCol.FieldName
    71  	}
    72  
    73  	return &m
    74  }
    75  
    76  type Model struct {
    77  	*types.TypeName
    78  	*Config
    79  	*Keys
    80  	*builder.Table
    81  	Fields                map[string]*types.Var
    82  	FieldKeyAutoIncrement string
    83  	HasDeletedAt          bool
    84  	HasCreatedAt          bool
    85  	HasUpdatedAt          bool
    86  	HasAutoIncrement      bool
    87  }
    88  
    89  func (m *Model) addColumn(col *builder.Column, tpe *types.Var) {
    90  	m.Table.Columns.Add(col)
    91  	if m.Fields == nil {
    92  		m.Fields = map[string]*types.Var{}
    93  	}
    94  	m.Fields[col.FieldName] = tpe
    95  }
    96  
    97  func (m *Model) WriteTo(file *codegen.File) {
    98  	m.WriteTableKeyInterfaces(file)
    99  
   100  	if m.WithTableName {
   101  		m.WriteTableName(file)
   102  	}
   103  
   104  	if m.WithTableInterfaces {
   105  		m.WriteTableInterfaces(file)
   106  	}
   107  
   108  	if m.WithMethods {
   109  		m.WriteCRUD(file)
   110  		m.WriteList(file)
   111  		m.WriteCount(file)
   112  		m.WriteBatchList(file)
   113  	}
   114  }
   115  
   116  func (m *Model) Type() codegen.SnippetType {
   117  	return codegen.Type(m.StructName)
   118  }
   119  
   120  func (m *Model) IteratorType() codegen.SnippetType {
   121  	return codegen.Type(m.StructName + "Iterator")
   122  }
   123  
   124  func (m *Model) PtrType() codegen.SnippetType {
   125  	return codegen.Star(m.Type())
   126  }
   127  
   128  func (m *Model) VarTable() string {
   129  	return m.StructName + "Table"
   130  }