Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tests and adds support for custom schema #164

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bridge/bridgetest/bridgetest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -144,7 +144,7 @@ func MockFunc(e mockEnvironment) net.Conn {

select {
case msg := <-statusCh:
if msg == "finnished" {
if msg == "finished" {
return
}
default: // do nothing
Expand Down
41 changes: 41 additions & 0 deletions server/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"strings"
"sync"
"time"

"github.com/ugorji/go/codec"
)

type rpcHandler struct {
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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{
Expand Down
4 changes: 2 additions & 2 deletions test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down