コード例 #1
0
ファイル: generator.go プロジェクト: favclip/qbg
func (b *BuildSource) parseStruct(typeInfo *genbase.TypeInfo) error {
	structType, err := typeInfo.StructType()
	if err != nil {
		return err
	}

	st := &BuildStruct{
		parent:   b,
		typeInfo: typeInfo,
	}

	for _, fieldInfo := range structType.FieldInfos() {
		if len := len(fieldInfo.Names); len == 0 {
			// embedded struct in outer struct or multiply field declarations
			// https://play.golang.org/p/bcxbdiMyP4
			continue
		}

		for _, nameIdent := range fieldInfo.Names {
			err := b.parseField(st, typeInfo, fieldInfo, nameIdent.Name)
			if err != nil {
				return err
			}
		}
	}

	b.Structs = append(b.Structs, st)

	return nil
}
コード例 #2
0
ファイル: main.go プロジェクト: mackee/go-sqlla
func toTable(typeInfo *genbase.TypeInfo) (*Table, error) {
	table := new(Table)
	table.PackageName = typeInfo.FileInfo.Name.Name

	comment := typeInfo.AnnotatedComment.Text
	tableName := strings.TrimPrefix(comment, "//+table: ")
	table.Name = tableName

	table.StructName = typeInfo.Name()

	structType, err := typeInfo.StructType()
	if err != nil {
		return nil, err
	}

	fieldInfos := structType.FieldInfos()
	for _, fieldInfo := range fieldInfos {
		tagText := fieldInfo.Tag.Value[1 : len(fieldInfo.Tag.Value)-1]
		tag := reflect.StructTag(tagText)
		columnInfo := tag.Get("db")
		columnMaps := strings.Split(columnInfo, ",")
		columnName := columnMaps[0]
		isPk := false
		for _, cm := range columnMaps {
			if cm == "primarykey" {
				isPk = true
				break
			}
		}
		column := Column{FieldInfo: fieldInfo, Name: columnName, IsPk: isPk}
		table.AddColumn(column)
	}

	return table, nil
}