當前位置: 首頁>>代碼示例>>Golang>>正文


Golang structs.Field類代碼示例

本文整理匯總了Golang中github.com/fatih/structs.Field的典型用法代碼示例。如果您正苦於以下問題:Golang Field類的具體用法?Golang Field怎麽用?Golang Field使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Field類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: processField

func (e *RequiredValidator) processField(fieldName string, field *structs.Field) error {
	fieldName += field.Name()
	switch field.Kind() {
	case reflect.Struct:
		// this is used for error messages below, when we have an error at the
		// child properties add parent properties into the error message as well
		fieldName += "."

		for _, f := range field.Fields() {
			if err := e.processField(fieldName, f); err != nil {
				return err
			}
		}
	default:
		val := field.Tag(e.RequiredTagName)
		if val != "true" && val != "1" {
			return nil
		}
		if field.IsZero() {
			return fmt.Errorf("Field '%s' is required", fieldName)
		}
	}

	return nil
}
開發者ID:heartsg,項目名稱:dasea,代碼行數:25,代碼來源:validator.go

示例2: processField

// processField gets tagName and the field, recursively checks if the field has the given
// tag, if yes, sets it otherwise ignores
func (t *TagLoader) processField(tagName string, field *structs.Field) error {
	switch {
	case field.Kind() == reflect.Struct && !implementsTextUnmarshaler(field):
		for _, f := range field.Fields() {
			if err := t.processField(tagName, f); err != nil {
				return err
			}
		}
	case field.Kind() == reflect.Ptr:
		field.InitElem()
		return t.processField(tagName, field)
	default:
		defaultVal := field.Tag(t.DefaultTagName)
		if defaultVal == "" {
			return nil
		}

		err := fieldSet(field, defaultVal)
		if err != nil {
			return err
		}
	}

	return nil
}
開發者ID:ilgooz,項目名稱:multiconfig,代碼行數:27,代碼來源:tag.go

示例3: setField

func (i *INILoader) setField(field *structs.Field, c *goconfig.ConfigFile, section string) error {
	//first process each subfield of a struct field
	switch field.Kind() {
	case reflect.Struct:
		for _, f := range field.Fields() {
			var subsection string
			if section == "" {
				subsection = field.Name()
			} else {
				subsection = section + "." + field.Name()
			}
			if err := i.setField(f, c, subsection); err != nil {
				return err
			}
		}
	default:
		v, err := c.GetValue(section, field.Name())
		if err == nil && v != "" {
			err := fieldSet(field, v)
			if err != nil {
				return err
			}
		}
	}

	return nil
}
開發者ID:heartsg,項目名稱:dasea,代碼行數:27,代碼來源:file.go

示例4: validateSelection

func validateSelection(field *structs.Field, selection string) error {
	selectionSet := strings.Split(selection, "|")
	switch field.Kind() {
	case reflect.Int:
		i := field.Value().(int)
		s := strconv.Itoa(i)
		for _, selection := range selectionSet {
			if s == selection {
				return nil
			}
		}
		return fmt.Errorf("validate: Field %s value %s, not in selection %s.", field.Name(), s, selection)
	case reflect.String:
		s := field.Value().(string)
		for _, selection := range selectionSet {
			if s == selection {
				return nil
			}
		}
		return fmt.Errorf("validate: Field %s value %s, not in selection %s.", field.Name(), s, selection)
	default:
		return nil
	}
	return nil
}
開發者ID:heartsg,項目名稱:dasea,代碼行數:25,代碼來源:reflect.go

示例5: setFieldValue

// setFieldValue will try to set the value of a given field to v, which is
// given as string and converted to the field type as required.
func setFieldValue(field *structs.Field, v string) error {
	switch field.Kind() {
	case reflect.String:
		err := field.Set(v)
		if err != nil {
			return err
		}
	case reflect.Int, reflect.Int64:
		i, err := strconv.Atoi(v)
		if err != nil {
			return err
		}
		err = field.Set(i)
		if err != nil {
			return err
		}
	case reflect.Float64:
		f, err := strconv.ParseFloat(v, 64)
		if err != nil {
			return err
		}
		err = field.Set(f)
		if err != nil {
			return err
		}
	default:
		return fmt.Errorf("cannot set field value for field kind %s", field.Kind())
	}
	return nil
}
開發者ID:miku,項目名稱:lynd,代碼行數:32,代碼來源:task.go

