You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwhisk.apache.org by ra...@apache.org on 2018/03/24 20:07:35 UTC

[incubator-openwhisk-client-go] branch master updated: adding start library for easy implementation of Go actions (#70)

This is an automated email from the ASF dual-hosted git repository.

rabbah pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-openwhisk-client-go.git


The following commit(s) were added to refs/heads/master by this push:
     new dc9ed92  adding start library for easy implementation of Go actions (#70)
dc9ed92 is described below

commit dc9ed920b9d08d20318ae87154de5b172e665cde
Author: Sciabarra.com ltd <30...@users.noreply.github.com>
AuthorDate: Sat Mar 24 20:07:33 2018 +0000

    adding start library for easy implementation of Go actions (#70)
    
    A library that implements an read-execute-write loop for convenience of testing functions in Go.
---
 whisk/start.go      | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 whisk/start_test.go | 55 +++++++++++++++++++++++++++++++++++++
 2 files changed, 134 insertions(+)

diff --git a/whisk/start.go b/whisk/start.go
new file mode 100644
index 0000000..c5f29a8
--- /dev/null
+++ b/whisk/start.go
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package whisk
+
+import (
+	"bufio"
+	"encoding/json"
+	"fmt"
+	"io"
+	"log"
+	"os"
+)
+
+// ActionFunction is the signature of an action in OpenWhisk
+type ActionFunction func(event json.RawMessage) (json.RawMessage, error)
+
+// actual implementation of a read-eval-print-loop
+func repl(fn ActionFunction, in io.Reader, out io.Writer) {
+	// read loop
+	reader := bufio.NewReader(in)
+	for {
+		event, err := reader.ReadBytes('\n')
+		if err != nil {
+			break
+		}
+		result, err := fn(event)
+		if err != nil {
+			fmt.Fprintf(out, "{ error: %q}\n", err.Error())
+			continue
+		}
+		fmt.Fprintln(out, string(result))
+	}
+}
+
+// Start will start a loop reading in stdin and outputting in fd3
+// This is expected to be uses for implementing Go actions
+func Start(fn ActionFunction) {
+	out := os.NewFile(3, "pipe")
+	defer out.Close()
+	repl(fn, os.Stdin, out)
+}
+
+// StartWithArgs will execute the function for each arg
+// If there are no args it will start a read-write loop on the function
+// Expected to be used as starting point for implementing Go Actions
+// as whisk.StartWithArgs(function, os.Args[:1])
+// if args are 2 (command and one parameter) it will invoke the function once
+// otherwise it will stat the function in a read-write loop
+func StartWithArgs(action ActionFunction, args []string) {
+	// handle command line argument
+	if len(args) > 0 {
+		for _, arg := range args {
+			log.Println(arg)
+			result, err := action([]byte(arg))
+			if err == nil {
+				fmt.Println(string(result))
+			} else {
+				log.Println(err)
+			}
+		}
+		return
+	}
+	Start(action)
+}
diff --git a/whisk/start_test.go b/whisk/start_test.go
new file mode 100644
index 0000000..4f9f803
--- /dev/null
+++ b/whisk/start_test.go
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package whisk
+
+import (
+	"bytes"
+	"encoding/json"
+	"fmt"
+	"os"
+)
+
+func hello(event json.RawMessage) (json.RawMessage, error) {
+	var obj map[string]interface{}
+	json.Unmarshal(event, &obj)
+	name, ok := obj["name"].(string)
+	if !ok {
+		name = "Stranger"
+	}
+	fmt.Printf("name=%s\n", name)
+	msg := map[string]string{"message": ("Hello, " + name + "!")}
+	return json.Marshal(msg)
+}
+
+func Example_repl() {
+	in := bytes.NewBufferString("{\"name\":\"Mike\"}\nerr\n")
+	repl(hello, in, os.Stdout)
+	// Output:
+	// name=Mike
+	// {"message":"Hello, Mike!"}
+	// name=Stranger
+	// {"message":"Hello, Stranger!"}
+}
+
+func ExampleStart() {
+	StartWithArgs(hello, []string{"{\"name\":\"Mike\"}", "err"})
+	// Output:
+	// name=Mike
+	// {"message":"Hello, Mike!"}
+	// name=Stranger
+	// {"message":"Hello, Stranger!"}
+}

-- 
To stop receiving notification emails like this one, please contact
rabbah@apache.org.