Created
September 26, 2018 16:03
-
-
Save komuw/7b2313a4ee20d2d1dde14de1cbca894b to your computer and use it in GitHub Desktop.
lambda shim
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import json | |
proc = subprocess.Popen(['./main'], | |
stdin=subprocess.PIPE, | |
stdout=subprocess.PIPE, | |
universal_newlines=True, | |
bufsize=1) | |
def id_generator(): | |
n = 0 | |
while True: | |
yield n | |
n += 1 | |
ids = id_generator() | |
def handle(event, context): | |
# write event | |
# TODO: context is not JSON serializable | |
proc.stdin.write( | |
json.dumps( | |
{ | |
'id': ids.send(None), | |
'event': event, | |
'context': {} | |
} | |
) | |
) | |
# read event | |
line = proc.stdout.readline() | |
event = json.loads(line) | |
# respond | |
# TODO: raise exception on error | |
return event['value'] | |
event_value = handle(event="my_event", context={"hello": "world"}) | |
print("event_value::", event_value) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
) | |
func main() { | |
type Err struct { | |
Error string | |
} | |
errr := Err{Error: "unable to json Marshal"} | |
jsonErr, _ := json.Marshal(errr) | |
type Out struct { | |
Name string | |
Age int32 | |
Value string `json:"value,omitempty"` | |
} | |
out := Out{Name: "Komu", Age: 30, Value: "The real thing we want."} | |
b, err := json.Marshal(out) | |
if err != nil { | |
fmt.Print(string(jsonErr)) | |
} | |
fmt.Print(string(b)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment