Skip to content

Commit

Permalink
Simplifying proto map conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasinsaurralde committed Aug 11, 2016
1 parent 60eea13 commit 93f7abc
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 85 deletions.
18 changes: 3 additions & 15 deletions coprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,6 @@ type CoProcessor struct {
Middleware *CoProcessMiddleware
}

func ToProtoMap(inputMap map[string][]string) map[string]*coprocess.StringSlice {
newMap := make(map[string]*coprocess.StringSlice, 0)

if inputMap != nil {
for k, v := range inputMap {
newMap[k] = &coprocess.StringSlice{v}
}
}

return newMap
}

func (c *CoProcessor) GetObjectFromRequest(r *http.Request) *coprocess.Object {

defer r.Body.Close()
Expand All @@ -92,14 +80,14 @@ func (c *CoProcessor) GetObjectFromRequest(r *http.Request) *coprocess.Object {
var miniRequestObject *coprocess.MiniRequestObject

miniRequestObject = &coprocess.MiniRequestObject{
Headers: ToProtoMap(r.Header),
Headers: ProtoMap(r.Header),
SetHeaders: make(map[string]string, 0),
DeleteHeaders: make([]string, 0),
Body: string(originalBody),
Url: r.URL.Path,
Params: ToProtoMap(r.URL.Query()),
Params: ProtoMap(r.URL.Query()),
AddParams: make(map[string]string),
ExtendedParams: ToProtoMap(nil),
ExtendedParams: ProtoMap(nil),
DeleteParams: make([]string, 0),
ReturnOverrides: &coprocess.ReturnOverrides{-1, ""},
}
Expand Down
44 changes: 20 additions & 24 deletions coprocess/bindings/python/coprocess_mini_request_object_pb2.py

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

80 changes: 39 additions & 41 deletions coprocess/coprocess_mini_request_object.pb.go

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

8 changes: 4 additions & 4 deletions coprocess/proto/coprocess_mini_request_object.proto
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
syntax = "proto3";

import "coprocess_return_overrides.proto";
import "coprocess_common.proto";
// import "coprocess_common.proto";

package coprocess;

message MiniRequestObject {
map<string, StringSlice> headers = 1;
map<string, string> headers = 1;
map<string, string> set_headers = 2;
repeated string delete_headers = 3;
string body = 4;
string url = 5;
map<string, StringSlice> params = 6;
map<string, string> params = 6;
map<string, string> add_params = 7;
map<string, StringSlice> extended_params = 8;
map<string, string> extended_params = 8;
repeated string delete_params = 9;
ReturnOverrides return_overrides = 10;
}
2 changes: 1 addition & 1 deletion coprocess/python/tyk/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ def delete_param(self, key):
self.__object__.delete_params.append(key)
def get_header(self, key):
if key in self.__object__.headers:
return self.__object__.headers[key].items[0]
return self.__object__.headers[key]
else:
return None
12 changes: 12 additions & 0 deletions coprocess_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,15 @@ func ProtoSessionState(sessionState SessionState) *coprocess.SessionState {

return session
}

func ProtoMap(inputMap map[string][]string) map[string]string {
newMap := make(map[string]string, 0)

if inputMap != nil {
for k, v := range inputMap {
newMap[k] = v[0]
}
}

return newMap
}

0 comments on commit 93f7abc

Please sign in to comment.