test patching feed using diffpatch
This commit is contained in:
parent
b7aa862cf5
commit
9ebb5bf610
|
@ -2,47 +2,16 @@ package statsapi
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FeedParams struct {
|
type FeedParams struct {
|
||||||
GamePk string
|
GamePk string
|
||||||
}
|
}
|
||||||
|
|
||||||
type FeedResponse struct {
|
type FeedResponse map[string]any
|
||||||
GamePk string
|
|
||||||
MetaData metadata
|
|
||||||
GameData gamedata
|
|
||||||
LiveData livedata
|
|
||||||
}
|
|
||||||
|
|
||||||
type metadata struct {
|
|
||||||
TimeStamp string
|
|
||||||
}
|
|
||||||
|
|
||||||
type gamedata struct {
|
|
||||||
Teams teams
|
|
||||||
}
|
|
||||||
|
|
||||||
type teams struct {
|
|
||||||
Away team
|
|
||||||
Home team
|
|
||||||
}
|
|
||||||
|
|
||||||
type team struct {
|
|
||||||
LocationName string
|
|
||||||
TeamName string
|
|
||||||
}
|
|
||||||
|
|
||||||
type livedata struct {
|
|
||||||
Plays plays
|
|
||||||
Linescore linescore
|
|
||||||
}
|
|
||||||
|
|
||||||
type plays struct {
|
|
||||||
AllPlays []Play
|
|
||||||
CurrentPlay Play
|
|
||||||
ScoringPlays []json.Number
|
|
||||||
}
|
|
||||||
|
|
||||||
type Play struct {
|
type Play struct {
|
||||||
Result result
|
Result result
|
||||||
|
@ -66,20 +35,59 @@ type about struct {
|
||||||
CaptivatingIndex json.Number
|
CaptivatingIndex json.Number
|
||||||
}
|
}
|
||||||
|
|
||||||
type linescore struct {
|
func patch(obj any, path string, value any) error {
|
||||||
Teams teamLines
|
if string(path[0]) == "/" {
|
||||||
|
path = path[1:]
|
||||||
}
|
}
|
||||||
|
|
||||||
type teamLines struct {
|
var err error
|
||||||
Home teamline
|
first, rest, found := strings.Cut(path, "/")
|
||||||
Away teamline
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
type teamline struct {
|
if idx < len(v) {
|
||||||
Runs json.Number
|
v[idx] = value
|
||||||
Hits json.Number
|
} else {
|
||||||
Errors json.Number
|
v = append(v, value)
|
||||||
LeftOnBase json.Number
|
}
|
||||||
|
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
|
||||||
|
}
|
||||||
|
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 {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Play) Patch(patch Patch) {
|
func (p *Play) Patch(patch Patch) {
|
||||||
|
|
27
main.go
27
main.go
|
@ -59,17 +59,34 @@ func main() {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
ts := feed.MetaData.TimeStamp
|
j, err := json.Marshal(feed)
|
||||||
game, err := InitGame(feed)
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
fmt.Println(string(j))
|
||||||
|
|
||||||
|
ts := feed["metaData"].(map[string]any)["timeStamp"].(string)
|
||||||
|
|
||||||
|
var p statsapi.Push
|
||||||
|
err = ws.ReadJSON(&p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for {
|
patches, err := statsapi.DiffPatch(gamePk, ts, p.UpdateId)
|
||||||
var p statsapi.Push
|
|
||||||
err := ws.ReadJSON(&p)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, patch := range patches {
|
||||||
|
for _, instr := range patch.Diff {
|
||||||
|
feed.Patch(&instr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
j, err = json.Marshal(feed)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
fmt.Println(string(j))
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue