本文整理匯總了Golang中github.com/fatih/structs.Field.Set方法的典型用法代碼示例。如果您正苦於以下問題:Golang Field.Set方法的具體用法?Golang Field.Set怎麽用?Golang Field.Set使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/fatih/structs.Field
的用法示例。
在下文中一共展示了Field.Set方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: fieldSet
// fieldSet sets field value from the given string value. It converts the
// string value in a sane way and is usefulf or environment variables or flags
// which are by nature in string types.
func fieldSet(field *structs.Field, v string) error {
// TODO: add support for other types
switch field.Kind() {
case reflect.Bool:
val, err := strconv.ParseBool(v)
if err != nil {
return err
}
if err := field.Set(val); err != nil {
return err
}
case reflect.Int:
i, err := strconv.Atoi(v)
if err != nil {
return err
}
if err := field.Set(i); err != nil {
return err
}
case reflect.String:
field.Set(v)
case reflect.Slice:
// TODO add other typed slice support
if _, ok := field.Value().([]string); !ok {
return errors.New("can't set on non string slices")
}
if err := field.Set(strings.Split(v, ",")); err != nil {
return err
}
case reflect.Float64:
f, err := strconv.ParseFloat(v, 64)
if err != nil {
return err
}
if err := field.Set(f); err != nil {
return err
}
default:
return fmt.Errorf("multiconfig: not supported type: %s", field.Kind())
}
return nil
}
示例2: 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
}
示例3: fieldSet
// fieldSet sets field value from the given string value. It converts the
// string value in a sane way and is usefulf or environment variables or flags
// which are by nature in string types.
func fieldSet(field *structs.Field, v string) error {
// TODO: add support for other types
switch field.Kind() {
case reflect.Bool:
val, err := strconv.ParseBool(v)
if err != nil {
return err
}
if err := field.Set(val); err != nil {
return err
}
case reflect.Int:
i, err := strconv.Atoi(v)
if err != nil {
return err
}
if field.RawValue.Type().Name() != "int" {
// We're probably dealing with an enum type
field.RawValue.SetInt(int64(i))
return nil
}
if err := field.Set(i); err != nil {
return err
}
case reflect.String:
if field.RawValue.Type().Name() != "string" {
// We're probably dealing with an enum type
field.RawValue.SetString(v)
return nil
}
if err := field.Set(v); err != nil {
return err
}
case reflect.Slice:
switch t := field.Value().(type) {
case []string:
if err := field.Set(strings.Split(v, ",")); err != nil {
return err
}
case []int:
var list []int
for _, in := range strings.Split(v, ",") {
i, err := strconv.Atoi(in)
if err != nil {
return err
}
list = append(list, i)
}
if err := field.Set(list); err != nil {
return err
}
default:
return fmt.Errorf("multiconfig: field '%s' of type slice is unsupported: %s (%T)",
field.Name(), field.Kind(), t)
}
case reflect.Float64:
f, err := strconv.ParseFloat(v, 64)
if err != nil {
return err
}
if err := field.Set(f); err != nil {
return err
}
case reflect.Int64:
switch t := field.Value().(type) {
case time.Duration:
d, err := time.ParseDuration(v)
if err != nil {
return err
}
if err := field.Set(d); err != nil {
return err
}
case int64:
p, err := strconv.ParseInt(v, 10, 0)
if err != nil {
return err
}
if err := field.Set(p); err != nil {
return err
}
default:
return fmt.Errorf("multiconfig: field '%s' of type int64 is unsupported: %s (%T)",
field.Name(), field.Kind(), t)
}
default:
return fmt.Errorf("multiconfig: field '%s' has unsupported type: %s", field.Name(), field.Kind())
//.........這裏部分代碼省略.........
示例4: fieldSet
// fieldSet sets field value from the given string value. It converts the
// string value in a sane way and is usefulf or environment variables or flags
// which are by nature in string types.
func fieldSet(field *structs.Field, v string) error {
switch field.Kind() {
case reflect.Bool:
val, err := strconv.ParseBool(v)
if err != nil {
return err
}
if err := field.Set(val); err != nil {
return err
}
case reflect.Int:
i, err := strconv.Atoi(v)
if err != nil {
return err
}
if err := field.Set(i); err != nil {
return err
}
case reflect.String:
if err := field.Set(v); err != nil {
return err
}
case reflect.Map:
switch t := field.Value().(type) {
case map[string]int:
si := make(map[string]int)
if err := json.Unmarshal([]byte(v), &si); err != nil {
return err
}
if err := field.Set(si); err != nil {
return err
}
case map[string]string:
ss := make(map[string]string)
if err := json.Unmarshal([]byte(v), &ss); err != nil {
return err
}
if err := field.Set(ss); err != nil {
return err
}
default:
return fmt.Errorf("config: field '%s' of type map is unsupported: %s (%T)",
field.Name(), field.Kind(), t)
}
case reflect.Slice:
switch t := field.Value().(type) {
case []string:
if err := field.Set(strings.Split(v, ",")); err != nil {
return err
}
case []int:
var list []int
for _, in := range strings.Split(v, ",") {
i, err := strconv.Atoi(in)
if err != nil {
return err
}
list = append(list, i)
}
if err := field.Set(list); err != nil {
return err
}
case []int64:
var list []int64
for _, in := range strings.Split(v, ",") {
i, err := strconv.ParseInt(in, 10, 64)
if err != nil {
return err
}
list = append(list, i)
}
if err := field.Set(list); err != nil {
return err
}
case []float64:
var list []float64
for _, in := range strings.Split(v, ",") {
i, err := strconv.ParseFloat(in, 64)
if err != nil {
return err
}
list = append(list, i)
}
if err := field.Set(list); err != nil {
return err
}
default:
return fmt.Errorf("config: field '%s' of type slice is unsupported: %s (%T)",
field.Name(), field.Kind(), t)
//.........這裏部分代碼省略.........
示例5: fieldSet
// fieldSet sets field value from the given string value. It converts the
// string value in a sane way and is usefulf or environment variables or flags
// which are by nature in string types.
func fieldSet(field *structs.Field, v string) error {
switch f := field.Value().(type) {
case flag.Value:
if v := reflect.ValueOf(field.Value()); v.IsNil() {
typ := v.Type()
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
if err := field.Set(reflect.New(typ).Interface()); err != nil {
return err
}
f = field.Value().(flag.Value)
}
return f.Set(v)
}
// TODO: add support for other types
switch field.Kind() {
case reflect.Bool:
val, err := strconv.ParseBool(v)
if err != nil {
return err
}
if err := field.Set(val); err != nil {
return err
}
case reflect.Int:
i, err := strconv.Atoi(v)
if err != nil {
return err
}
if err := field.Set(i); err != nil {
return err
}
case reflect.String:
if err := field.Set(v); err != nil {
return err
}
case reflect.Slice:
switch t := field.Value().(type) {
case []string:
if err := field.Set(strings.Split(v, ",")); err != nil {
return err
}
case []int:
var list []int
for _, in := range strings.Split(v, ",") {
i, err := strconv.Atoi(in)
if err != nil {
return err
}
list = append(list, i)
}
if err := field.Set(list); err != nil {
return err
}
default:
return fmt.Errorf("multiconfig: field '%s' of type slice is unsupported: %s (%T)",
field.Name(), field.Kind(), t)
}
case reflect.Float64:
f, err := strconv.ParseFloat(v, 64)
if err != nil {
return err
}
if err := field.Set(f); err != nil {
return err
}
case reflect.Int64:
switch t := field.Value().(type) {
case time.Duration:
d, err := time.ParseDuration(v)
if err != nil {
return err
}
if err := field.Set(d); err != nil {
return err
}
case int64:
p, err := strconv.ParseInt(v, 10, 0)
if err != nil {
return err
}
if err := field.Set(p); err != nil {
return err
}
default:
//.........這裏部分代碼省略.........