Skip to content

Commit

Permalink
fix: add missing UUIDs and fields on some resources (#152)
Browse files Browse the repository at this point in the history
* decode with mapstructure when debugging to find unused fields

* add mapstructure

* GOOBS_LOG=error when running func tests

* add uuids on resources missing them

* wow of course the protocol has undocumented fields it returns

---------

Co-authored-by: Andrey Kaipov <andreykaipov@users.noreply.github.com>
  • Loading branch information
andreykaipov and andreykaipov authored May 8, 2024
1 parent 286febd commit 980a85a
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 6 deletions.
32 changes: 30 additions & 2 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ package api
import (
"encoding/json"
"fmt"
"os"
"sync"
"time"

"github.com/andreykaipov/goobs/api/opcodes"
"github.com/andreykaipov/goobs/api/requests"
"github.com/mitchellh/mapstructure"
uuid "github.com/nu7hatch/gouuid"
)

Expand Down Expand Up @@ -137,9 +139,9 @@ func (c *Client) SendRequest(requestBody Params, responseBody Response) error {

responseBody.setRaw(data)

if err := json.Unmarshal(data, responseBody); err != nil {
if err := c.decodeResponse(data, responseBody); err != nil {
return fmt.Errorf(
"request %s: unmarshalling `%s` into type %T: %s",
"request %s: decoding `%s` into type %T: %s",
name,
data,
responseBody,
Expand All @@ -149,3 +151,29 @@ func (c *Client) SendRequest(requestBody Params, responseBody Response) error {

return nil
}

func (c *Client) decodeResponse(data json.RawMessage, responseBody Response) error {
// no need for mapstructure if we're not debugging since it's slower
if os.Getenv("GOOBS_LOG") == "" {
return json.Unmarshal(data, responseBody)
}

dataParsed := map[string]any{}
if err := json.Unmarshal(data, &dataParsed); err != nil {
return fmt.Errorf("unmarshalling `%s` into map: %s", data, err)
}

// decoding with mapstructure, specifically erroring on unused fields,
// will find fields on manually maintained structs that i've forgetten
// to update whenever obs-websocket updates its API
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
Result: responseBody,
ErrorUnused: true,
TagName: "json",
})
if err != nil {
return fmt.Errorf("creating decoder: %s", err)
}

return decoder.Decode(dataParsed)
}
2 changes: 2 additions & 0 deletions api/requests/record/xx_generated.togglerecordpause.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ func (o *ToggleRecordPauseParams) GetRequestName() string {
// Represents the response body for the ToggleRecordPause request.
type ToggleRecordPauseResponse struct {
_response

OutputPaused bool `json:"outputPaused,omitempty"`
}

// Toggles pause on the record output.
Expand Down
4 changes: 4 additions & 0 deletions api/typedefs/typedefs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package typedefs

type Input struct {
InputUuid string `json:"inputUuid"`
InputName string `json:"inputName"`
InputKind string `json:"inputKind"`
UnversionedInputKind string `json:"unversionedInputKind"`
Expand All @@ -24,6 +25,7 @@ type OutputFlags struct {
}

type Scene struct {
SceneUuid string `json:"sceneUuid"`
SceneIndex int `json:"sceneIndex"`
SceneName string `json:"sceneName"`
}
Expand All @@ -43,6 +45,7 @@ type Filter struct {
}

type Transition struct {
TransitionUuid string `json:"transitionUuid"`
TransitionConfigurable bool `json:"transitionConfigurable"`
TransitionFixed bool `json:"transitionFixed"`
TransitionKind string `json:"transitionKind"`
Expand All @@ -63,6 +66,7 @@ type SceneItem struct {
SceneItemIndex int `json:"sceneItemIndex"`
SceneItemLocked bool `json:"sceneItemLocked"`
SceneItemTransform SceneItemTransform `json:"sceneItemTransform"`
SourceUuid string `json:"sourceUuid"`
SourceName string `json:"sourceName"`
SourceType string `json:"sourceType"`
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/buger/jsonparser v1.1.1
github.com/gorilla/websocket v1.5.1
github.com/hashicorp/logutils v1.0.0
github.com/mitchellh/mapstructure v1.5.0
github.com/mmcloughlin/profile v0.1.1
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d
github.com/stretchr/testify v1.9.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mmcloughlin/profile v0.1.1 h1:jhDmAqPyebOsVDOCICJoINoLb/AnLBaUw58nFzxWS2w=
github.com/mmcloughlin/profile v0.1.1/go.mod h1:IhHD7q1ooxgwTgjxQYkACGA77oFTDdFVejUS1/tS/qU=
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d h1:VhgPp6v9qf9Agr/56bj7Y/xa04UccTW04VP0Qed4vnQ=
Expand Down
23 changes: 19 additions & 4 deletions internal/generate/protocol/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,7 @@ func generateRequest(request *Request) (s *Statement, err error) {
structName = name + "Response"
s.Commentf("Represents the response body for the %s request.", name).Line()

respf := &ResponseField{}
respf.ValueName = "_response"
respf.ValueType = "~requests~" // internal type
request.ResponseFields = append(request.ResponseFields, respf)
augmentStructFromProtocol(structName, request)

if err := generateStructFromParams("response", s, structName, request.ResponseFields); err != nil {
return nil, fmt.Errorf("Failed parsing 'Returns' for request %q in category %q", name, category)
Expand Down Expand Up @@ -397,6 +394,24 @@ func generateRequestStatuses(enums []*Enum, filter enumFilter) {
}
}

func augmentStructFromProtocol(name string, request *Request) {
if strings.HasSuffix(name, "Response") {
respf := &ResponseField{}
respf.ValueName = "_response"
respf.ValueType = "~requests~" // internal type
request.ResponseFields = append(request.ResponseFields, respf)
}

// the protocol sometimes omits fields that are returned by the server
switch name {
case "ToggleRecordPauseResponse":
f := &ResponseField{}
f.ValueName = "outputPaused"
f.ValueType = "Boolean"
request.ResponseFields = append(request.ResponseFields, f)
}
}

func generateStructFromParams[F Field](origin string, s *Statement, name string, fields []F) error {
keysInfo := map[string]keyInfo{}

Expand Down
1 change: 1 addition & 0 deletions internal/sample/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mmcloughlin/profile v0.1.1 // indirect
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions internal/sample/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mmcloughlin/profile v0.1.1 h1:jhDmAqPyebOsVDOCICJoINoLb/AnLBaUw58nFzxWS2w=
github.com/mmcloughlin/profile v0.1.1/go.mod h1:IhHD7q1ooxgwTgjxQYkACGA77oFTDdFVejUS1/tS/qU=
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d h1:VhgPp6v9qf9Agr/56bj7Y/xa04UccTW04VP0Qed4vnQ=
Expand Down
1 change: 1 addition & 0 deletions script/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ setup() {
}

gotest() {
export GOOBS_LOG=error
category="$1"
go test -v -run="^Test_$category$" -count 1 -coverprofile=cover.out -coverpkg=./... -covermode=$covermode ./...
awk 'NR>1' cover.out >>coverall.out
Expand Down

0 comments on commit 980a85a

Please sign in to comment.