Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add missing UUIDs and fields on some resources #152

Merged
merged 5 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading