Skip to content

Commit

Permalink
Add client and server versions of generators (webrpc, schema and ridl…
Browse files Browse the repository at this point in the history
… schema)
  • Loading branch information
LukasJenicek committed Sep 30, 2024
1 parent 694c53c commit 6df17be
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 11 deletions.
60 changes: 54 additions & 6 deletions _examples/golang-basics/example.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions _examples/golang-basics/example.ridl
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
webrpc = v1 # version of webrpc schema format (ridl or json)

name = example # name of your backend app
version = v0.0.1 # version of your schema

version = [email protected] # version of your schema

# user role
# which defines which type of operations user can do
Expand Down Expand Up @@ -69,6 +68,8 @@ struct Version
- webrpcVersion: string
- schemaVersion: string
- schemaHash: string
- clientGenVersion: GenVersions
- serverGenVersion: GenVersions

struct ComplexType
- meta: map<string,any>
Expand All @@ -81,6 +82,12 @@ struct ComplexType
- mapOfUsers: map<string,User>
- user: User

struct GenVersions
- WebRPCGenVersion: string
- TmplTarget: string
- TmplVersion: string
- SchemaVersion: string

error 500100 MissingArgument "missing argument"
error 500101 InvalidUsername "invalid username"
error 400100 MemoryFull "system memory is full"
Expand Down
8 changes: 8 additions & 0 deletions _examples/golang-basics/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ func TestStatus(t *testing.T) {
assert.NoError(t, err)
}

func TestVersion(t *testing.T) {
version, err := client.Version(context.Background())

assert.NoError(t, err)
assert.NotNil(t, version.ClientGenVersion)
assert.NotNil(t, version.ServerGenVersion)
}

func TestGetUser(t *testing.T) {
{
arg1 := map[string]string{"a": "1"}
Expand Down
Binary file added _examples/golang-basics/golang-basics
Binary file not shown.
40 changes: 37 additions & 3 deletions _examples/golang-basics/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,44 @@ func (rpc *ExampleServiceRPC) Status(ctx context.Context) (bool, error) {
}

func (rpc *ExampleServiceRPC) Version(ctx context.Context) (*Version, error) {
req := RequestFromContext(ctx)
writer := ResponseWriterFromContext(ctx)

if writer.Header().Get("Webrpc") == "" {
return nil, fmt.Errorf("server webrpc header is missing")
}

serverVersions, err := ParseWebRPCGenVersions(writer.Header().Get("Webrpc"))
if err != nil {
return nil, fmt.Errorf("parse server webrpc gen versions: %w", err)
}

var clientVersions *GenVersions
if req.Header.Get("Webrpc") != "" {
parsedVersions, err := ParseWebRPCGenVersions(req.Header.Get("Webrpc"))
if err != nil {
return nil, fmt.Errorf("parse client webrpc gen versions: %w", err)
}

clientVersions = &GenVersions{
WebRPCGenVersion: parsedVersions.WebRPCGenVersion,
TmplTarget: parsedVersions.TmplTarget,
TmplVersion: parsedVersions.TmplVersion,
SchemaVersion: parsedVersions.SchemaVersion,
}
}

return &Version{
WebrpcVersion: WebRPCVersion(),
SchemaVersion: WebRPCSchemaVersion(),
SchemaHash: WebRPCSchemaHash(),
WebrpcVersion: WebRPCVersion(),
SchemaVersion: WebRPCSchemaVersion(),
SchemaHash: WebRPCSchemaHash(),
ClientGenVersion: clientVersions,
ServerGenVersion: &GenVersions{
WebRPCGenVersion: serverVersions.WebRPCGenVersion,
TmplTarget: serverVersions.TmplTarget,
TmplVersion: serverVersions.TmplVersion,
SchemaVersion: serverVersions.SchemaVersion,
},
}, nil
}

Expand Down
39 changes: 39 additions & 0 deletions _examples/golang-imports/api.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions client.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ func newRequest(ctx context.Context, url string, reqBody io.Reader, contentType
}
req.Header.Set("Accept", contentType)
req.Header.Set("Content-Type", contentType)
req.Header.Set("Webrpc", WebRPCHeader())
if headers, ok := HTTPRequestHeaders(ctx); ok {
for k := range headers {
for _, v := range headers[k] {
Expand Down
38 changes: 38 additions & 0 deletions main.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,44 @@ func WebRPCSchemaHash() string {
return "{{.SchemaHash}}"
}

type WebRPCGenVersions struct {
WebRPCGenVersion string
TmplTarget string
TmplVersion string
SchemaVersion string
}

// WebRPCHeader
// TmplVersion might be empty when generated from local template folder
func WebRPCHeader() string {
return "webrpc@{{.WebrpcGenVersion}};{{.TmplTarget}}@{{ if eq .TmplVersion "" }}unknown{{ else }}{{.TmplVersion}}{{end}};{{.SchemaVersion}}"
}

func ParseWebRPCGenVersions(header string) (*WebRPCGenVersions, error) {
versions := strings.Split(header, ";")

if len(versions) != 3 {
return nil, fmt.Errorf("expected 3 parts while parsing webrpc header: %s", header)
}

_, webrpcGenVersion, ok := strings.Cut(versions[0], "@")
if !ok {
return nil, fmt.Errorf("webrpc gen version could not be parsed from: %s", header)
}

tmplTarget, tmplVersion, ok := strings.Cut(versions[1], "@")
if !ok {
return nil, fmt.Errorf("tmplTarget and tmplVersion could not be parsed from: %s", header)
}

return &WebRPCGenVersions{
WebRPCGenVersion: webrpcGenVersion,
TmplTarget: tmplTarget,
TmplVersion: tmplVersion,
SchemaVersion: versions[2],
}, nil
}

{{- printf "\n" -}}

{{- if eq $opts.importTypesFrom "" }}
Expand Down
2 changes: 2 additions & 0 deletions server.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func (s *{{$serviceName}}) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}()

w.Header().Set("Webrpc", WebRPCHeader())

ctx := r.Context()
ctx = context.WithValue(ctx, HTTPResponseWriterCtxKey, w)
ctx = context.WithValue(ctx, HTTPRequestCtxKey, r)
Expand Down

0 comments on commit 6df17be

Please sign in to comment.