From 627a5ed5c6f32fb7d67d3f51862ad0c3a4fae75e Mon Sep 17 00:00:00 2001 From: Lukas Jenicek Date: Fri, 18 Oct 2024 11:39:02 +0200 Subject: [PATCH] regenerate examples --- _examples/golang-basics/example.gen.go | 76 ++++++++++++++++++++++++-- _examples/golang-imports/api.gen.go | 57 +++++++++++++++++++ 2 files changed, 128 insertions(+), 5 deletions(-) diff --git a/_examples/golang-basics/example.gen.go b/_examples/golang-basics/example.gen.go index 7b6da62..5333931 100644 --- a/_examples/golang-basics/example.gen.go +++ b/_examples/golang-basics/example.gen.go @@ -1,4 +1,4 @@ -// example v0.0.1 05b7a5c86b98738f4fe6ce9bb1fccd4af064847a +// example v0.0.1 ccd12408e7dd53252d6e605ae1619666287d960e // -- // Code generated by webrpc-gen@v0.20.3-1-gf6584bc with ../../../gen-golang generator. DO NOT EDIT. // @@ -21,6 +21,10 @@ import ( "github.com/google/uuid" ) +const WebrpcHeader = "Webrpc" + +const WebrpcHeaderValue = "webrpc@v0.20.3-1-gf6584bc;gen-golang@unknown;example@v0.0.1" + // WebRPC description and code-gen version func WebRPCVersion() string { return "v1" @@ -33,7 +37,58 @@ func WebRPCSchemaVersion() string { // Schema hash generated from your RIDL schema func WebRPCSchemaHash() string { - return "05b7a5c86b98738f4fe6ce9bb1fccd4af064847a" + return "ccd12408e7dd53252d6e605ae1619666287d960e" +} + +type WebrpcGenVersions struct { + WebrpcGenVersion string + CodeGenName string + CodeGenVersion string + SchemaName string + SchemaVersion string +} + +func VersionFromHeader(h http.Header) (*WebrpcGenVersions, error) { + if h.Get(WebrpcHeader) == "" { + return nil, fmt.Errorf("header is empty or missing") + } + + versions, err := parseWebrpcGenVersions(h.Get(WebrpcHeader)) + if err != nil { + return nil, fmt.Errorf("webrpc header is invalid: %w", err) + } + + return versions, nil +} + +func parseWebrpcGenVersions(header string) (*WebrpcGenVersions, error) { + versions := strings.Split(header, ";") + if len(versions) < 3 { + return nil, fmt.Errorf("expected at least 3 parts while parsing webrpc header: %v", header) + } + + _, webrpcGenVersion, ok := strings.Cut(versions[0], "@") + if !ok { + return nil, fmt.Errorf("webrpc gen version could not be parsed from: %s", versions[0]) + } + + tmplTarget, tmplVersion, ok := strings.Cut(versions[1], "@") + if !ok { + return nil, fmt.Errorf("tmplTarget and tmplVersion could not be parsed from: %s", versions[1]) + } + + schemaName, schemaVersion, ok := strings.Cut(versions[2], "@") + if !ok { + return nil, fmt.Errorf("schema name and schema version could not be parsed from: %s", versions[2]) + } + + return &WebrpcGenVersions{ + WebrpcGenVersion: webrpcGenVersion, + CodeGenName: tmplTarget, + CodeGenVersion: tmplVersion, + SchemaName: schemaName, + SchemaVersion: schemaVersion, + }, nil } // @@ -143,9 +198,11 @@ type SearchFilter struct { } type Version struct { - WebrpcVersion string `json:"webrpcVersion"` - SchemaVersion string `json:"schemaVersion"` - SchemaHash string `json:"schemaHash"` + WebrpcVersion string `json:"webrpcVersion"` + SchemaVersion string `json:"schemaVersion"` + SchemaHash string `json:"schemaHash"` + ClientGenVersion *GenVersions `json:"clientGenVersion"` + ServerGenVersion *GenVersions `json:"serverGenVersion"` } type ComplexType struct { @@ -160,6 +217,13 @@ type ComplexType struct { User *User `json:"user"` } +type GenVersions struct { + WebrpcGenVersion string `json:"WebrpcGenVersion"` + TmplTarget string `json:"TmplTarget"` + TmplVersion string `json:"TmplVersion"` + SchemaVersion string `json:"SchemaVersion"` +} + var ( methods = map[string]method{ "/rpc/ExampleService/Ping": { @@ -267,6 +331,8 @@ func (s *exampleServiceServer) ServeHTTP(w http.ResponseWriter, r *http.Request) } }() + w.Header().Set(WebrpcHeader, WebrpcHeaderValue) + ctx := r.Context() ctx = context.WithValue(ctx, HTTPResponseWriterCtxKey, w) ctx = context.WithValue(ctx, HTTPRequestCtxKey, r) diff --git a/_examples/golang-imports/api.gen.go b/_examples/golang-imports/api.gen.go index 6f635e4..6e16407 100644 --- a/_examples/golang-imports/api.gen.go +++ b/_examples/golang-imports/api.gen.go @@ -18,6 +18,10 @@ import ( ) +const WebrpcHeader = "Webrpc" + +const WebrpcHeaderValue = "webrpc@v0.20.3-1-gf6584bc;gen-golang@unknown;example-api-service@v1.0.0" + // WebRPC description and code-gen version func WebRPCVersion() string { return "v1" @@ -33,6 +37,57 @@ func WebRPCSchemaHash() string { return "cae4e128f4fb4c938bfe1ea312deeea3dfd6b6af" } +type WebrpcGenVersions struct { + WebrpcGenVersion string + CodeGenName string + CodeGenVersion string + SchemaName string + SchemaVersion string +} + +func VersionFromHeader(h http.Header) (*WebrpcGenVersions, error) { + if h.Get(WebrpcHeader) == "" { + return nil, fmt.Errorf("header is empty or missing") + } + + versions, err := parseWebrpcGenVersions(h.Get(WebrpcHeader)) + if err != nil { + return nil, fmt.Errorf("webrpc header is invalid: %w", err) + } + + return versions, nil +} + +func parseWebrpcGenVersions(header string) (*WebrpcGenVersions, error) { + versions := strings.Split(header, ";") + if len(versions) < 3 { + return nil, fmt.Errorf("expected at least 3 parts while parsing webrpc header: %v", header) + } + + _, webrpcGenVersion, ok := strings.Cut(versions[0], "@") + if !ok { + return nil, fmt.Errorf("webrpc gen version could not be parsed from: %s", versions[0]) + } + + tmplTarget, tmplVersion, ok := strings.Cut(versions[1], "@") + if !ok { + return nil, fmt.Errorf("tmplTarget and tmplVersion could not be parsed from: %s", versions[1]) + } + + schemaName, schemaVersion, ok := strings.Cut(versions[2], "@") + if !ok { + return nil, fmt.Errorf("schema name and schema version could not be parsed from: %s", versions[2]) + } + + return &WebrpcGenVersions{ + WebrpcGenVersion: webrpcGenVersion, + CodeGenName: tmplTarget, + CodeGenVersion: tmplVersion, + SchemaName: schemaName, + SchemaVersion: schemaVersion, + }, nil +} + // // Common types // @@ -167,6 +222,8 @@ func (s *exampleAPIServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { } }() + w.Header().Set(WebrpcHeader, WebrpcHeaderValue) + ctx := r.Context() ctx = context.WithValue(ctx, HTTPResponseWriterCtxKey, w) ctx = context.WithValue(ctx, HTTPRequestCtxKey, r)