ParseKeyValOpts performs two steps 1 - Split the options into key/value pairs 2 - Cast the values to the expected type defined in the schema
(opts []string, propertiesMap sdk.SchemaPropertiesMap)
| 105 | // 1 - Split the options into key/value pairs |
| 106 | // 2 - Cast the values to the expected type defined in the schema |
| 107 | func ParseKeyValOpts(opts []string, propertiesMap sdk.SchemaPropertiesMap) (map[string]any, error) { |
| 108 | // 1 - Split the options into key/value pairs |
| 109 | var options = make(map[string]any) |
| 110 | for _, opt := range opts { |
| 111 | kv := strings.SplitN(opt, "=", 2) |
| 112 | if len(kv) != 2 { |
| 113 | return nil, fmt.Errorf("invalid option %q, the expected format is key=value", opt) |
| 114 | } |
| 115 | options[kv[0]] = kv[1] |
| 116 | } |
| 117 | |
| 118 | // 2 - Cast the values to the expected type defined in the schema |
| 119 | for k, v := range options { |
| 120 | prop, ok := propertiesMap[k] |
| 121 | if !ok { |
| 122 | continue |
| 123 | } |
| 124 | |
| 125 | switch prop.Type { |
| 126 | case "string": |
| 127 | options[k] = v.(string) |
| 128 | case "integer": |
| 129 | nv, err := strconv.Atoi(v.(string)) |
| 130 | if err != nil { |
| 131 | return nil, fmt.Errorf("invalid option %q, the expected format is %q", v, prop.Type) |
| 132 | } |
| 133 | |
| 134 | options[k] = nv |
| 135 | case "number": |
| 136 | nv, err := strconv.ParseFloat(v.(string), 32) |
| 137 | if err != nil { |
| 138 | return nil, fmt.Errorf("invalid option %q, the expected format is %q", v, prop.Type) |
| 139 | } |
| 140 | |
| 141 | options[k] = nv |
| 142 | case "boolean": |
| 143 | nv, err := strconv.ParseBool(v.(string)) |
| 144 | if err != nil { |
| 145 | return nil, fmt.Errorf("invalid option %q, the expected format is %q", v, prop.Type) |
| 146 | } |
| 147 | options[k] = nv |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | return options, nil |
| 152 | } |
no outgoing calls
no test coverage detected