rewrite expand function
This commit is contained in:
		
							parent
							
								
									babc02905d
								
							
						
					
					
						commit
						90ba6e6066
					
				| 
						 | 
				
			
			@ -36,6 +36,12 @@ type about struct {
 | 
			
		|||
	CaptivatingIndex json.Number
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func expand(arr []any, idx int) []any {
 | 
			
		||||
	front := append(arr[:idx], nil)
 | 
			
		||||
	all := append(front, arr[idx:])
 | 
			
		||||
	return all
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func setValue(obj any, key string, value any) error {
 | 
			
		||||
	var err error
 | 
			
		||||
	switch v := obj.(type) {
 | 
			
		||||
| 
						 | 
				
			
			@ -51,13 +57,7 @@ func setValue(obj any, key string, value any) error {
 | 
			
		|||
			break
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if len(v) <= idx {
 | 
			
		||||
			arr := make([]any, idx+1)
 | 
			
		||||
			for i, val := range v {
 | 
			
		||||
				arr[i] = val
 | 
			
		||||
			}
 | 
			
		||||
			v = arr
 | 
			
		||||
		}
 | 
			
		||||
		v = expand(v, idx)
 | 
			
		||||
		v[idx] = value
 | 
			
		||||
	default:
 | 
			
		||||
		err = errors.New("couldn't determine type")
 | 
			
		||||
| 
						 | 
				
			
			@ -66,16 +66,6 @@ func setValue(obj any, key string, value any) error {
 | 
			
		|||
	return err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func expand(v []any, idx int) {
 | 
			
		||||
	if len(v) <= idx {
 | 
			
		||||
		arr := make([]any, idx+1)
 | 
			
		||||
		for i, val := range v {
 | 
			
		||||
			arr[i] = val
 | 
			
		||||
		}
 | 
			
		||||
		v = arr
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func patch(obj any, path string, value any) error {
 | 
			
		||||
	var err error
 | 
			
		||||
	first, rest, found := strings.Cut(path, "/")
 | 
			
		||||
| 
						 | 
				
			
			@ -94,7 +84,7 @@ func patch(obj any, path string, value any) error {
 | 
			
		|||
			break
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		expand(v, idx)
 | 
			
		||||
		v = expand(v, idx)
 | 
			
		||||
		err = patch(v[idx], rest, value)
 | 
			
		||||
	default:
 | 
			
		||||
		err = errors.New("couldn't determine type")
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue