Esempio n. 1
0
// add an model and it's field to parse result
func (v Visitor) add(model, table, field, col string) {
	if table == "" {
		table = strings2.ToSnake(model)
	}

	if col == "" {
		col = strings2.ToSnake(field)
	}

	t, has := v.Models[model]
	if !has {
		t = &Table{Name: table, Fields: sortedmap.New(), initialed: true}
		v.Models[model] = t
	} else if t.Fields.Indexes == nil {
		t.Name = table
		t.Fields = sortedmap.New()
	}

	t.Fields.Set(field, col)
}
Esempio n. 2
0
// parseModel will first use field tag as column name, the tag key is 'column',
// if no tag specified, use field name's camel_case, disable a field or model
// by set '-' as field tag value
func parseModel(v Model, db *DB) *Table {
	var nocache bool
	if nc, is := v.(Nocacher); is {
		nocache = nc.Nocache()
	}

	if c, is := v.(Columner); is {
		return newTable(v.Table(), c.Columns(), nocache)
	}

	typ := reflect2.IndirectType(v)
	num := typ.NumField()

	cols := make([]string, 0)

	for i := 0; i < num; i++ {
		field := typ.Field(i)
		col := strings2.ToSnake(field.Name)

		b, err := strconv.ParseBool(field.Tag.Get("nocache"))
		if err == nil {
			nocache = b
		}
		if nocache {
			break
		}

		if !field.Anonymous ||
			field.Type.Kind() != reflect.Struct {

			colTag := field.Tag.Get("column")
			if colTag == "-" {
				continue
			}
			if colTag != "" {
				col = colTag
			}

			cols = append(cols, col)
		}
	}

	return newTable(
		v.Table(),
		slices.FitCapToLenString(cols),
		nocache,
	)
}