104 lines
1.6 KiB
Go
104 lines
1.6 KiB
Go
package statsapi
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"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 patch(obj any, path string, value any) error {
|
|
if string(path[0]) == "/" {
|
|
path = path[1:]
|
|
}
|
|
|
|
var err error
|
|
first, rest, found := strings.Cut(path, "/")
|
|
if !found {
|
|
switch v := obj.(type) {
|
|
case *FeedResponse:
|
|
(*v)[first] = value
|
|
case map[string]any:
|
|
v[first] = value
|
|
case []any:
|
|
idx, err := strconv.Atoi(first)
|
|
if err != nil {
|
|
break
|
|
}
|
|
|
|
if len(v) <= idx {
|
|
arr := make([]any, idx+1)
|
|
for i, val := range v {
|
|
arr[i] = val
|
|
}
|
|
v = arr
|
|
}
|
|
v[idx] = value
|
|
default:
|
|
err = errors.New("couldn't determine type")
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
switch v := obj.(type) {
|
|
case *FeedResponse:
|
|
err = patch((*v)[first], rest, value)
|
|
case map[string]any:
|
|
err = patch(v[first], rest, value)
|
|
case []any:
|
|
idx, err := strconv.Atoi(first)
|
|
if err != nil {
|
|
break
|
|
}
|
|
|
|
if len(v) <= idx {
|
|
arr := make([]any, idx+1)
|
|
for i, val := range v {
|
|
arr[i] = val
|
|
}
|
|
v = arr
|
|
}
|
|
|
|
err = patch(v[idx], rest, value)
|
|
default:
|
|
err = errors.New("couldn't determine type")
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
func (f *FeedResponse) Patch(instr *instruction) {
|
|
err := patch(f, instr.Path, instr.Value)
|
|
if err != nil {
|
|
}
|
|
}
|