MCPcopy Create free account
hub / github.com/devspace-sh/devspace / setIndex

Function setIndex

pkg/util/strvals/strvals.go:268–297  ·  view source on GitHub ↗
(list []interface{}, index int, val interface{})

Source from the content-addressed store, hash-verified

266}
267
268func setIndex(list []interface{}, index int, val interface{}) (l2 []interface{}, err error) {
269 // There are possible index values that are out of range on a target system
270 // causing a panic. This will catch the panic and return an error instead.
271 // The value of the index that causes a panic varies from system to system.
272 defer func() {
273 if r := recover(); r != nil {
274 err = fmt.Errorf("error processing index %d: %s", index, r)
275 }
276 }()
277
278 if index < 0 {
279 return list, fmt.Errorf("negative %d index not allowed", index)
280 }
281 if len(list) <= index {
282 newlist := make([]interface{}, index+1)
283 copy(newlist, list)
284 list = newlist
285 }
286 switch reflect.TypeOf(val).Kind() {
287 case reflect.String:
288 if len(val.(string)) > 0 {
289 list[index] = val
290 } else {
291 list[index] = nil
292 }
293 default:
294 list[index] = val
295 }
296 return list, nil
297}
298
299func (t *parser) keyIndex() (int, error) {
300 // First, get the key.

Callers 1

listItemMethod · 0.85

Calls 1

ErrorfMethod · 0.45

Tested by

no test coverage detected