mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
apply/make Travis's json-format branch prototype to work with latest restructured master; added StatusCode to JSONOutput server-function contract
This commit is contained in:
committed by
Denis Makogon
parent
b8d7154747
commit
b6b9b55ca9
6
examples/formats/json/go/.gitignore
vendored
Normal file
6
examples/formats/json/go/.gitignore
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
vendor/
|
||||
/go
|
||||
/app
|
||||
/__uberscript__
|
||||
|
||||
func.yaml
|
||||
3
examples/formats/json/go/README.md
Normal file
3
examples/formats/json/go/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
# Go using JSON format
|
||||
|
||||
This example uses the `json` input format.
|
||||
69
examples/formats/json/go/func.go
Normal file
69
examples/formats/json/go/func.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Person struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
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"`
|
||||
}
|
||||
|
||||
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 err := json.Unmarshal([]byte(in.Body), &person); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
}
|
||||
|
||||
log.Println("Person: ", person)
|
||||
|
||||
mapResult := map[string]string{"message": fmt.Sprintf("Hello %s", person.Name)}
|
||||
out := &JSONOutput{StatusCode: 200}
|
||||
b, _ := json.Marshal(mapResult)
|
||||
out.Body = string(b)
|
||||
if err := enc.Encode(out); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
3
examples/formats/json/go/sample.payload.json
Normal file
3
examples/formats/json/go/sample.payload.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"Name": "Johnny"
|
||||
}
|
||||
26
examples/formats/json/go/test.json
Normal file
26
examples/formats/json/go/test.json
Normal file
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"tests": [
|
||||
{
|
||||
"input": {
|
||||
"body": {
|
||||
"name": "Johnny"
|
||||
}
|
||||
},
|
||||
"output": {
|
||||
"body": {
|
||||
"message": "Hello Johnny"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"input": {
|
||||
"body": ""
|
||||
},
|
||||
"output": {
|
||||
"body": {
|
||||
"message": "Hello World"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user