From ecaa5eefbf77e74d270a0541c8b77cec24afbb3a Mon Sep 17 00:00:00 2001 From: Denis Makogon Date: Tue, 26 Sep 2017 15:21:58 +0300 Subject: [PATCH] Cleaning up code Getting rid of request url, call id, method: all of them are redundant and available through env --- api/agent/protocol/factory.go | 2 +- api/agent/protocol/json.go | 14 ++----------- docs/function-format.md | 13 +----------- examples/formats/json/go/func.go | 36 +++++++++----------------------- 4 files changed, 14 insertions(+), 51 deletions(-) diff --git a/api/agent/protocol/factory.go b/api/agent/protocol/factory.go index 5fb5bb2e1..aab124195 100644 --- a/api/agent/protocol/factory.go +++ b/api/agent/protocol/factory.go @@ -47,7 +47,7 @@ func (p *Protocol) UnmarshalJSON(b []byte) error { case HTTP: *p = HTTP case JSON: - *p = JSON + *p = JSON default: return errInvalidProtocol } diff --git a/api/agent/protocol/json.go b/api/agent/protocol/json.go index 15b5fbc28..5531883e6 100644 --- a/api/agent/protocol/json.go +++ b/api/agent/protocol/json.go @@ -11,10 +11,7 @@ import ( // JSONInput is what's sent into the function // All HTTP request headers should be set in env type JSONInput struct { - RequestURL string `json:"request_url"` - CallID string `json:"call_id"` - Method string `json:"method"` - Body string `json:"body"` + Body string `json:"body"` } // JSONOutput function must return this format @@ -35,10 +32,6 @@ func (p *JSONProtocol) IsStreamable() bool { } func (h *JSONProtocol) Dispatch(w io.Writer, req *http.Request) error { - reqURL := req.Header.Get("REQUEST_URL") - method := req.Header.Get("METHOD") - callID := req.Header.Get("CALL_ID") - // TODO content-length or chunked encoding var body bytes.Buffer if req.Body != nil { @@ -50,10 +43,7 @@ func (h *JSONProtocol) Dispatch(w io.Writer, req *http.Request) error { // convert to JSON func format jin := &JSONInput{ - RequestURL: reqURL, - Method: method, - CallID: callID, - Body: body.String(), + Body: body.String(), } b, err := json.Marshal(jin) if err != nil { diff --git a/docs/function-format.md b/docs/function-format.md index 2a54d1088..72b1abad5 100644 --- a/docs/function-format.md +++ b/docs/function-format.md @@ -62,7 +62,7 @@ The header keys and values would be populated with information about the functio Pros: -* Streamable +* Supports streaming * Common format Cons: @@ -78,21 +78,10 @@ An easy to parse JSON structure. ```json { - "request_url": "http://....", - "call_id": "abc123", - "method": "GET", "body": { "some": "input" } } -{ - "request_url":"http://....", - "call_id": "edf456", - "method": "GET", - "body": { - "other": "input" - } -} ``` Pros: diff --git a/examples/formats/json/go/func.go b/examples/formats/json/go/func.go index feef0fe3a..1ddb01ef7 100644 --- a/examples/formats/json/go/func.go +++ b/examples/formats/json/go/func.go @@ -12,51 +12,35 @@ type Person struct { } type JSONInput struct { - RequestURL string `json:"request_url"` - CallID string `json:"call_id"` - Method string `json:"method"` - Body string `json:"body"` -} - -func (a *JSONInput) String() string { - return fmt.Sprintf("request_url=%s\ncall_id=%s\nmethod=%s\n\nbody=%s", - a.RequestURL, a.CallID, a.Method, a.Body) -} - -type JSONOutput struct { - StatusCode int `json:"status"` Body string `json:"body"` } +type JSONOutput struct { + StatusCode int `json:"status"` + Body string `json:"body"` +} + func main() { - // p := &Person{Name: "World"} - // json.Unmarshal(os.Stdin).Decode(p) - // mapD := map[string]string{"message": fmt.Sprintf("Hello %s", p.Name)} - // mapB, _ := json.Marshal(mapD) - // fmt.Println(string(mapB)) dec := json.NewDecoder(os.Stdin) enc := json.NewEncoder(os.Stdout) - var loopCounter = 0 for { - loopCounter++ - log.Println("loopCounter:", loopCounter) in := &JSONInput{} if err := dec.Decode(in); err != nil { log.Fatalln(err) return } - log.Println("JSONInput: ", in) person := Person{} - if in.Body != "" { + if in.Body != "" { if err := json.Unmarshal([]byte(in.Body), &person); err != nil { log.Fatalln(err) } - } - - log.Println("Person: ", person) + } + if person.Name == "" { + person.Name = "World" + } mapResult := map[string]string{"message": fmt.Sprintf("Hello %s", person.Name)} out := &JSONOutput{StatusCode: 200}