本文整理汇总了Golang中reflect.Type.NumField方法的典型用法代码示例。如果您正苦于以下问题:Golang Type.NumField方法的具体用法?Golang Type.NumField怎么用?Golang Type.NumField使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reflect.Type
的用法示例。
在下文中一共展示了Type.NumField方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: finishNewTable
func finishNewTable(b *tablebase, ty reflect.Type) Table {
id := C.newTable()
t := &table{
scroller: newScroller(id, true), // border on Table
tablebase: b,
selected: newEvent(),
}
t.fpreferredSize = t.xpreferredSize
// also sets the delegate
C.tableMakeDataSource(t.id, unsafe.Pointer(t))
for i := 0; i < ty.NumField(); i++ {
colname := ty.Field(i).Tag.Get("uicolumn")
if colname == "" {
colname = ty.Field(i).Name
}
cname := C.CString(colname)
coltype := C.colTypeText
editable := false
switch {
case ty.Field(i).Type == reflect.TypeOf((*image.RGBA)(nil)):
coltype = C.colTypeImage
case ty.Field(i).Type.Kind() == reflect.Bool:
coltype = C.colTypeCheckbox
editable = true
}
C.tableAppendColumn(t.id, C.intptr_t(i), cname, C.int(coltype), toBOOL(editable))
C.free(unsafe.Pointer(cname)) // free now (not deferred) to conserve memory
}
return t
}
示例2: isEmptyStruct
// isEmptyStruct returns true if given Type is either not a Struct or
// has 0 fields
func isEmptyStruct(t reflect.Type) bool {
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
// TODO: check for unexported fields?
return t.Kind() == reflect.Struct && t.NumField() == 0
}
示例3: validateListType
func validateListType(target reflect.Type) error {
// exceptions
if listTypeExceptions.Has(target.Name()) {
return nil
}
hasListSuffix := strings.HasSuffix(target.Name(), "List")
hasMetadata := false
hasItems := false
for i := 0; i < target.NumField(); i++ {
field := target.Field(i)
tag := field.Tag.Get("json")
switch {
case strings.HasPrefix(tag, "metadata"):
hasMetadata = true
case tag == "items":
hasItems = true
if field.Type.Kind() != reflect.Slice {
return fmt.Errorf("Expected items to be slice, got %s", field.Type.Kind())
}
}
}
if hasListSuffix && !hasMetadata {
return fmt.Errorf("Expected type %s to contain \"metadata\"", target.Name())
}
if hasListSuffix && !hasItems {
return fmt.Errorf("Expected type %s to contain \"items\"", target.Name())
}
// if a type contains field Items with JSON tag "items", its name should end with List.
if !hasListSuffix && hasItems {
return fmt.Errorf("Type %s has Items, its name is expected to end with \"List\"", target.Name())
}
return nil
}
示例4: MakeTypeInfo
func MakeTypeInfo(rt reflect.Type) *TypeInfo {
info := &TypeInfo{Type: rt}
// If struct, register field name options
if rt.Kind() == reflect.Struct {
numFields := rt.NumField()
structFields := []StructFieldInfo{}
for i := 0; i < numFields; i++ {
field := rt.Field(i)
if field.PkgPath != "" {
continue
}
skip, opts := getOptionsFromField(field)
if skip {
continue
}
structFields = append(structFields, StructFieldInfo{
Index: i,
Type: field.Type,
Options: opts,
})
}
info.Fields = structFields
}
return info
}
示例5: Indexes
func (c *structCache) Indexes(typ reflect.Type) map[string][]int {
c.l.RLock()
indxs, ok := c.m[typ]
c.l.RUnlock()
if ok {
return indxs
}
numField := typ.NumField()
indxs = make(map[string][]int, numField)
for i := 0; i < numField; i++ {
f := typ.Field(i)
if f.PkgPath != "" {
continue
}
tokens := strings.Split(f.Tag.Get("pg"), ",")
name := tokens[0]
if name == "-" {
continue
}
if name == "" {
name = formatColumnName(f.Name)
}
indxs[name] = f.Index
}
c.l.Lock()
c.m[typ] = indxs
c.l.Unlock()
return indxs
}
示例6: getTableColumns
func getTableColumns(thing interface{}, typ reflect.Type) []*columnMap {
columns := make([]*columnMap, 0, typ.NumField())
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
tag := strings.Split(field.Tag.Get("db"), ",")
if len(tag) > 0 && tag[0] != "" {
col := &columnMap{Field: i}
for _, flag := range tag {
switch flag {
case "pk":
col.PrimaryKey = true
case "serialize":
col.Serialize = true
default:
if col.Name == "" {
col.Name = flag
}
}
}
columns = append(columns, col)
}
}
return columns
}
示例7: rawSelectByStruct
func (db *DB) rawSelectByStruct(structType reflect.Type, qi SqlQueryInfo) (rows *sql.Rows, fields []string, err error) {
// nums of struct's fields
lf := structType.NumField()
// type's fields
fields = make([]string, 0, lf)
// sql select columns, it's Snake Cased
columns := make([]string, 0, lf)
// get fields in structType,
// and convert to sql query column name
for i := 0; i < lf; i++ {
structField := structType.Field(i)
fieldName := structField.Name
fields = append(fields, fieldName)
columns = append(columns, utils.SnakeCasedName(fieldName))
}
// tableName := utils.SnakeCasedName(utils.StructName(s))
tableName := utils.SnakeCasedName(structType.Name())
// TODO: check the fileds has specified ?
qi.Fields = strings.Join(columns, ", ")
// run query from db
rows, err = db.Select(tableName, qi)
return
}
示例8: getFields
func getFields(typ reflect.Type) *fields {
numField := typ.NumField()
fs := newFields(numField)
for i := 0; i < numField; i++ {
f := typ.Field(i)
if f.PkgPath != "" && !f.Anonymous {
continue
}
name, opts := parseTag(f.Tag.Get("msgpack"))
if name == "-" {
continue
}
if opts.Contains("inline") {
inlineFields(fs, f)
continue
}
if name == "" {
name = f.Name
}
field := field{
name: name,
index: f.Index,
omitEmpty: opts.Contains("omitempty"),
encoder: getEncoder(f.Type),
decoder: getDecoder(f.Type),
}
fs.Add(&field)
}
return fs
}
示例9: checkStructs
func checkStructs(c *C, t reflect.Type, structsChecked map[string]struct{}) {
for t.Kind() == reflect.Ptr || t.Kind() == reflect.Map || t.Kind() == reflect.Slice {
t = t.Elem()
}
if t.Kind() != reflect.Struct {
return
}
if _, present := structsChecked[t.String()]; present {
// Already checked this type
return
}
structsChecked[t.String()] = struct{}{}
byUpperCase := make(map[string]int)
for i := 0; i < t.NumField(); i++ {
sf := t.Field(i)
// Check that the yaml tag does not contain an _.
yamlTag := sf.Tag.Get("yaml")
if strings.Contains(yamlTag, "_") {
c.Fatalf("yaml field name includes _ character: %s", yamlTag)
}
upper := strings.ToUpper(sf.Name)
if _, present := byUpperCase[upper]; present {
c.Fatalf("field name collision in configuration object: %s", sf.Name)
}
byUpperCase[upper] = i
checkStructs(c, sf.Type, structsChecked)
}
}
示例10: javaType
func (g *schemaGenerator) javaType(t reflect.Type) string {
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
pkgDesc, ok := g.packages[t.PkgPath()]
if ok {
return pkgDesc.JavaPackage + "." + capitalizeFirst(t.Name())
} else {
switch t.Kind() {
case reflect.Bool:
return "bool"
case reflect.Int, reflect.Int8, reflect.Int16,
reflect.Int32, reflect.Uint,
reflect.Uint8, reflect.Uint16, reflect.Uint32:
return "int"
case reflect.Int64, reflect.Uint64:
return "Long"
case reflect.Float32, reflect.Float64, reflect.Complex64,
reflect.Complex128:
return "double"
case reflect.String:
return "String"
case reflect.Array, reflect.Slice:
return g.javaTypeArrayList(t.Elem())
case reflect.Map:
return "java.util.Map<String," + g.javaTypeWrapPrimitive(t.Elem()) + ">"
default:
if len(t.Name()) == 0 && t.NumField() == 0 {
return "Object"
}
return capitalizeClassName(t.Name())
}
}
}
示例11: decodeField
func (d decoder) decodeField(sf reflect.StructField, t reflect.Type, v reflect.Value) MessageRejectError {
if sf.Tag.Get("fix") != "" {
fixTag, err := strconv.Atoi(sf.Tag.Get("fix"))
if err != nil {
panic(err)
}
if !d.FieldMap.Has(Tag(fixTag)) {
return nil
}
return d.decodeValue(Tag(fixTag), t, v)
}
switch t.Kind() {
case reflect.Ptr:
v.Set(reflect.New(t.Elem()))
return d.decodeField(sf, t.Elem(), v.Elem())
case reflect.Struct:
for i := 0; i < t.NumField(); i++ {
if err := d.decodeField(t.Field(i), t.Field(i).Type, v.Field(i)); err != nil {
return err
}
}
}
return nil
}
示例12: New
func New(t interface{}, tags []string, conf Configurator) (*Struct, error) {
var typ reflect.Type
if tt, ok := t.(reflect.Type); ok {
typ = tt
} else {
typ = reflect.TypeOf(t)
}
for typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
if typ.Kind() != reflect.Struct {
return nil, ErrNoStruct
}
if typ.NumField() == 0 {
return nil, ErrNoFields
}
s := &Struct{
Type: typ,
MNameMap: make(map[string]int),
QNameMap: make(map[string]int),
tags: tags,
conf: conf,
}
if err := s.initialize(typ); err != nil {
return nil, err
}
return s, nil
}
示例13: getFieldInfos
func getFieldInfos(rType reflect.Type, parentIndexChain []int) []fieldInfo {
fieldsCount := rType.NumField()
fieldsList := make([]fieldInfo, 0, fieldsCount)
for i := 0; i < fieldsCount; i++ {
field := rType.Field(i)
if field.PkgPath != "" {
continue
}
indexChain := append(parentIndexChain, i)
// if the field is an embedded struct, create a fieldInfo for each of its fields
if field.Anonymous && field.Type.Kind() == reflect.Struct {
fieldsList = append(fieldsList, getFieldInfos(field.Type, indexChain)...)
continue
}
fieldInfo := fieldInfo{IndexChain: indexChain}
fieldTag := field.Tag.Get("csv")
fieldTags := strings.Split(fieldTag, TagSeparator)
filteredTags := []string{}
for _, fieldTagEntry := range fieldTags {
if fieldTagEntry != "omitempty" {
filteredTags = append(filteredTags, fieldTagEntry)
}
}
if len(filteredTags) == 1 && filteredTags[0] == "-" {
continue
} else if len(filteredTags) > 0 && filteredTags[0] != "" {
fieldInfo.keys = filteredTags
} else {
fieldInfo.keys = []string{field.Name}
}
fieldsList = append(fieldsList, fieldInfo)
}
return fieldsList
}
示例14: checkJsonTags
func checkJsonTags(objType reflect.Type, seen *map[reflect.Type]bool, t *testing.T) {
if _, exists := (*seen)[objType]; exists {
return
}
(*seen)[objType] = true
if !strings.Contains(objType.PkgPath(), "openshift/origin") {
return
}
if internalTypesWithAllowedJsonTags.Has(objType.Name()) {
return
}
for i := 0; i < objType.NumField(); i++ {
structField := objType.FieldByIndex([]int{i})
jsonTag := structField.Tag.Get("json")
if len(jsonTag) != 0 {
t.Errorf("%v.%v should not have a json tag", objType, structField.Name)
}
switch structField.Type.Kind() {
case reflect.Struct:
checkJsonTags(structField.Type, seen, t)
}
}
}
示例15: checkDescriptions
func checkDescriptions(objType reflect.Type, seen *map[reflect.Type]bool, t *testing.T) {
if _, exists := (*seen)[objType]; exists {
return
}
(*seen)[objType] = true
if !strings.Contains(objType.PkgPath(), "openshift/origin") {
return
}
for i := 0; i < objType.NumField(); i++ {
structField := objType.FieldByIndex([]int{i})
// these fields don't need descriptions
if structField.Name == "TypeMeta" || structField.Name == "ObjectMeta" || structField.Name == "ListMeta" {
continue
}
if structField.Type == reflect.TypeOf(unversioned.Time{}) || structField.Type == reflect.TypeOf(time.Time{}) || structField.Type == reflect.TypeOf(runtime.RawExtension{}) {
continue
}
descriptionTag := structField.Tag.Get("description")
if len(descriptionTag) == 0 {
t.Errorf("%v", structField.Tag)
t.Errorf("%v.%v does not have a description", objType, structField.Name)
}
switch structField.Type.Kind() {
case reflect.Struct:
checkDescriptions(structField.Type, seen, t)
}
}
}