Example #1
0
File: typeinfo.go Project: a-kr/pg
func fields(typ reflect.Type) map[string][]int {
	num := typ.NumField()
	dst := make(map[string][]int, num)
	for i := 0; i < num; i++ {
		f := typ.Field(i)

		if f.Anonymous {
			typ := f.Type
			if typ.Kind() == reflect.Ptr {
				typ = typ.Elem()
			}
			for name, indx := range fields(typ) {
				dst[name] = append(f.Index, indx...)
			}
			continue
		}

		if f.PkgPath != "" {
			continue
		}

		tokens := strings.Split(f.Tag.Get("pg"), ",")
		name := tokens[0]
		if name == "-" {
			continue
		}
		if name == "" {
			name = pgutil.Underscore(f.Name)
		}
		dst[name] = f.Index
	}
	return dst
}
Example #2
0
File: main_test.go Project: a-kr/pg
func (t *DBTest) TestFormattingColumnNames(c *C) {
	c.Assert(pgutil.Underscore("Megacolumn"), Equals, "megacolumn")
	c.Assert(pgutil.Underscore("MegaColumn"), Equals, "mega_column")
	c.Assert(pgutil.Underscore("MegaColumn_Id"), Equals, "mega_column__id")
	c.Assert(pgutil.Underscore("MegaColumn_id"), Equals, "mega_column_id")
}