Skip to content

Commit

Permalink
Merge pull request #2 from ksysoev/refactor_schema_handling
Browse files Browse the repository at this point in the history
Refactoring schema handling and code generation
  • Loading branch information
ksysoev authored Jul 25, 2023
2 parents 6181b4b + 8aef1be commit c8336c7
Show file tree
Hide file tree
Showing 239 changed files with 43,312 additions and 41,513 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ prepare-schema:
git apply ../schema.patch && \
cd config/v3/ && \
mkdir schema && \
find . -type f | grep -v example | grep -v schema | sed 'p; s/^\./.\/schema/; s/\/send//; s/\/receive/_resp/g' | xargs -n2 cp -f
find . -type f | grep -v example | grep -v schema | sed 'p; s/^\./.\/schema/; s/\/send//; s/\/receive/_resp/; s/\.json//;' | xargs -n2 cp -f

generate-schema:
cd deriv-developers-portal/config/v3/schema && \
gojsonschema -p deriv *.json > ../../../../schema.go
for file in *; do gojsonschema -p schema "$$file" > "../../../../schema/$${file%.*}.go"; done

clean:
rm -rf deriv-developers-portal
Expand All @@ -28,6 +28,6 @@ test:

coverage:
go test -covermode=count -coverprofile=coverage.out .
cat coverage.out | grep -v "/schema.go" | grep -v "/calls.go" | grep -v "subscription_calls.go" > coverage.final.out
cat coverage.out | grep -v "/calls.go" | grep -v "subscription_calls.go" > coverage.final.out
go tool cover -func=coverage.final.out
rm coverage.out coverage.final.out
3 changes: 2 additions & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"sync/atomic"
"time"

"github.com/ksysoev/deriv-api/schema"
"golang.org/x/net/websocket"
)

Expand Down Expand Up @@ -175,7 +176,7 @@ func (api *DerivAPI) Connect() error {
for {
select {
case <-time.After(interval):
_, err := api.Ping(Ping{Ping: 1})
_, err := api.Ping(schema.Ping{Ping: 1})
if err != nil {
return
}
Expand Down
13 changes: 7 additions & 6 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"
"time"

"github.com/ksysoev/deriv-api/schema"
"golang.org/x/net/websocket"
)

Expand Down Expand Up @@ -274,8 +275,8 @@ func TestSendRequestTimeout(t *testing.T) {
api.TimeOut = time.Millisecond

reqID := 1
req := Ping{Ping: 1, ReqId: &reqID}
var resp PingResp
req := schema.Ping{Ping: 1, ReqId: &reqID}
var resp schema.PingResp
err := api.SendRequest(reqID, req, &resp)

if err != nil && err.Error() != "timeout" {
Expand Down Expand Up @@ -343,8 +344,8 @@ func TestSendRequest(t *testing.T) {
}

reqID := 1
req := Ping{Ping: 1, ReqId: &reqID}
var resp PingResp
req := schema.Ping{Ping: 1, ReqId: &reqID}
var resp schema.PingResp
err = api.SendRequest(reqID, req, &resp)

if err != nil {
Expand All @@ -369,8 +370,8 @@ func TestSendRequestFailed(t *testing.T) {
api, _ := NewDerivAPI(url, 123, "en", "http://example.com")

reqID := 1
req := Ping{Ping: 1, ReqId: &reqID}
var resp PingResp
req := schema.Ping{Ping: 1, ReqId: &reqID}
var resp schema.PingResp
err := api.SendRequest(reqID, req, &resp)

if err == nil {
Expand Down
16 changes: 8 additions & 8 deletions bin/generate_calls.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func main() {
log.Fatal(err)
}

apiCalls := "// Code generated by bin/generate_call.go, DO NOT EDIT.\n\npackage deriv\n\n"
subcriptionCalls := "// Code generated by bin/generate_call.go, DO NOT EDIT.\n\npackage deriv\n\n"
apiCalls := "// Code generated by bin/generate_call.go, DO NOT EDIT.\n\npackage deriv\n\nimport \"github.com/ksysoev/deriv-api/schema\"\n\n"
subcriptionCalls := "// Code generated by bin/generate_call.go, DO NOT EDIT.\n\npackage deriv\n\nimport \"github.com/ksysoev/deriv-api/schema\"\n\n"

for _, file := range files {
name := file.Name()
Expand Down Expand Up @@ -85,7 +85,7 @@ func CreateTitle(name string) string {
}

func CreateApiCallFunction(title string, description string) string {
signature := fmt.Sprintf("// %s %s\nfunc (a *DerivAPI) %s(r %s) (resp %sResp, err error)", title, description, title, title, title)
signature := fmt.Sprintf("// %s %s\nfunc (a *DerivAPI) %s(r schema.%s) (resp schema.%sResp, err error)", title, description, title, title, title)

body := fmt.Sprintf(
`id := a.getNextRequestID()
Expand Down Expand Up @@ -113,25 +113,25 @@ func CreateSubscriptionCallFunction(title string, description string) string {
Resp = initResp
}

signature := fmt.Sprintf("// Subscribe%s %s\nfunc (a *DerivAPI) Subscribe%s(r %s) (rsp %s, s *Subsciption[%s, %s], err error)", title, description, title, title, initResp, initResp, Resp)
signature := fmt.Sprintf("// Subscribe%s %s\nfunc (a *DerivAPI) Subscribe%s(r schema.%s) (rsp schema.%s, s *Subsciption[schema.%s, schema.%s], err error)", title, description, title, title, initResp, initResp, Resp)

var body string
if title == "Transaction" {
body = fmt.Sprintf(
`id := a.getNextRequestID()
var f %sSubscribe = 1
var f schema.%sSubscribe = 1
r.ReqId = &id
r.Subscribe = f
s = NewSubcription[%s, %s](a)
s = NewSubcription[schema.%s, schema.%s](a)
rsp, err = s.Start(id, r)
return`, title, initResp, Resp)
} else {
body = fmt.Sprintf(
`id := a.getNextRequestID()
var f %sSubscribe = 1
var f schema.%sSubscribe = 1
r.ReqId = &id
r.Subscribe = &f
s = NewSubcription[%s, %s](a)
s = NewSubcription[schema.%s, schema.%s](a)
rsp, err = s.Start(id, r)
return`, title, initResp, Resp)
}
Expand Down
Loading

0 comments on commit c8336c7

Please sign in to comment.