test patching feed using diffpatch
This commit is contained in:
		
							parent
							
								
									b7aa862cf5
								
							
						
					
					
						commit
						9ebb5bf610
					
				| 
						 | 
				
			
			@ -2,47 +2,16 @@ package statsapi
 | 
			
		|||
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"errors"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"strings"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type FeedParams struct {
 | 
			
		||||
	GamePk string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type FeedResponse struct {
 | 
			
		||||
	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 FeedResponse map[string]any
 | 
			
		||||
 | 
			
		||||
type Play struct {
 | 
			
		||||
	Result     result
 | 
			
		||||
| 
						 | 
				
			
			@ -66,20 +35,59 @@ type about struct {
 | 
			
		|||
	CaptivatingIndex json.Number
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type linescore struct {
 | 
			
		||||
	Teams teamLines
 | 
			
		||||
func patch(obj any, path string, value any) error {
 | 
			
		||||
	if string(path[0]) == "/" {
 | 
			
		||||
		path = path[1:]
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
type teamLines struct {
 | 
			
		||||
	Home teamline
 | 
			
		||||
	Away teamline
 | 
			
		||||
	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
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
type teamline struct {
 | 
			
		||||
	Runs       json.Number
 | 
			
		||||
	Hits       json.Number
 | 
			
		||||
	Errors     json.Number
 | 
			
		||||
	LeftOnBase json.Number
 | 
			
		||||
			if idx < len(v) {
 | 
			
		||||
				v[idx] = value
 | 
			
		||||
			} else {
 | 
			
		||||
				v = append(v, 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
 | 
			
		||||
		}
 | 
			
		||||
		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) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										27
									
								
								main.go
								
								
								
								
							
							
						
						
									
										27
									
								
								main.go
								
								
								
								
							| 
						 | 
				
			
			@ -59,17 +59,34 @@ func main() {
 | 
			
		|||
		log.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ts := feed.MetaData.TimeStamp
 | 
			
		||||
	game, err := InitGame(feed)
 | 
			
		||||
	j, err := json.Marshal(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 {
 | 
			
		||||
		log.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for {
 | 
			
		||||
		var p statsapi.Push
 | 
			
		||||
		err := ws.ReadJSON(&p)
 | 
			
		||||
	patches, err := statsapi.DiffPatch(gamePk, ts, p.UpdateId)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		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