示例6: processField

// processField gets leading name for the env variable and combines the current
// field's name and generates environment variable names recursively
func (e *EnvironmentLoader) processField(prefix string, field *structs.Field, name string, strctMap interface{}) error {
	fieldName := e.generateFieldName(prefix, name)

	switch strctMap.(type) {
	case map[string]interface{}:
		for key, val := range strctMap.(map[string]interface{}) {
			field := field.Field(key)

			if err := e.processField(fieldName, field, key, val); err != nil {
				return err
			}
		}
	default:
		v := os.Getenv(fieldName)
		if v == "" {
			return nil
		}

		if err := fieldSet(field, v); err != nil {
			return err
		}
	}

	return nil
}
開發者ID:koding,項目名稱:multiconfig,代碼行數:27,代碼來源:env.go

示例7: implementsTextUnmarshaler

func implementsTextUnmarshaler(field *structs.Field) bool {
	t := field.Type()
	if t.Kind() != reflect.Ptr {
		t = reflect.PtrTo(t)
	}
	return t.Implements(textUnmarshalerType)
}
開發者ID:ilgooz,項目名稱:multiconfig,代碼行數:7,代碼來源:multiconfig.go

示例8: generateFieldName

// generateFieldName generates the fiels name combined with the prefix and the
// struct's field name
func (e *EnvironmentLoader) generateFieldName(prefix string, field *structs.Field) string {
	fieldName := strings.ToUpper(field.Name())
	if e.CamelCase {
		fieldName = strings.ToUpper(strings.Join(camelcase.Split(field.Name()), "_"))
	}

	return strings.ToUpper(prefix) + "_" + fieldName
}
開發者ID:heartsg,項目名稱:dasea,代碼行數:10,代碼來源:env.go

示例9: getBsonName

func getBsonName(field *structs.Field) string {
	tag := field.Tag("bson")
	if tag == "" || tag == "-" {
		return ""
	}
	tags := strings.Split(tag, ",")
	return tags[0]
}
開發者ID:iwarsong,項目名稱:bearded,代碼行數:8,代碼來源:fltr.go

示例10: parseValue

func parseValue(field *structs.Field, val string) (interface{}, error) {
	switch field.Kind() {
	case reflect.Int:
		v, err := strconv.Atoi(val)
		if err != nil {
			return nil, err
		}
		return v, nil
	case reflect.Float64:
		v, err := strconv.ParseFloat(val, 64)
		if err != nil {
			return nil, err
		}
		return v, nil
	case reflect.Bool:
		v, err := strconv.ParseBool(val)
		if err != nil {
			return nil, err
		}
		return v, nil
	default:
		fieldVal := field.Value()

		if _, casted := fieldVal.(bson.ObjectId); casted {
			if bson.IsObjectIdHex(val) {
				return bson.ObjectIdHex(val), nil
			} else {
				return nil, fmt.Errorf("should be bson.ObjectId hex")
			}
		}
		if _, casted := (fieldVal).(time.Time); casted {
			v := &time.Time{}
			return v, v.UnmarshalText([]byte(val))
		}
		if convertable, casted := fieldVal.(Converter); casted {
			converted, err := convertable.Convert(val)
			if err != nil {
				return nil, err
			}
			if enum, casted := fieldVal.(Enumer); casted {
				enumValues := enum.Enum()
				for _, eV := range enumValues {
					if eV == converted {
						return converted, nil
					}
				}
				return nil, fmt.Errorf("should be one of %v", enumValues)
			}
			return converted, nil
		}
		return val, nil
	}

}
開發者ID:iwarsong,項目名稱:bearded,代碼行數:54,代碼來源:fltr.go

示例11: printField

// printField prints the field of the config struct for the flag.Usage
func (e *EnvironmentLoader) printField(prefix string, field *structs.Field) {
	fieldName := e.generateFieldName(prefix, field)

	switch field.Kind() {
	case reflect.Struct:
		for _, f := range field.Fields() {
			e.printField(fieldName, f)
		}
	default:
		fmt.Println("  ", fieldName)
	}
}
開發者ID:heartsg,項目名稱:dasea,代碼行數:13,代碼來源:env.go

示例12: processField

// processField gets leading name for the env variable and combines the current
// field's name and generates environemnt variable names recursively
func (e *EnvironmentLoader) processField(prefix string, field *structs.Field) error {
	fieldName := e.generateFieldName(prefix, field)

	switch {
	case field.Kind() == reflect.Struct && !implementsTextUnmarshaler(field):
		for _, f := range field.Fields() {
			if err := e.processField(fieldName, f); err != nil {
				return err
			}
		}
	case field.Kind() == reflect.Ptr:
		field.InitElem()
		return e.processField(prefix, field)
	default:
		v := os.Getenv(fieldName)
		if v == "" {
			return nil
		}

		if err := fieldSet(field, v); err != nil {
			return err
		}
	}

	return nil
}
開發者ID:ilgooz,項目名稱:multiconfig,代碼行數:28,代碼來源:env.go

示例13: keyFromField

func (b *Builder) keyFromField(f *structs.Field) string {
	tag := f.Tag(b.Tag)

	if i := strings.IndexRune(tag, ','); i != -1 {
		tag = tag[:i]
	}

	switch tag {
	case "-":
		return ""
	case "":
		return b.fieldFunc(f.Name())
	}

	return tag
}
開發者ID:koding,項目名稱:koding,代碼行數:16,代碼來源:object.go

示例14: processField

// processField generates a flag based on the given field and fieldName. If a
// nested struct is detected, a flag for each field of that nested struct is
// generated too.
func (f *FlagLoader) processField(flagSet *flag.FlagSet, fieldName string, field *structs.Field) error {
	if !field.IsExported() {
		return nil
	}

	if f.CamelCase {
		fieldName = strings.Join(camelcase.Split(fieldName), "-")
	}

	switch {
	case field.Kind() == reflect.Struct && !implementsTextUnmarshaler(field):
		for _, ff := range field.Fields() {
			flagName := fieldName + "-" + ff.Name()

			if f.Flatten {
				// first check if it's set or not, because if we have duplicate
				// we don't want to break the flag. Panic by giving a readable
				// output
				flagSet.VisitAll(func(fl *flag.Flag) {
					if strings.ToLower(ff.Name()) == fl.Name {
						// already defined
						panic(fmt.Sprintf("flag '%s' is already defined in outer struct", fl.Name))
					}
				})

				flagName = ff.Name()
			}

			if err := f.processField(flagSet, flagName, ff); err != nil {
				return err
			}
		}
	case field.Kind() == reflect.Ptr:
		field.InitElem()
		return f.processField(flagSet, fieldName, field)
	default:
		// Add custom prefix to the flag if it's set
		if f.Prefix != "" {
			fieldName = f.Prefix + "-" + fieldName
		}

		flagSet.Var(newFieldValue(field), flagName(fieldName), flagUsage(fieldName))
	}

	return nil
}
開發者ID:ilgooz,項目名稱:multiconfig,代碼行數:49,代碼來源:flag.go

示例15: processField

// processField generates a flag based on the given field and fieldName. If a
// nested struct is detected, a flag for each field of that nested struct is
// generated too.
func (f *FlagLoader) processField(flagSet *flag.FlagSet, fieldName string, field *structs.Field) error {
	if f.CamelCase {
		fieldName = strings.Join(camelcase.Split(fieldName), "-")
	}

	switch field.Kind() {
	case reflect.Struct:
		for _, ff := range field.Fields() {
			flagName := field.Name() + "-" + ff.Name()

			if f.Flatten {
				// first check if it's set or not, because if we have duplicate
				// we don't want to break the flag. Panic by giving a readable
				// output
				flagSet.VisitAll(func(fl *flag.Flag) {
					if strings.ToLower(ff.Name()) == fl.Name {
						// already defined
						panic(fmt.Sprintf("flag '%s' is already defined in outer struct", fl.Name))
					}
				})

				flagName = ff.Name()
			}

			if err := f.processField(flagSet, flagName, ff); err != nil {
				return err
			}
		}
	default:
		// First see tag, if exists, use tag instead of fieldname
		flagTag := field.Tag(f.FlagTagName)
		if flagTag != "" {
			fieldName = flagTag
		} else if f.Prefix != "" {
			fieldName = f.Prefix + "-" + fieldName
		}

		// we only can get the value from expored fields, unexported fields panics
		if field.IsExported() {
			flagSet.Var(newFieldValue(field), flagName(fieldName), flagUsage(fieldName))
		}
	}

	return nil
}
開發者ID:heartsg,項目名稱:dasea,代碼行數:48,代碼來源:flag.go


注:本文中的github.com/fatih/structs.Field類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。