|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding" |
| 5 | + "fmt" |
| 6 | + "reflect" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +// UnmarshallableBool typedef for builtin bool |
| 12 | +// because builtin type's methods can't be declared |
| 13 | +type UnmarshallableBool bool |
| 14 | + |
| 15 | +// UnmarshalText implements the encoding.TextUnmarshaler interface. |
| 16 | +// Returns boolean true if the string is "1" or "[Tt]rue" |
| 17 | +// Returns boolean false if the string is "0" or "[Ff]alse" |
| 18 | +func (b *UnmarshallableBool) UnmarshalText(data []byte) error { |
| 19 | + s := strings.ToLower(string(data)) |
| 20 | + switch s { |
| 21 | + case "1", "true": |
| 22 | + *b = true |
| 23 | + case "0", "false": |
| 24 | + *b = false |
| 25 | + default: |
| 26 | + return fmt.Errorf("Boolean unmarshal error: invalid input %s", s) |
| 27 | + } |
| 28 | + return nil |
| 29 | +} |
| 30 | + |
| 31 | +// UnmarshallableString typedef for builtin string |
| 32 | +type UnmarshallableString string |
| 33 | + |
| 34 | +// UnmarshalText implements the encoding.TextUnmarshaler interface. |
| 35 | +// Returns the string |
| 36 | +func (s *UnmarshallableString) UnmarshalText(data []byte) error { |
| 37 | + *s = UnmarshallableString(data) |
| 38 | + return nil |
| 39 | +} |
| 40 | + |
| 41 | +// CommonArgs contains the IgnoreUnknown argument |
| 42 | +// and must be embedded by all Arg structs |
| 43 | +type CommonArgs struct { |
| 44 | + IgnoreUnknown UnmarshallableBool `json:"ignoreunknown,omitempty"` |
| 45 | +} |
| 46 | + |
| 47 | +// GetKeyField is a helper function to receive Values |
| 48 | +// Values that represent a pointer to a struct |
| 49 | +func GetKeyField(keyString string, v reflect.Value) reflect.Value { |
| 50 | + return v.Elem().FieldByName(keyString) |
| 51 | +} |
| 52 | + |
| 53 | +// LoadSriovArgs parses args from a string in the form "K=V;K2=V2;..." |
| 54 | +func LoadSriovArgs(args string, container interface{}) error { |
| 55 | + if args == "" { |
| 56 | + return nil |
| 57 | + } |
| 58 | + |
| 59 | + containerValue := reflect.ValueOf(container) |
| 60 | + |
| 61 | + pairs := strings.Split(args, ";") |
| 62 | + unknownArgs := []string{} |
| 63 | + for _, pair := range pairs { |
| 64 | + kv := strings.Split(pair, "=") |
| 65 | + if len(kv) != 2 { |
| 66 | + return fmt.Errorf("ARGS: invalid pair %q", pair) |
| 67 | + } |
| 68 | + keyString := kv[0] |
| 69 | + valueString := kv[1] |
| 70 | + keyField := GetKeyField(keyString, containerValue) |
| 71 | + if !keyField.IsValid() { |
| 72 | + unknownArgs = append(unknownArgs, pair) |
| 73 | + continue |
| 74 | + } |
| 75 | + |
| 76 | + switch keyField.Kind() { |
| 77 | + case reflect.Int: |
| 78 | + vi, err := strconv.Atoi(valueString) |
| 79 | + if err != nil { |
| 80 | + return fmt.Errorf("Convert value %s for field %s failed", valueString, keyString) |
| 81 | + } |
| 82 | + keyField.SetInt(int64(vi)) |
| 83 | + case reflect.String: |
| 84 | + keyField.SetString(valueString) |
| 85 | + case reflect.Slice: |
| 86 | + u := keyField.Addr().Interface().(encoding.TextUnmarshaler) |
| 87 | + err := u.UnmarshalText([]byte(valueString)) |
| 88 | + if err != nil { |
| 89 | + return fmt.Errorf("ARGS: error parsing value of pair %q: %v)", pair, err) |
| 90 | + } |
| 91 | + default: |
| 92 | + return fmt.Errorf("Unsupport type %s for %s", keyField.Kind().String(), keyString) |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + isIgnoreUnknown := GetKeyField("IgnoreUnknown", containerValue).Bool() |
| 97 | + if len(unknownArgs) > 0 && !isIgnoreUnknown { |
| 98 | + return fmt.Errorf("ARGS: unknown args %q", unknownArgs) |
| 99 | + } |
| 100 | + return nil |
| 101 | +} |
0 commit comments