remove my patch stuff
This commit is contained in:
		
							parent
							
								
									3a0a83e32e
								
							
						
					
					
						commit
						0a09ed2091
					
				|  | @ -35,120 +35,3 @@ type about struct { | |||
| 	IsScoringPlay    bool | ||||
| 	CaptivatingIndex json.Number | ||||
| } | ||||
| 
 | ||||
| func expand(arr []any, idx int) []any { | ||||
| 	var all []any | ||||
| 	if idx < len(arr) { | ||||
| 		all = arr | ||||
| 	} else { | ||||
| 		all = make([]any, idx+1) | ||||
| 		copy(all[:len(arr)], arr) | ||||
| 		all[idx] = make(map[string]any) | ||||
| 	} | ||||
| 	return all | ||||
| } | ||||
| 
 | ||||
| func setLeaf(obj any, key string, value any) error { | ||||
| 	var err error | ||||
| 	switch v := obj.(type) { | ||||
| 	case map[string]any: | ||||
| 		log.Println("Formerly", v[key]) | ||||
| 		v[key] = value | ||||
| 		log.Println("Now", v[key]) | ||||
| 	case []any: | ||||
| 		idx, err := strconv.Atoi(key) | ||||
| 		if err != nil { | ||||
| 			break | ||||
| 		} | ||||
| 
 | ||||
| 		v = expand(v, idx) | ||||
| 		log.Println("Formerly", v[idx]) | ||||
| 		v[idx] = value | ||||
| 		log.Println("Now", v[idx]) | ||||
| 	default: | ||||
| 		err = errors.New("couldn't determine type") | ||||
| 	} | ||||
| 
 | ||||
| 	return err | ||||
| } | ||||
| 
 | ||||
| func getLeaf(key string, obj any) (any, error) { | ||||
| 	switch v := obj.(type) { | ||||
| 	case map[string]any: | ||||
| 		val, ok := v[key] | ||||
| 		if !ok { | ||||
| 			return nil, errors.New("invalid key") | ||||
| 		} | ||||
| 		return val, nil | ||||
| 	case []any: | ||||
| 		idx, err := strconv.Atoi(key) | ||||
| 		if err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
| 
 | ||||
| 		if idx < 0 || idx >= len(v) { | ||||
| 			return nil, errors.New("invalid index") | ||||
| 		} | ||||
| 
 | ||||
| 		return v[idx], nil | ||||
| 	default: | ||||
| 		return nil, errors.New("couldn't determine type") | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func set(path string, obj any, value any) error { | ||||
| 	elems := strings.Split(path, "/") | ||||
| 	leaf := elems[len(elems)-1] | ||||
| 	leafPath := strings.Join(elems[:len(elems)-1], "/") | ||||
| 
 | ||||
| 	parent, err := get(leafPath, obj) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 
 | ||||
| 	err = setLeaf(parent, leaf, value) | ||||
| 	return err | ||||
| } | ||||
| 
 | ||||
| func get(path string, obj any) (any, error) { | ||||
| 	var err error | ||||
| 	first, rest, found := strings.Cut(path, "/") | ||||
| 	log.Println(first, rest) | ||||
| 	if !found { | ||||
| 		return getLeaf(first, obj) | ||||
| 	} else if first == "" { | ||||
| 		return get(rest, obj) | ||||
| 	} | ||||
| 
 | ||||
| 	switch v := obj.(type) { | ||||
| 	case map[string]any: | ||||
| 		val, ok := v[first] | ||||
| 		if !ok { | ||||
| 			return v, errors.New("invalid key") | ||||
| 		} | ||||
| 		return get(rest, val) | ||||
| 	case []any: | ||||
| 		idx, err := strconv.Atoi(first) | ||||
| 		if err != nil { | ||||
| 			break | ||||
| 		} | ||||
| 
 | ||||
| 		if idx < 0 || idx >= len(v) { | ||||
| 			return v, errors.New("invalid index") | ||||
| 		} | ||||
| 
 | ||||
| 		return get(rest, v[idx]) | ||||
| 	default: | ||||
| 		err = errors.New("couldn't determine type") | ||||
| 	} | ||||
| 
 | ||||
| 	return obj, err | ||||
| } | ||||
| 
 | ||||
| func (f *FeedResponse) Patch(instr *instruction) error { | ||||
| 	// TODO: need to handle each type of instruction separately: add,
 | ||||
| 	// replace, remove, copy, move
 | ||||
| 	log.Println("updating", instr.Path) | ||||
| 	err := set(instr.Path, map[string]any(*f), instr.Value) | ||||
| 	return err | ||||
| } | ||||
|  |  | |||
|  | @ -5,16 +5,3 @@ type DiffPatchParams struct { | |||
| 	StartTimecode string | ||||
| 	PushUpdateId  string | ||||
| } | ||||
| 
 | ||||
| type DiffPatchResponse []Patch | ||||
| 
 | ||||
| type Patch struct { | ||||
| 	Diff []instruction | ||||
| } | ||||
| 
 | ||||
| type instruction struct { | ||||
| 	Op    string | ||||
| 	Path  string | ||||
| 	Value any | ||||
| 	From  string | ||||
| } | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue