From 469767484a3421506558e944a4f3b129dcb9d4e9 Mon Sep 17 00:00:00 2001 From: Atila Tauan Date: Fri, 3 Nov 2023 10:27:51 -0500 Subject: [PATCH 1/2] fix(test): typo and coroutine deadlock --- bridge/bridgetest/bridgetest.go | 4 ++-- test/test.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/bridge/bridgetest/bridgetest.go b/bridge/bridgetest/bridgetest.go index 1fa13ac..ca95e0f 100644 --- a/bridge/bridgetest/bridgetest.go +++ b/bridge/bridgetest/bridgetest.go @@ -116,7 +116,7 @@ type mockEnvironment interface { func MockFunc(e mockEnvironment) net.Conn { conA, conB := net.Pipe() - statusCh := make(chan string) + statusCh := make(chan string, 1) e.SubscribeStatusChange(statusCh) go func() { @@ -144,7 +144,7 @@ func MockFunc(e mockEnvironment) net.Conn { select { case msg := <-statusCh: - if msg == "finnished" { + if msg == "finished" { return } default: // do nothing diff --git a/test/test.go b/test/test.go index 209daa3..293a219 100644 --- a/test/test.go +++ b/test/test.go @@ -438,11 +438,11 @@ func (e *TestEnv) Handle(method string, args_d []byte) []byte { e.noErr(proto.Unmarshal(args_d, &args)) e.ClientRes.Status = int(args.Status) if args.Headers != nil { - e.Finish() - e.ClientRes.Body = args.Body headers := bridge.UnwrapHeaders(args.Headers) mergeHeaders(e.ClientRes.Headers, headers) } + e.Finish() + e.ClientRes.Body = args.Body case "kong.router.get_route": out = &kong_plugin_protocol.Route{ From 4ca34de463ef98612131cecd2050123ef2e27d96 Mon Sep 17 00:00:00 2001 From: Atila Tauan Date: Fri, 3 Nov 2023 10:41:58 -0500 Subject: [PATCH 2/2] feat(api): adds support for custom schema --- server/rpc.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/server/rpc.go b/server/rpc.go index ebbd168..9a99309 100644 --- a/server/rpc.go +++ b/server/rpc.go @@ -6,6 +6,8 @@ import ( "strings" "sync" "time" + + "github.com/ugorji/go/codec" ) type rpcHandler struct { @@ -69,6 +71,34 @@ func newRpcHandler(constructor func() interface{}, version string, priority int) type schemaDict map[string]interface{} +func decodeSchema(s string) schemaDict { + var ( + sd schemaDict + h codec.JsonHandle + ret strings.Builder + ) + + // allows for strings enclosed in single quotes + for i := 0; i < len(s); i++ { + if s[i] == '\'' && i > 0 && s[i-1] != '\\' { + ret.WriteByte('"') + } else { + // allows for escaped single quotes as \' + ret.WriteByte(s[i]) + } + } + + enc := codec.NewDecoder(strings.NewReader(ret.String()), &h) + enc.MustDecode(&sd) + return sd +} + +func updateSchemaDict(a, b schemaDict) { + for k, v := range b { + a[k] = v + } +} + func getSchemaDict(t reflect.Type) schemaDict { switch t.Kind() { case reflect.String: @@ -131,6 +161,17 @@ func getSchemaDict(t reflect.Type) schemaDict { if name == "" { name = strings.ToLower(field.Name) } + + if s, ok := field.Tag.Lookup("schema"); ok { + if customSchema := decodeSchema(s); customSchema != nil { + if field.Type.Kind() == reflect.Map { + updateSchemaDict(typeDecl["keys"].(schemaDict), customSchema) + } else { + updateSchemaDict(typeDecl, customSchema) + } + } + } + fieldsArray = append(fieldsArray, schemaDict{name: typeDecl}) } return schemaDict{