From a17462ae2ca2e893b2aa7efaac740b39112698da Mon Sep 17 00:00:00 2001 From: Travis Cline Date: Thu, 13 Aug 2015 19:12:03 -0400 Subject: [PATCH] rename internal to utilties for 1.5 compatibility --- Makefile | 6 +- examples/a_bit_of_everything.pb.gw.go | 8 +- examples/echo_service.pb.gw.go | 4 +- internal/doc.go | 2 - protoc-gen-grpc-gateway/descriptor/types.go | 8 +- .../gengateway/generator.go | 2 +- .../gengateway/template.go | 12 +- protoc-gen-grpc-gateway/httprule/compile.go | 18 +- .../httprule/compile_test.go | 34 +-- runtime/mux_test.go | 24 +-- runtime/pattern.go | 40 ++-- runtime/pattern_test.go | 194 +++++++++--------- runtime/query.go | 6 +- runtime/query_test.go | 22 +- utilities/doc.go | 2 + {internal => utilities}/name.go | 2 +- {internal => utilities}/name_test.go | 8 +- {internal => utilities}/pattern.go | 2 +- {internal => utilities}/trie.go | 2 +- {internal => utilities}/trie_test.go | 50 ++--- 20 files changed, 223 insertions(+), 223 deletions(-) delete mode 100644 internal/doc.go create mode 100644 utilities/doc.go rename {internal => utilities}/name.go (94%) rename {internal => utilities}/name_test.go (58%) rename {internal => utilities}/pattern.go (97%) rename {internal => utilities}/trie.go (99%) rename {internal => utilities}/trie_test.go (88%) diff --git a/Makefile b/Makefile index fbde89d7cd5..b3f70c5e631 100644 --- a/Makefile +++ b/Makefile @@ -3,9 +3,9 @@ GO_PLUGIN=bin/protoc-gen-go GO_PLUGIN_PKG=github.com/golang/protobuf/protoc-gen-go GATEWAY_PLUGIN=bin/protoc-gen-grpc-gateway GATEWAY_PLUGIN_PKG=$(PKG)/protoc-gen-grpc-gateway -GATEWAY_PLUGIN_SRC= internal/doc.go \ - internal/name.go \ - internal/pattern.go \ +GATEWAY_PLUGIN_SRC= utilities/doc.go \ + utilities/name.go \ + utilities/pattern.go \ protoc-gen-grpc-gateway/descriptor/registry.go \ protoc-gen-grpc-gateway/descriptor/services.go \ protoc-gen-grpc-gateway/descriptor/types.go \ diff --git a/examples/a_bit_of_everything.pb.gw.go b/examples/a_bit_of_everything.pb.gw.go index 39495a8d128..5ff2f5bae04 100644 --- a/examples/a_bit_of_everything.pb.gw.go +++ b/examples/a_bit_of_everything.pb.gw.go @@ -15,7 +15,7 @@ import ( "net/http" "github.com/gengo/grpc-gateway/examples/sub" - "github.com/gengo/grpc-gateway/internal" + "github.com/gengo/grpc-gateway/utilities" "github.com/gengo/grpc-gateway/runtime" "github.com/golang/glog" "github.com/golang/protobuf/proto" @@ -28,10 +28,10 @@ var _ codes.Code var _ io.Reader var _ = runtime.String var _ = json.Marshal -var _ = internal.PascalFromSnake +var _ = utilities.PascalFromSnake var ( - filter_ABitOfEverythingService_Create_0 = &internal.DoubleArray{Encoding: map[string]int{"float_value": 0, "double_value": 1, "int64_value": 2, "uint64_value": 3, "int32_value": 4, "fixed64_value": 5, "fixed32_value": 6, "bool_value": 7, "string_value": 8, "uint32_value": 9, "sfixed32_value": 10, "sfixed64_value": 11, "sint32_value": 12, "sint64_value": 13}, Base: []int{1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, Check: []int{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}} + filter_ABitOfEverythingService_Create_0 = &utilities.DoubleArray{Encoding: map[string]int{"float_value": 0, "double_value": 1, "int64_value": 2, "uint64_value": 3, "int32_value": 4, "fixed64_value": 5, "fixed32_value": 6, "bool_value": 7, "string_value": 8, "uint32_value": 9, "sfixed32_value": 10, "sfixed64_value": 11, "sint32_value": 12, "sint64_value": 13}, Base: []int{1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, Check: []int{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}} ) func request_ABitOfEverythingService_Create_0(ctx context.Context, client ABitOfEverythingServiceClient, req *http.Request, pathParams map[string]string) (msg proto.Message, err error) { @@ -303,7 +303,7 @@ func request_ABitOfEverythingService_Echo_1(ctx context.Context, client ABitOfEv } var ( - filter_ABitOfEverythingService_Echo_2 = &internal.DoubleArray{Encoding: map[string]int{}, Base: []int(nil), Check: []int(nil)} + filter_ABitOfEverythingService_Echo_2 = &utilities.DoubleArray{Encoding: map[string]int{}, Base: []int(nil), Check: []int(nil)} ) func request_ABitOfEverythingService_Echo_2(ctx context.Context, client ABitOfEverythingServiceClient, req *http.Request, pathParams map[string]string) (msg proto.Message, err error) { diff --git a/examples/echo_service.pb.gw.go b/examples/echo_service.pb.gw.go index 5f1561803f2..51ff107d3a2 100644 --- a/examples/echo_service.pb.gw.go +++ b/examples/echo_service.pb.gw.go @@ -14,7 +14,7 @@ import ( "io" "net/http" - "github.com/gengo/grpc-gateway/internal" + "github.com/gengo/grpc-gateway/utilities" "github.com/gengo/grpc-gateway/runtime" "github.com/golang/glog" "github.com/golang/protobuf/proto" @@ -27,7 +27,7 @@ var _ codes.Code var _ io.Reader var _ = runtime.String var _ = json.Marshal -var _ = internal.PascalFromSnake +var _ = utilities.PascalFromSnake func request_EchoService_Echo_0(ctx context.Context, client EchoServiceClient, req *http.Request, pathParams map[string]string) (msg proto.Message, err error) { var protoReq SimpleMessage diff --git a/internal/doc.go b/internal/doc.go deleted file mode 100644 index 0860ea062cf..00000000000 --- a/internal/doc.go +++ /dev/null @@ -1,2 +0,0 @@ -// Package internal provides members for internal use in grpc-gateway. -package internal diff --git a/protoc-gen-grpc-gateway/descriptor/types.go b/protoc-gen-grpc-gateway/descriptor/types.go index e9fb6e1b9d3..b3d155eb566 100644 --- a/protoc-gen-grpc-gateway/descriptor/types.go +++ b/protoc-gen-grpc-gateway/descriptor/types.go @@ -4,7 +4,7 @@ import ( "fmt" "strings" - "github.com/gengo/grpc-gateway/internal" + "github.com/gengo/grpc-gateway/utilities" "github.com/gengo/grpc-gateway/protoc-gen-grpc-gateway/httprule" descriptor "github.com/golang/protobuf/protoc-gen-go/descriptor" ) @@ -233,15 +233,15 @@ type FieldPathComponent struct { // RHS returns a right-hand-side expression in go for this field. func (c FieldPathComponent) RHS() string { - return internal.PascalFromSnake(c.Name) + return utilities.PascalFromSnake(c.Name) } // LHS returns a left-hand-side expression in go for this field. func (c FieldPathComponent) LHS() string { if c.Target.Message.File.proto2() { - return fmt.Sprintf("Get%s()", internal.PascalFromSnake(c.Name)) + return fmt.Sprintf("Get%s()", utilities.PascalFromSnake(c.Name)) } - return internal.PascalFromSnake(c.Name) + return utilities.PascalFromSnake(c.Name) } var ( diff --git a/protoc-gen-grpc-gateway/gengateway/generator.go b/protoc-gen-grpc-gateway/gengateway/generator.go index 6d752440b4a..8a9927dc8f8 100644 --- a/protoc-gen-grpc-gateway/gengateway/generator.go +++ b/protoc-gen-grpc-gateway/gengateway/generator.go @@ -31,7 +31,7 @@ func New(reg *descriptor.Registry) *generator { "io", "net/http", "github.com/gengo/grpc-gateway/runtime", - "github.com/gengo/grpc-gateway/internal", + "github.com/gengo/grpc-gateway/utilities", "github.com/golang/glog", "github.com/golang/protobuf/proto", "golang.org/x/net/context", diff --git a/protoc-gen-grpc-gateway/gengateway/template.go b/protoc-gen-grpc-gateway/gengateway/template.go index c005bafef91..e313a0f667c 100644 --- a/protoc-gen-grpc-gateway/gengateway/template.go +++ b/protoc-gen-grpc-gateway/gengateway/template.go @@ -6,7 +6,7 @@ import ( "strings" "text/template" - "github.com/gengo/grpc-gateway/internal" + "github.com/gengo/grpc-gateway/utilities" "github.com/gengo/grpc-gateway/protoc-gen-grpc-gateway/descriptor" "github.com/golang/glog" ) @@ -49,12 +49,12 @@ func (b binding) QueryParamFilter() queryParamFilter { for _, p := range b.PathParams { seqs = append(seqs, strings.Split(p.FieldPath.String(), ".")) } - return queryParamFilter{internal.NewDoubleArray(seqs)} + return queryParamFilter{utilities.NewDoubleArray(seqs)} } -// queryParamFilter is a wrapper of internal.DoubleArray which provides String() to output DoubleArray.Encoding in a stable and predictable format. +// queryParamFilter is a wrapper of utilities.DoubleArray which provides String() to output DoubleArray.Encoding in a stable and predictable format. type queryParamFilter struct { - *internal.DoubleArray + *utilities.DoubleArray } func (f queryParamFilter) String() string { @@ -63,7 +63,7 @@ func (f queryParamFilter) String() string { encodings[enc] = fmt.Sprintf("%q: %d", str, enc) } e := strings.Join(encodings, ", ") - return fmt.Sprintf("&internal.DoubleArray{Encoding: map[string]int{%s}, Base: %#v, Check: %#v}", e, f.Base, f.Check) + return fmt.Sprintf("&utilities.DoubleArray{Encoding: map[string]int{%s}, Base: %#v, Check: %#v}", e, f.Base, f.Check) } func applyTemplate(p param) (string, error) { @@ -114,7 +114,7 @@ var _ codes.Code var _ io.Reader var _ = runtime.String var _ = json.Marshal -var _ = internal.PascalFromSnake +var _ = utilities.PascalFromSnake `)) handlerTemplate = template.Must(template.New("handler").Parse(` diff --git a/protoc-gen-grpc-gateway/httprule/compile.go b/protoc-gen-grpc-gateway/httprule/compile.go index 8339b896201..90073575181 100644 --- a/protoc-gen-grpc-gateway/httprule/compile.go +++ b/protoc-gen-grpc-gateway/httprule/compile.go @@ -1,7 +1,7 @@ package httprule import ( - "github.com/gengo/grpc-gateway/internal" + "github.com/gengo/grpc-gateway/utilities" ) const ( @@ -22,7 +22,7 @@ type Template struct { Fields []string } -// Compiler compiles internal representation of path templates into marshallable operations. +// Compiler compiles utilities representation of path templates into marshallable operations. // They can be unmarshalled by runtime.NewPattern. type Compiler interface { Compile() Template @@ -30,7 +30,7 @@ type Compiler interface { type op struct { // code is the opcode of the operation - code internal.OpCode + code utilities.OpCode // str is a string operand of the code. // num is ignored if str is not empty. @@ -42,20 +42,20 @@ type op struct { func (w wildcard) compile() []op { return []op{ - {code: internal.OpPush}, + {code: utilities.OpPush}, } } func (w deepWildcard) compile() []op { return []op{ - {code: internal.OpPushM}, + {code: utilities.OpPushM}, } } func (l literal) compile() []op { return []op{ { - code: internal.OpLitPush, + code: utilities.OpLitPush, str: string(l), }, } @@ -67,10 +67,10 @@ func (v variable) compile() []op { ops = append(ops, s.compile()...) } ops = append(ops, op{ - code: internal.OpConcatN, + code: utilities.OpConcatN, num: len(v.segments), }, op{ - code: internal.OpCapture, + code: utilities.OpCapture, str: v.path, }) @@ -100,7 +100,7 @@ func (t template) Compile() Template { } ops = append(ops, consts[op.str]) } - if op.code == internal.OpCapture { + if op.code == utilities.OpCapture { fields = append(fields, op.str) } } diff --git a/protoc-gen-grpc-gateway/httprule/compile_test.go b/protoc-gen-grpc-gateway/httprule/compile_test.go index d4e8b3e7d12..451ae2ad467 100644 --- a/protoc-gen-grpc-gateway/httprule/compile_test.go +++ b/protoc-gen-grpc-gateway/httprule/compile_test.go @@ -4,7 +4,7 @@ import ( "reflect" "testing" - "github.com/gengo/grpc-gateway/internal" + "github.com/gengo/grpc-gateway/utilities" ) const ( @@ -25,19 +25,19 @@ func TestCompile(t *testing.T) { segs: []segment{ wildcard{}, }, - ops: []int{int(internal.OpPush), operandFiller}, + ops: []int{int(utilities.OpPush), operandFiller}, }, { segs: []segment{ deepWildcard{}, }, - ops: []int{int(internal.OpPushM), operandFiller}, + ops: []int{int(utilities.OpPushM), operandFiller}, }, { segs: []segment{ literal("v1"), }, - ops: []int{int(internal.OpLitPush), 0}, + ops: []int{int(utilities.OpLitPush), 0}, pool: []string{"v1"}, }, { @@ -45,7 +45,7 @@ func TestCompile(t *testing.T) { literal("v1"), }, verb: "LOCK", - ops: []int{int(internal.OpLitPush), 0}, + ops: []int{int(utilities.OpLitPush), 0}, pool: []string{"v1"}, }, { @@ -58,9 +58,9 @@ func TestCompile(t *testing.T) { }, }, ops: []int{ - int(internal.OpPush), operandFiller, - int(internal.OpConcatN), 1, - int(internal.OpCapture), 0, + int(utilities.OpPush), operandFiller, + int(utilities.OpConcatN), 1, + int(utilities.OpCapture), 0, }, pool: []string{"name.nested"}, fields: []string{"name.nested"}, @@ -84,15 +84,15 @@ func TestCompile(t *testing.T) { }, }, ops: []int{ - int(internal.OpLitPush), 0, - int(internal.OpLitPush), 1, - int(internal.OpPush), operandFiller, - int(internal.OpLitPush), 2, - int(internal.OpConcatN), 3, - int(internal.OpCapture), 3, - int(internal.OpPushM), operandFiller, - int(internal.OpConcatN), 1, - int(internal.OpCapture), 0, + int(utilities.OpLitPush), 0, + int(utilities.OpLitPush), 1, + int(utilities.OpPush), operandFiller, + int(utilities.OpLitPush), 2, + int(utilities.OpConcatN), 3, + int(utilities.OpCapture), 3, + int(utilities.OpPushM), operandFiller, + int(utilities.OpConcatN), 1, + int(utilities.OpCapture), 0, }, pool: []string{"obj", "a", "b", "name.nested"}, fields: []string{"name.nested", "obj"}, diff --git a/runtime/mux_test.go b/runtime/mux_test.go index 7394bb13a94..d86eba23153 100644 --- a/runtime/mux_test.go +++ b/runtime/mux_test.go @@ -7,7 +7,7 @@ import ( "net/http/httptest" "testing" - "github.com/gengo/grpc-gateway/internal" + "github.com/gengo/grpc-gateway/utilities" "github.com/gengo/grpc-gateway/runtime" ) @@ -37,7 +37,7 @@ func TestMuxServeHTTP(t *testing.T) { patterns: []stubPattern{ { method: "GET", - ops: []int{int(internal.OpLitPush), 0}, + ops: []int{int(utilities.OpLitPush), 0}, pool: []string{"foo"}, }, }, @@ -50,7 +50,7 @@ func TestMuxServeHTTP(t *testing.T) { patterns: []stubPattern{ { method: "GET", - ops: []int{int(internal.OpLitPush), 0}, + ops: []int{int(utilities.OpLitPush), 0}, pool: []string{"foo"}, }, }, @@ -62,12 +62,12 @@ func TestMuxServeHTTP(t *testing.T) { patterns: []stubPattern{ { method: "GET", - ops: []int{int(internal.OpLitPush), 0}, + ops: []int{int(utilities.OpLitPush), 0}, pool: []string{"foo"}, }, { method: "GET", - ops: []int{int(internal.OpPush), 0}, + ops: []int{int(utilities.OpPush), 0}, }, }, reqMethod: "GET", @@ -79,12 +79,12 @@ func TestMuxServeHTTP(t *testing.T) { patterns: []stubPattern{ { method: "GET", - ops: []int{int(internal.OpLitPush), 0}, + ops: []int{int(utilities.OpLitPush), 0}, pool: []string{"foo"}, }, { method: "POST", - ops: []int{int(internal.OpLitPush), 0}, + ops: []int{int(utilities.OpLitPush), 0}, pool: []string{"foo"}, }, }, @@ -97,7 +97,7 @@ func TestMuxServeHTTP(t *testing.T) { patterns: []stubPattern{ { method: "GET", - ops: []int{int(internal.OpLitPush), 0}, + ops: []int{int(utilities.OpLitPush), 0}, pool: []string{"foo"}, }, }, @@ -109,7 +109,7 @@ func TestMuxServeHTTP(t *testing.T) { patterns: []stubPattern{ { method: "GET", - ops: []int{int(internal.OpLitPush), 0}, + ops: []int{int(utilities.OpLitPush), 0}, pool: []string{"foo"}, }, }, @@ -125,12 +125,12 @@ func TestMuxServeHTTP(t *testing.T) { patterns: []stubPattern{ { method: "GET", - ops: []int{int(internal.OpLitPush), 0}, + ops: []int{int(utilities.OpLitPush), 0}, pool: []string{"foo"}, }, { method: "POST", - ops: []int{int(internal.OpLitPush), 0}, + ops: []int{int(utilities.OpLitPush), 0}, pool: []string{"foo"}, }, }, @@ -147,7 +147,7 @@ func TestMuxServeHTTP(t *testing.T) { patterns: []stubPattern{ { method: "GET", - ops: []int{int(internal.OpLitPush), 0}, + ops: []int{int(utilities.OpLitPush), 0}, pool: []string{"foo"}, }, }, diff --git a/runtime/pattern.go b/runtime/pattern.go index 3bb1451707c..694231ff9d7 100644 --- a/runtime/pattern.go +++ b/runtime/pattern.go @@ -5,7 +5,7 @@ import ( "fmt" "strings" - "github.com/gengo/grpc-gateway/internal" + "github.com/gengo/grpc-gateway/utilities" "github.com/golang/glog" ) @@ -17,7 +17,7 @@ var ( ) type op struct { - code internal.OpCode + code utilities.OpCode operand int } @@ -56,19 +56,19 @@ func NewPattern(version int, ops []int, pool []string, verb string) (Pattern, er var stack, maxstack int var vars []string for i := 0; i < l; i += 2 { - op := op{code: internal.OpCode(ops[i]), operand: ops[i+1]} + op := op{code: utilities.OpCode(ops[i]), operand: ops[i+1]} switch op.code { - case internal.OpNop: + case utilities.OpNop: continue - case internal.OpPush, internal.OpPushM: + case utilities.OpPush, utilities.OpPushM: stack++ - case internal.OpLitPush: + case utilities.OpLitPush: if op.operand < 0 || len(pool) <= op.operand { glog.V(2).Infof("negative literal index: %d", op.operand) return Pattern{}, ErrInvalidPattern } stack++ - case internal.OpConcatN: + case utilities.OpConcatN: if op.operand <= 0 { glog.V(2).Infof("negative concat size: %d", op.operand) return Pattern{}, ErrInvalidPattern @@ -79,7 +79,7 @@ func NewPattern(version int, ops []int, pool []string, verb string) (Pattern, er return Pattern{}, ErrInvalidPattern } stack++ - case internal.OpCapture: + case utilities.OpCapture: if op.operand < 0 || len(pool) <= op.operand { glog.V(2).Infof("variable name index out of bound: %d", op.operand) return Pattern{}, ErrInvalidPattern @@ -136,15 +136,15 @@ func (p Pattern) Match(components []string, verb string) (map[string]string, err l := len(components) for _, op := range p.ops { switch op.code { - case internal.OpNop: + case utilities.OpNop: continue - case internal.OpPush, internal.OpLitPush: + case utilities.OpPush, utilities.OpLitPush: if pos >= l { glog.V(1).Infof("insufficient # of segments") return nil, ErrNotMatch } c := components[pos] - if op.code == internal.OpLitPush { + if op.code == utilities.OpLitPush { if lit := p.pool[op.operand]; c != lit { glog.V(1).Infof("literal segment mismatch: got %q; want %q", c, lit) return nil, ErrNotMatch @@ -152,14 +152,14 @@ func (p Pattern) Match(components []string, verb string) (map[string]string, err } stack = append(stack, c) pos++ - case internal.OpPushM: + case utilities.OpPushM: stack = append(stack, strings.Join(components[pos:], "/")) pos = len(components) - case internal.OpConcatN: + case utilities.OpConcatN: n := op.operand l := len(stack) - n stack = append(stack[:l], strings.Join(stack[l:], "/")) - case internal.OpCapture: + case utilities.OpCapture: n := len(stack) - 1 captured[op.operand] = stack[n] stack = stack[:n] @@ -183,19 +183,19 @@ func (p Pattern) String() string { var stack []string for _, op := range p.ops { switch op.code { - case internal.OpNop: + case utilities.OpNop: continue - case internal.OpPush: + case utilities.OpPush: stack = append(stack, "*") - case internal.OpLitPush: + case utilities.OpLitPush: stack = append(stack, p.pool[op.operand]) - case internal.OpPushM: + case utilities.OpPushM: stack = append(stack, "**") - case internal.OpConcatN: + case utilities.OpConcatN: n := op.operand l := len(stack) - n stack = append(stack[:l], strings.Join(stack[l:], "/")) - case internal.OpCapture: + case utilities.OpCapture: n := len(stack) - 1 stack[n] = fmt.Sprintf("{%s=%s}", p.vars[op.operand], stack[n]) } diff --git a/runtime/pattern_test.go b/runtime/pattern_test.go index 878b21c9f29..d81916539a5 100644 --- a/runtime/pattern_test.go +++ b/runtime/pattern_test.go @@ -6,7 +6,7 @@ import ( "strings" "testing" - "github.com/gengo/grpc-gateway/internal" + "github.com/gengo/grpc-gateway/utilities" ) const ( @@ -24,52 +24,52 @@ func TestNewPattern(t *testing.T) { }{ {}, { - ops: []int{int(internal.OpNop), anything}, + ops: []int{int(utilities.OpNop), anything}, stackSizeWant: 0, }, { - ops: []int{int(internal.OpPush), anything}, + ops: []int{int(utilities.OpPush), anything}, stackSizeWant: 1, }, { - ops: []int{int(internal.OpLitPush), 0}, + ops: []int{int(utilities.OpLitPush), 0}, pool: []string{"abc"}, stackSizeWant: 1, }, { - ops: []int{int(internal.OpPushM), anything}, + ops: []int{int(utilities.OpPushM), anything}, stackSizeWant: 1, }, { ops: []int{ - int(internal.OpPush), anything, - int(internal.OpConcatN), 1, + int(utilities.OpPush), anything, + int(utilities.OpConcatN), 1, }, stackSizeWant: 1, }, { ops: []int{ - int(internal.OpPush), anything, - int(internal.OpConcatN), 1, - int(internal.OpCapture), 0, + int(utilities.OpPush), anything, + int(utilities.OpConcatN), 1, + int(utilities.OpCapture), 0, }, pool: []string{"abc"}, stackSizeWant: 1, }, { ops: []int{ - int(internal.OpPush), anything, - int(internal.OpLitPush), 0, - int(internal.OpLitPush), 1, - int(internal.OpPushM), anything, - int(internal.OpConcatN), 2, - int(internal.OpCapture), 2, + int(utilities.OpPush), anything, + int(utilities.OpLitPush), 0, + int(utilities.OpLitPush), 1, + int(utilities.OpPushM), anything, + int(utilities.OpConcatN), 2, + int(utilities.OpCapture), 2, }, pool: []string{"lit1", "lit2", "var1"}, stackSizeWant: 4, }, { - ops: []int{int(internal.OpLitPush), 0}, + ops: []int{int(utilities.OpLitPush), 0}, pool: []string{"abc"}, stackSizeWant: 1, verb: "LOCK", @@ -98,35 +98,35 @@ func TestNewPatternWithWrongOp(t *testing.T) { }, { // op code out of bound - ops: []int{int(internal.OpEnd), 0}, + ops: []int{int(utilities.OpEnd), 0}, }, { // odd number of items - ops: []int{int(internal.OpPush)}, + ops: []int{int(utilities.OpPush)}, }, { // negative index - ops: []int{int(internal.OpLitPush), -1}, + ops: []int{int(utilities.OpLitPush), -1}, pool: []string{"abc"}, }, { // index out of bound - ops: []int{int(internal.OpLitPush), 1}, + ops: []int{int(utilities.OpLitPush), 1}, pool: []string{"abc"}, }, { // negative # of segments - ops: []int{int(internal.OpConcatN), -1}, + ops: []int{int(utilities.OpConcatN), -1}, pool: []string{"abc"}, }, { // negative index - ops: []int{int(internal.OpCapture), -1}, + ops: []int{int(utilities.OpCapture), -1}, pool: []string{"abc"}, }, { // index out of bound - ops: []int{int(internal.OpCapture), 1}, + ops: []int{int(utilities.OpCapture), 1}, pool: []string{"abc"}, }, } { @@ -149,10 +149,10 @@ func TestNewPatternWithStackUnderflow(t *testing.T) { verb string }{ { - ops: []int{int(internal.OpConcatN), 1}, + ops: []int{int(utilities.OpConcatN), 1}, }, { - ops: []int{int(internal.OpCapture), 0}, + ops: []int{int(utilities.OpCapture), 0}, pool: []string{"abc"}, }, } { @@ -182,32 +182,32 @@ func TestMatch(t *testing.T) { notMatch: []string{"example"}, }, { - ops: []int{int(internal.OpNop), anything}, + ops: []int{int(utilities.OpNop), anything}, match: []string{""}, notMatch: []string{"example", "path/to/example"}, }, { - ops: []int{int(internal.OpPush), anything}, + ops: []int{int(utilities.OpPush), anything}, match: []string{"abc", "def"}, notMatch: []string{"", "abc/def"}, }, { - ops: []int{int(internal.OpLitPush), 0}, + ops: []int{int(utilities.OpLitPush), 0}, pool: []string{"v1"}, match: []string{"v1"}, notMatch: []string{"", "v2"}, }, { - ops: []int{int(internal.OpPushM), anything}, + ops: []int{int(utilities.OpPushM), anything}, match: []string{"", "abc", "abc/def", "abc/def/ghi"}, }, { ops: []int{ - int(internal.OpLitPush), 0, - int(internal.OpLitPush), 1, - int(internal.OpPush), anything, - int(internal.OpConcatN), 1, - int(internal.OpCapture), 2, + int(utilities.OpLitPush), 0, + int(utilities.OpLitPush), 1, + int(utilities.OpPush), anything, + int(utilities.OpConcatN), 1, + int(utilities.OpCapture), 2, }, pool: []string{"v1", "bucket", "name"}, match: []string{"v1/bucket/my-bucket", "v1/bucket/our-bucket"}, @@ -221,11 +221,11 @@ func TestMatch(t *testing.T) { }, { ops: []int{ - int(internal.OpLitPush), 0, - int(internal.OpLitPush), 1, - int(internal.OpPushM), anything, - int(internal.OpConcatN), 2, - int(internal.OpCapture), 2, + int(utilities.OpLitPush), 0, + int(utilities.OpLitPush), 1, + int(utilities.OpPushM), anything, + int(utilities.OpConcatN), 2, + int(utilities.OpCapture), 2, }, pool: []string{"v1", "o", "name"}, match: []string{ @@ -245,15 +245,15 @@ func TestMatch(t *testing.T) { }, { ops: []int{ - int(internal.OpLitPush), 0, - int(internal.OpLitPush), 1, - int(internal.OpPush), anything, - int(internal.OpConcatN), 2, - int(internal.OpCapture), 2, - int(internal.OpLitPush), 3, - int(internal.OpPush), anything, - int(internal.OpConcatN), 1, - int(internal.OpCapture), 4, + int(utilities.OpLitPush), 0, + int(utilities.OpLitPush), 1, + int(utilities.OpPush), anything, + int(utilities.OpConcatN), 2, + int(utilities.OpCapture), 2, + int(utilities.OpLitPush), 3, + int(utilities.OpPush), anything, + int(utilities.OpConcatN), 1, + int(utilities.OpCapture), 4, }, pool: []string{"v2", "b", "name", "o", "oname"}, match: []string{ @@ -270,7 +270,7 @@ func TestMatch(t *testing.T) { }, }, { - ops: []int{int(internal.OpLitPush), 0}, + ops: []int{int(utilities.OpLitPush), 0}, pool: []string{"v1"}, verb: "LOCK", match: []string{"v1:LOCK"}, @@ -316,38 +316,38 @@ func TestMatchWithBinding(t *testing.T) { want: make(map[string]string), }, { - ops: []int{int(internal.OpNop), anything}, + ops: []int{int(utilities.OpNop), anything}, want: make(map[string]string), }, { - ops: []int{int(internal.OpPush), anything}, + ops: []int{int(utilities.OpPush), anything}, path: "abc", want: make(map[string]string), }, { - ops: []int{int(internal.OpPush), anything}, + ops: []int{int(utilities.OpPush), anything}, verb: "LOCK", path: "abc:LOCK", want: make(map[string]string), }, { - ops: []int{int(internal.OpLitPush), 0}, + ops: []int{int(utilities.OpLitPush), 0}, pool: []string{"endpoint"}, path: "endpoint", want: make(map[string]string), }, { - ops: []int{int(internal.OpPushM), anything}, + ops: []int{int(utilities.OpPushM), anything}, path: "abc/def/ghi", want: make(map[string]string), }, { ops: []int{ - int(internal.OpLitPush), 0, - int(internal.OpLitPush), 1, - int(internal.OpPush), anything, - int(internal.OpConcatN), 1, - int(internal.OpCapture), 2, + int(utilities.OpLitPush), 0, + int(utilities.OpLitPush), 1, + int(utilities.OpPush), anything, + int(utilities.OpConcatN), 1, + int(utilities.OpCapture), 2, }, pool: []string{"v1", "bucket", "name"}, path: "v1/bucket/my-bucket", @@ -357,11 +357,11 @@ func TestMatchWithBinding(t *testing.T) { }, { ops: []int{ - int(internal.OpLitPush), 0, - int(internal.OpLitPush), 1, - int(internal.OpPush), anything, - int(internal.OpConcatN), 1, - int(internal.OpCapture), 2, + int(utilities.OpLitPush), 0, + int(utilities.OpLitPush), 1, + int(utilities.OpPush), anything, + int(utilities.OpConcatN), 1, + int(utilities.OpCapture), 2, }, pool: []string{"v1", "bucket", "name"}, verb: "LOCK", @@ -372,11 +372,11 @@ func TestMatchWithBinding(t *testing.T) { }, { ops: []int{ - int(internal.OpLitPush), 0, - int(internal.OpLitPush), 1, - int(internal.OpPushM), anything, - int(internal.OpConcatN), 2, - int(internal.OpCapture), 2, + int(utilities.OpLitPush), 0, + int(utilities.OpLitPush), 1, + int(utilities.OpPushM), anything, + int(utilities.OpConcatN), 2, + int(utilities.OpCapture), 2, }, pool: []string{"v1", "o", "name"}, path: "v1/o/my-bucket/dir/dir2/obj", @@ -386,15 +386,15 @@ func TestMatchWithBinding(t *testing.T) { }, { ops: []int{ - int(internal.OpLitPush), 0, - int(internal.OpLitPush), 1, - int(internal.OpPush), anything, - int(internal.OpConcatN), 2, - int(internal.OpCapture), 2, - int(internal.OpLitPush), 3, - int(internal.OpPush), anything, - int(internal.OpConcatN), 1, - int(internal.OpCapture), 4, + int(utilities.OpLitPush), 0, + int(utilities.OpLitPush), 1, + int(utilities.OpPush), anything, + int(utilities.OpConcatN), 2, + int(utilities.OpCapture), 2, + int(utilities.OpLitPush), 3, + int(utilities.OpPush), anything, + int(utilities.OpConcatN), 1, + int(utilities.OpCapture), 4, }, pool: []string{"v2", "b", "name", "o", "oname"}, path: "v2/b/my-bucket/o/obj", @@ -444,49 +444,49 @@ func TestPatternString(t *testing.T) { want: "/", }, { - ops: []int{int(internal.OpNop), anything}, + ops: []int{int(utilities.OpNop), anything}, want: "/", }, { - ops: []int{int(internal.OpPush), anything}, + ops: []int{int(utilities.OpPush), anything}, want: "/*", }, { - ops: []int{int(internal.OpLitPush), 0}, + ops: []int{int(utilities.OpLitPush), 0}, pool: []string{"endpoint"}, want: "/endpoint", }, { - ops: []int{int(internal.OpPushM), anything}, + ops: []int{int(utilities.OpPushM), anything}, want: "/**", }, { ops: []int{ - int(internal.OpPush), anything, - int(internal.OpConcatN), 1, + int(utilities.OpPush), anything, + int(utilities.OpConcatN), 1, }, want: "/*", }, { ops: []int{ - int(internal.OpPush), anything, - int(internal.OpConcatN), 1, - int(internal.OpCapture), 0, + int(utilities.OpPush), anything, + int(utilities.OpConcatN), 1, + int(utilities.OpCapture), 0, }, pool: []string{"name"}, want: "/{name=*}", }, { ops: []int{ - int(internal.OpLitPush), 0, - int(internal.OpLitPush), 1, - int(internal.OpPush), anything, - int(internal.OpConcatN), 2, - int(internal.OpCapture), 2, - int(internal.OpLitPush), 3, - int(internal.OpPushM), anything, - int(internal.OpConcatN), 2, - int(internal.OpCapture), 4, + int(utilities.OpLitPush), 0, + int(utilities.OpLitPush), 1, + int(utilities.OpPush), anything, + int(utilities.OpConcatN), 2, + int(utilities.OpCapture), 2, + int(utilities.OpLitPush), 3, + int(utilities.OpPushM), anything, + int(utilities.OpConcatN), 2, + int(utilities.OpCapture), 4, }, pool: []string{"v1", "buckets", "bucket_name", "objects", "name"}, want: "/v1/{bucket_name=buckets/*}/{name=objects/**}", diff --git a/runtime/query.go b/runtime/query.go index 67448cb7096..82b07886ca3 100644 --- a/runtime/query.go +++ b/runtime/query.go @@ -6,14 +6,14 @@ import ( "reflect" "strings" - "github.com/gengo/grpc-gateway/internal" + "github.com/gengo/grpc-gateway/utilities" "github.com/golang/glog" "github.com/golang/protobuf/proto" ) // PopulateQueryParameters populates "values" into "msg". // A value is ignored if its key starts with one of the elements in "filters". -func PopulateQueryParameters(msg proto.Message, values url.Values, filter *internal.DoubleArray) error { +func PopulateQueryParameters(msg proto.Message, values url.Values, filter *utilities.DoubleArray) error { for key, values := range values { fieldPath := strings.Split(key, ".") if filter.HasCommonPrefix(fieldPath) { @@ -44,7 +44,7 @@ func populateFieldValueFromPath(msg proto.Message, fieldPath []string, values [] if !isLast && m.Kind() != reflect.Struct { return fmt.Errorf("non-aggregate type in the mid of path: %s", strings.Join(fieldPath, ".")) } - f := m.FieldByName(internal.PascalFromSnake(fieldName)) + f := m.FieldByName(utilities.PascalFromSnake(fieldName)) if !f.IsValid() { glog.Warningf("field not found in %T: %s", msg, strings.Join(fieldPath, ".")) return nil diff --git a/runtime/query_test.go b/runtime/query_test.go index 1dc4a990633..4f0aad80b62 100644 --- a/runtime/query_test.go +++ b/runtime/query_test.go @@ -4,7 +4,7 @@ import ( "net/url" "testing" - "github.com/gengo/grpc-gateway/internal" + "github.com/gengo/grpc-gateway/utilities" "github.com/gengo/grpc-gateway/runtime" "github.com/golang/protobuf/proto" ) @@ -12,7 +12,7 @@ import ( func TestPopulateParameters(t *testing.T) { for _, spec := range []struct { values url.Values - filter *internal.DoubleArray + filter *utilities.DoubleArray want proto.Message }{ { @@ -27,7 +27,7 @@ func TestPopulateParameters(t *testing.T) { "string_value": {"str"}, "repeated_value": {"a", "b", "c"}, }, - filter: internal.NewDoubleArray(nil), + filter: utilities.NewDoubleArray(nil), want: &proto3Message{ FloatValue: 1.5, DoubleValue: 2.5, @@ -52,7 +52,7 @@ func TestPopulateParameters(t *testing.T) { "string_value": {"str"}, "repeated_value": {"a", "b", "c"}, }, - filter: internal.NewDoubleArray(nil), + filter: utilities.NewDoubleArray(nil), want: &proto2Message{ FloatValue: proto.Float32(1.5), DoubleValue: proto.Float64(2.5), @@ -73,7 +73,7 @@ func TestPopulateParameters(t *testing.T) { "nested.string_value": {"u"}, "nested_non_null.string_value": {"v"}, }, - filter: internal.NewDoubleArray(nil), + filter: utilities.NewDoubleArray(nil), want: &proto3Message{ Nested: &proto2Message{ Nested: &proto3Message{ @@ -94,7 +94,7 @@ func TestPopulateParameters(t *testing.T) { values: url.Values{ "uint64_value": {"1", "2", "3", "4", "5"}, }, - filter: internal.NewDoubleArray(nil), + filter: utilities.NewDoubleArray(nil), want: &proto3Message{ Uint64Value: 1, }, @@ -116,7 +116,7 @@ func TestPopulateParameters(t *testing.T) { func TestPopulateParametersWithFilters(t *testing.T) { for _, spec := range []struct { values url.Values - filter *internal.DoubleArray + filter *utilities.DoubleArray want proto.Message }{ { @@ -125,7 +125,7 @@ func TestPopulateParametersWithFilters(t *testing.T) { "string_value": {"str"}, "repeated_value": {"a", "b", "c"}, }, - filter: internal.NewDoubleArray([][]string{ + filter: utilities.NewDoubleArray([][]string{ {"bool_value"}, {"repeated_value"}, }), want: &proto3Message{ @@ -139,7 +139,7 @@ func TestPopulateParametersWithFilters(t *testing.T) { "nested.string_value": {"str"}, "string_value": {"str"}, }, - filter: internal.NewDoubleArray([][]string{ + filter: utilities.NewDoubleArray([][]string{ {"nested"}, }), want: &proto3Message{ @@ -153,7 +153,7 @@ func TestPopulateParametersWithFilters(t *testing.T) { "nested.string_value": {"str"}, "string_value": {"str"}, }, - filter: internal.NewDoubleArray([][]string{ + filter: utilities.NewDoubleArray([][]string{ {"nested", "nested"}, }), want: &proto3Message{ @@ -170,7 +170,7 @@ func TestPopulateParametersWithFilters(t *testing.T) { "nested.string_value": {"str"}, "string_value": {"str"}, }, - filter: internal.NewDoubleArray([][]string{ + filter: utilities.NewDoubleArray([][]string{ {"nested", "nested", "string_value"}, }), want: &proto3Message{ diff --git a/utilities/doc.go b/utilities/doc.go new file mode 100644 index 00000000000..cf79a4d5886 --- /dev/null +++ b/utilities/doc.go @@ -0,0 +1,2 @@ +// Package utilities provides members for internal use in grpc-gateway. +package utilities diff --git a/internal/name.go b/utilities/name.go similarity index 94% rename from internal/name.go rename to utilities/name.go index a6d771936c9..e99648c3337 100644 --- a/internal/name.go +++ b/utilities/name.go @@ -1,4 +1,4 @@ -package internal +package utilities import ( "strings" diff --git a/internal/name_test.go b/utilities/name_test.go similarity index 58% rename from internal/name_test.go rename to utilities/name_test.go index 7b1b6b62bbe..a33dde74e8b 100644 --- a/internal/name_test.go +++ b/utilities/name_test.go @@ -1,9 +1,9 @@ -package internal_test +package utilities_test import ( "testing" - "github.com/gengo/grpc-gateway/internal" + "github.com/gengo/grpc-gateway/utilities" ) func TestPascalToSnake(t *testing.T) { @@ -14,9 +14,9 @@ func TestPascalToSnake(t *testing.T) { {input: "prefixed_value", want: "PrefixedValue"}, {input: "foo_id", want: "FooId"}, } { - got := internal.PascalFromSnake(spec.input) + got := utilities.PascalFromSnake(spec.input) if got != spec.want { - t.Errorf("internal.PascalFromSnake(%q) = %q; want %q", spec.input, got, spec.want) + t.Errorf("utilities.PascalFromSnake(%q) = %q; want %q", spec.input, got, spec.want) } } } diff --git a/internal/pattern.go b/utilities/pattern.go similarity index 97% rename from internal/pattern.go rename to utilities/pattern.go index 99680fc3851..28ad9461f86 100644 --- a/internal/pattern.go +++ b/utilities/pattern.go @@ -1,4 +1,4 @@ -package internal +package utilities // An OpCode is a opcode of compiled path patterns. type OpCode int diff --git a/internal/trie.go b/utilities/trie.go similarity index 99% rename from internal/trie.go rename to utilities/trie.go index 03f80d72f8a..c2b7b30dd91 100644 --- a/internal/trie.go +++ b/utilities/trie.go @@ -1,4 +1,4 @@ -package internal +package utilities import ( "sort" diff --git a/internal/trie_test.go b/utilities/trie_test.go similarity index 88% rename from internal/trie_test.go rename to utilities/trie_test.go index 0422b693598..e5d0271dc52 100644 --- a/internal/trie_test.go +++ b/utilities/trie_test.go @@ -1,30 +1,30 @@ -package internal_test +package utilities_test import ( "reflect" "testing" - "github.com/gengo/grpc-gateway/internal" + "github.com/gengo/grpc-gateway/utilities" ) func TestMaxCommonPrefix(t *testing.T) { for _, spec := range []struct { - da internal.DoubleArray + da utilities.DoubleArray tokens []string want bool }{ { - da: internal.DoubleArray{}, + da: utilities.DoubleArray{}, tokens: nil, want: false, }, { - da: internal.DoubleArray{}, + da: utilities.DoubleArray{}, tokens: []string{"foo"}, want: false, }, { - da: internal.DoubleArray{ + da: utilities.DoubleArray{ Encoding: map[string]int{ "foo": 0, }, @@ -35,7 +35,7 @@ func TestMaxCommonPrefix(t *testing.T) { want: false, }, { - da: internal.DoubleArray{ + da: utilities.DoubleArray{ Encoding: map[string]int{ "foo": 0, }, @@ -46,7 +46,7 @@ func TestMaxCommonPrefix(t *testing.T) { want: true, }, { - da: internal.DoubleArray{ + da: utilities.DoubleArray{ Encoding: map[string]int{ "foo": 0, }, @@ -58,7 +58,7 @@ func TestMaxCommonPrefix(t *testing.T) { }, { // foo|bar - da: internal.DoubleArray{ + da: utilities.DoubleArray{ Encoding: map[string]int{ "foo": 0, "bar": 1, @@ -76,7 +76,7 @@ func TestMaxCommonPrefix(t *testing.T) { }, { // foo|bar - da: internal.DoubleArray{ + da: utilities.DoubleArray{ Encoding: map[string]int{ "foo": 0, "bar": 1, @@ -94,7 +94,7 @@ func TestMaxCommonPrefix(t *testing.T) { }, { // foo|bar - da: internal.DoubleArray{ + da: utilities.DoubleArray{ Encoding: map[string]int{ "foo": 0, "bar": 1, @@ -112,7 +112,7 @@ func TestMaxCommonPrefix(t *testing.T) { }, { // foo|bar - da: internal.DoubleArray{ + da: utilities.DoubleArray{ Encoding: map[string]int{ "foo": 0, "bar": 1, @@ -130,7 +130,7 @@ func TestMaxCommonPrefix(t *testing.T) { }, { // foo|foo\.bar|bar - da: internal.DoubleArray{ + da: utilities.DoubleArray{ Encoding: map[string]int{ "foo": 0, "bar": 1, @@ -150,7 +150,7 @@ func TestMaxCommonPrefix(t *testing.T) { }, { // foo|foo\.bar|bar - da: internal.DoubleArray{ + da: utilities.DoubleArray{ Encoding: map[string]int{ "foo": 0, "bar": 1, @@ -170,7 +170,7 @@ func TestMaxCommonPrefix(t *testing.T) { }, { // foo|foo\.bar|bar - da: internal.DoubleArray{ + da: utilities.DoubleArray{ Encoding: map[string]int{ "foo": 0, "bar": 1, @@ -190,7 +190,7 @@ func TestMaxCommonPrefix(t *testing.T) { }, { // foo|foo\.bar|bar - da: internal.DoubleArray{ + da: utilities.DoubleArray{ Encoding: map[string]int{ "foo": 0, "bar": 1, @@ -210,7 +210,7 @@ func TestMaxCommonPrefix(t *testing.T) { }, { // foo|foo\.bar|bar - da: internal.DoubleArray{ + da: utilities.DoubleArray{ Encoding: map[string]int{ "foo": 0, "bar": 1, @@ -239,16 +239,16 @@ func TestMaxCommonPrefix(t *testing.T) { func TestAdd(t *testing.T) { for _, spec := range []struct { tokens [][]string - want internal.DoubleArray + want utilities.DoubleArray }{ { - want: internal.DoubleArray{ + want: utilities.DoubleArray{ Encoding: make(map[string]int), }, }, { tokens: [][]string{{"foo"}}, - want: internal.DoubleArray{ + want: utilities.DoubleArray{ Encoding: map[string]int{"foo": 0}, Base: []int{1, 1, 0}, Check: []int{0, 1, 2}, @@ -259,7 +259,7 @@ func TestAdd(t *testing.T) { }, { tokens: [][]string{{"foo"}, {"bar"}}, - want: internal.DoubleArray{ + want: utilities.DoubleArray{ Encoding: map[string]int{ "foo": 0, "bar": 1, @@ -275,7 +275,7 @@ func TestAdd(t *testing.T) { }, { tokens: [][]string{{"foo", "bar"}, {"foo", "baz"}}, - want: internal.DoubleArray{ + want: utilities.DoubleArray{ Encoding: map[string]int{ "foo": 0, "bar": 1, @@ -293,7 +293,7 @@ func TestAdd(t *testing.T) { }, { tokens: [][]string{{"foo", "bar"}, {"foo", "baz"}, {"qux"}}, - want: internal.DoubleArray{ + want: utilities.DoubleArray{ Encoding: map[string]int{ "foo": 0, "bar": 1, @@ -318,7 +318,7 @@ func TestAdd(t *testing.T) { {"foo", "baz", "bar"}, {"qux", "foo"}, }, - want: internal.DoubleArray{ + want: utilities.DoubleArray{ Encoding: map[string]int{ "foo": 0, "bar": 1, @@ -340,7 +340,7 @@ func TestAdd(t *testing.T) { }, }, } { - da := internal.NewDoubleArray(spec.tokens) + da := utilities.NewDoubleArray(spec.tokens) if got, want := da.Encoding, spec.want.Encoding; !reflect.DeepEqual(got, want) { t.Errorf("da.Encoding = %v; want %v; tokens = %#v", got, want, spec.tokens) }