Skip to content

Commit

Permalink
add support for custom shell request to example.json (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominic Wong authored Dec 13, 2021
1 parent 76c0f57 commit 9cb789b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
9 changes: 5 additions & 4 deletions cmd/client-gen/go_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,18 @@ import(
}`

const curlExampleTemplate = `{{ $reqType := requestType .endpoint }}{{ $service := .service -}}
{{- if isNotStream $service.Spec $service.Name $reqType -}}
{{ if isCustomShell .example }}
{{ .example.ShellRequest }}
{{ else if isNotStream $service.Spec $service.Name $reqType }}
curl "https://api.m3o.com/v1/{{ $service.Name }}/{{ title .endpoint }}" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $M3O_API_TOKEN" \
-d '{{ tsExampleRequest $service.Name .endpoint $service.Spec.Components.Schemas .example.Request }}'
{{- end -}}
{{- if isStream $service.Spec $service.Name $reqType -}}
{{ else if isStream $service.Spec $service.Name $reqType }}
echo '{{ tsExampleRequest $service.Name .endpoint $service.Spec.Components.Schemas .example.Request }}' | \
websocat -n -H "Authorization: Bearer $M3O_API_TOKEN" \
wss://api.m3o.com/v1/{{ $service.Name }}/{{ title .endpoint }}
{{- end}}`
{{ end }}`

const goReadmeTopTemplate = `{{ $service := .service }}# {{ title $service.Name }}
Expand Down
26 changes: 19 additions & 7 deletions cmd/client-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -31,12 +32,13 @@ type service struct {
}

type example struct {
Title string `json:"title"`
Description string `json:"description"`
Request map[string]interface{}
Response map[string]interface{}
RunCheck bool `json:"run_check"`
Idempotent bool `json:"idempotent"`
Title string `json:"title"`
Description string `json:"description"`
Request map[string]interface{}
Response map[string]interface{}
RunCheck bool `json:"run_check"`
Idempotent bool `json:"idempotent"`
ShellRequest string `json:"shell_request"`
}

var (
Expand All @@ -62,6 +64,9 @@ func funcMap() map[string]interface{} {
return false
}
return map[string]interface{}{
"isCustomShell": func(ex example) bool {
return len(ex.ShellRequest) > 0
},
"recursiveTypeDefinition": func(language, serviceName, typeName string, schemas map[string]*openapi3.SchemaRef) string {
return schemaToType(language, serviceName, typeName, schemas)
},
Expand Down Expand Up @@ -722,7 +727,11 @@ func publishToNpm(tsPath string, tsFileList []string) {
}

func main() {
files, err := ioutil.ReadDir(os.Args[1])

serviceFlag := flag.String("service", "", "the service dir to process")
flag.Parse()

files, err := ioutil.ReadDir(flag.Arg(0))
if err != nil {
log.Fatal(err)
}
Expand All @@ -749,6 +758,9 @@ func main() {
services := []service{}
tsFileList := []string{"esm", "index.js", "index.d.ts"}
for _, f := range files {
if len(*serviceFlag) > 0 && f.Name() != *serviceFlag {
continue
}
if strings.Contains(f.Name(), "clients") || strings.Contains(f.Name(), "examples") {
continue
}
Expand Down

0 comments on commit 9cb789b

Please sign in to comment.