package statsapi import ( "encoding/json" "errors" "log" "strconv" "strings" ) type FeedParams struct { GamePk string } type FeedResponse map[string]any type Play struct { Result result About about AtBatIndex int } type result struct { Event string Description string RBI int AwayScore int HomeScore int } type about struct { AtBatIndex json.Number IsTopInning bool Inning json.Number 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 setValue(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 case []any: idx, err := strconv.Atoi(key) if err != nil { break } v = expand(v, idx) v[idx] = value default: err = errors.New("couldn't determine type") } return err } func patch(obj any, path string, value any) error { var err error first, rest, found := strings.Cut(path, "/") log.Println(first, rest) if !found { err = setValue(obj, first, value) return err } switch v := obj.(type) { case *FeedResponse: err = patch(map[string]any(*v), rest, value) case map[string]any: err = patch(v[first], rest, value) case []any: idx, err := strconv.Atoi(first) if err != nil { break } v = expand(v, idx) err = patch(v[idx], rest, value) default: err = errors.New("couldn't determine type") } return err } func (f *FeedResponse) Patch(instr *instruction) { // TODO: need to handle each type of instruction separately: add, // replace, remove, copy, move log.Println("updating", instr.Path, "to", instr.Value) err := patch(f, instr.Path, instr.Value) if err != nil { log.Println(err) } }