MCPcopy Create free account
hub / github.com/chainloop-dev/chainloop / ParseKeyValOpts

Function ParseKeyValOpts

app/cli/cmd/registered_integration_add.go:107–152  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

105// 1 - Split the options into key/value pairs
106// 2 - Cast the values to the expected type defined in the schema
107func 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}

Callers 1

ParseAndValidateOptsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected