You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwhisk.apache.org by mr...@apache.org on 2018/04/24 18:08:55 UTC

[incubator-openwhisk-wskdeploy] branch master updated: Adding annotation conversion (#877)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new b5664c1  Adding annotation conversion (#877)
b5664c1 is described below

commit b5664c1f971b49868e7e32ba2b6e1efd99747696
Author: Priti Desai <pd...@us.ibm.com>
AuthorDate: Tue Apr 24 11:08:53 2018 -0700

    Adding annotation conversion (#877)
    
    * Adding conversion for annotations
    
    * adding license
    
    * fixing import
---
 parsers/manifest_parser.go                         |  3 +-
 parsers/manifest_parser_test.go                    | 83 ++++++++++++++++++++++
 ...ifest_data_compose_actions_for_annotations.yaml | 38 ++++++++++
 utils/conversion.go                                | 16 ++++-
 utils/conversion_test.go                           | 80 +++++++++++++++++++++
 5 files changed, 216 insertions(+), 4 deletions(-)

diff --git a/parsers/manifest_parser.go b/parsers/manifest_parser.go
index 3403820..c275731 100644
--- a/parsers/manifest_parser.go
+++ b/parsers/manifest_parser.go
@@ -134,7 +134,8 @@ func (dm *YAMLParser) composeAnnotations(annotations map[string]interface{}) whi
 	for name, value := range annotations {
 		var keyVal whisk.KeyValue
 		keyVal.Key = name
-		keyVal.Value = wskenv.InterpolateStringWithEnvVar(value)
+		value = wskenv.InterpolateStringWithEnvVar(value)
+		keyVal.Value = utils.ConvertInterfaceValue(value)
 		listOfAnnotations = append(listOfAnnotations, keyVal)
 	}
 	return listOfAnnotations
diff --git a/parsers/manifest_parser_test.go b/parsers/manifest_parser_test.go
index 113049c..087edb1 100644
--- a/parsers/manifest_parser_test.go
+++ b/parsers/manifest_parser_test.go
@@ -52,6 +52,7 @@ const (
 	TEST_MSG_ACTION_DOCKER_KIND_MISMATCH            = "Docker action kind is set to [%s] instead of " + utils.BLACKBOX
 	TEST_MSG_ACTION_DOCKER_IMAGE_MISMATCH           = "Docker action image had a value mismatch."
 	TEST_MSG_ACTION_CODE_MISSING                    = "Action code is missing."
+	TEST_MSG_INVALID_ACTION_ANNOTATION              = "Action annotations are invalid"
 
 	// local error messages
 	TEST_ERROR_MANIFEST_PARSE_FAILURE     = "Manifest [%s]: Failed to parse."
@@ -1068,6 +1069,88 @@ func TestComposeActionsForWebAndWebExport(t *testing.T) {
 	}
 }
 
+func TestYAMLParser_ComposeActionsForAnnotations(t *testing.T) {
+	file := "../tests/dat/manifest_data_compose_actions_for_annotations.yaml"
+	p, m, _ := testLoadParseManifest(t, file)
+
+	actions, err := p.ComposeActionsFromAllPackages(m, m.Filepath, whisk.KeyValue{})
+	assert.Nil(t, err, fmt.Sprintf(TEST_ERROR_COMPOSE_ACTION_FAILURE, file))
+
+	for _, action := range actions {
+		if action.Action.Name == "hello" {
+			for _, a := range action.Action.Annotations {
+				switch a.Key {
+				// annotation_string: this is a string annotations
+				case "annotation_string":
+					assert.Equal(t, "this is a string annotations",
+						a.Value.(string), TEST_MSG_INVALID_ACTION_ANNOTATION)
+				//annotation_int: 100
+				case "annotation_int":
+					assert.Equal(t, 100,
+						a.Value.(int), TEST_MSG_INVALID_ACTION_ANNOTATION)
+				//annotation_float: 99.99
+				case "annotation_float":
+					assert.Equal(t, 99.99,
+						a.Value.(float64), TEST_MSG_INVALID_ACTION_ANNOTATION)
+				//annotation_bool: true
+				case "annotation_bool":
+					assert.Equal(t, true,
+						a.Value.(bool), TEST_MSG_INVALID_ACTION_ANNOTATION)
+				// annotation_list_1: [1, 2, 3, 4]
+				case "annotation_list_1":
+					assert.Equal(t, []interface{}{1, 2, 3, 4},
+						a.Value.([]interface{}), TEST_MSG_INVALID_ACTION_ANNOTATION)
+				//annotation_list_2: [{ "payload": "one,two,three" }, { "payload": "one,two,three", "separator": "," }]
+				case "annotation_list_2":
+					assert.Equal(t, []interface{}{map[string]interface{}{"payload": "one,two,three"}, map[string]interface{}{"payload": "one,two,three", "separator": ","}},
+						a.Value.([]interface{}), TEST_MSG_INVALID_ACTION_ANNOTATION)
+				// annotation_json_1: { "payload": "one,two,three" }
+				case "annotation_json_1":
+					assert.Equal(t, map[string]interface{}{"payload": "one,two,three"},
+						a.Value.(map[string]interface{}), TEST_MSG_INVALID_ACTION_ANNOTATION)
+				// annotation_json_2: { "payload": "one,two,three", "separator": "," }
+				case "annotation_json_2":
+					assert.Equal(t, map[string]interface{}{"payload": "one,two,three", "separator": ","},
+						a.Value.(map[string]interface{}), TEST_MSG_INVALID_ACTION_ANNOTATION)
+				// annotation_json_3: { "payload": "one,two,three", "lines": ["one", "two", "three"] }
+				case "annotation_json_3":
+					assert.Equal(t, map[string]interface{}{"payload": "one,two,three", "lines": []interface{}{"one", "two", "three"}},
+						a.Value.(map[string]interface{}), TEST_MSG_INVALID_ACTION_ANNOTATION)
+				//annotation_json_4: { "p": { "a": 1 } }
+				case "annotation_json_4":
+					assert.Equal(t, map[string]interface{}{"p": map[string]interface{}{"a": 1}},
+						a.Value.(map[string]interface{}), TEST_MSG_INVALID_ACTION_ANNOTATION)
+				//annotation_json_5: { "p": { "a": 1, "b": 2 } }
+				case "annotation_json_5":
+					assert.Equal(t, map[string]interface{}{"p": map[string]interface{}{"a": 1, "b": 2}},
+						a.Value.(map[string]interface{}), TEST_MSG_INVALID_ACTION_ANNOTATION)
+				//annotation_json_6: { "p": { "a": 1, "b": { "c": 2 } } }
+				case "annotation_json_6":
+					assert.Equal(t, map[string]interface{}{"p": map[string]interface{}{"a": 1, "b": map[string]interface{}{"c": 2}}},
+						a.Value.(map[string]interface{}), TEST_MSG_INVALID_ACTION_ANNOTATION)
+				//annotation_json_7: { "p": { "a": 1, "b": { "c": 2, "d": 3 } } }
+				case "annotation_json_7":
+					assert.Equal(t, map[string]interface{}{"p": map[string]interface{}{"a": 1, "b": map[string]interface{}{"c": 2, "d": 3}}},
+						a.Value.(map[string]interface{}), TEST_MSG_INVALID_ACTION_ANNOTATION)
+				//annotation_json_8: { "p": { "a": 1, "b": { "c": 2, "d": [3, 4] } } }
+				case "annotation_json_8":
+					assert.Equal(t, map[string]interface{}{"p": map[string]interface{}{"a": 1, "b": map[string]interface{}{"c": 2, "d": []interface{}{3, 4}}}},
+						a.Value.(map[string]interface{}), TEST_MSG_INVALID_ACTION_ANNOTATION)
+				//annotation_json_9: { "p": { "a": 99.99 } }
+				case "annotation_json_9":
+					assert.Equal(t, map[string]interface{}{"p": map[string]interface{}{"a": 99.99}},
+						a.Value.(map[string]interface{}), TEST_MSG_INVALID_ACTION_ANNOTATION)
+				//annotation_json_10: { "p": { "a": true } }
+				case "annotation_json_10":
+					assert.Equal(t, map[string]interface{}{"p": map[string]interface{}{"a": true}},
+						a.Value.(map[string]interface{}), TEST_MSG_INVALID_ACTION_ANNOTATION)
+
+				}
+			}
+		}
+	}
+}
+
 // Test 16: validate manifest_parser.ResolveParameter() method
 func TestResolveParameterForMultiLineParams(t *testing.T) {
 	paramName := "name"
diff --git a/tests/dat/manifest_data_compose_actions_for_annotations.yaml b/tests/dat/manifest_data_compose_actions_for_annotations.yaml
new file mode 100644
index 0000000..588cab1
--- /dev/null
+++ b/tests/dat/manifest_data_compose_actions_for_annotations.yaml
@@ -0,0 +1,38 @@
+#
+# 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.
+#
+
+packages:
+    helloworld:
+        actions:
+            hello:
+                function: ../src/integration/helloworld/actions/hello.js
+                annotations:
+                    annotation_string: this is a string annotations
+                    annotation_int: 100
+                    annotation_float: 99.99
+                    annotation_bool: true
+                    annotation_list_1: [1, 2, 3, 4]
+                    annotation_list_2: [{ "payload": "one,two,three" }, { "payload": "one,two,three", "separator": "," }]
+                    annotation_json_1: { "payload": "one,two,three" }
+                    annotation_json_2: { "payload": "one,two,three", "separator": "," }
+                    annotation_json_3: { "payload": "one,two,three", "lines": ["one", "two", "three"] }
+                    annotation_json_4: { "p": { "a": 1 } }
+                    annotation_json_5: { "p": { "a": 1, "b": 2 } }
+                    annotation_json_6: { "p": { "a": 1, "b": { "c": 2 } } }
+                    annotation_json_7: { "p": { "a": 1, "b": { "c": 2, "d": 3 } } }
+                    annotation_json_8: { "p": { "a": 1, "b": { "c": 2, "d": [3, 4] } } }
+                    annotation_json_9: { "p": { "a": 99.99 } }
+                    annotation_json_10: { "p": { "a": true } }
diff --git a/utils/conversion.go b/utils/conversion.go
index 8b3ce5d..c22781e 100644
--- a/utils/conversion.go
+++ b/utils/conversion.go
@@ -25,7 +25,7 @@ import (
 func convertInterfaceArray(in []interface{}) []interface{} {
 	res := make([]interface{}, len(in))
 	for i, v := range in {
-		res[i] = convertMapValue(v)
+		res[i] = ConvertInterfaceValue(v)
 	}
 	return res
 }
@@ -33,22 +33,32 @@ func convertInterfaceArray(in []interface{}) []interface{} {
 func ConvertInterfaceMap(mapIn map[interface{}]interface{}) map[string]interface{} {
 	mapOut := make(map[string]interface{})
 	for k, v := range mapIn {
-		mapOut[fmt.Sprintf("%v", k)] = convertMapValue(v)
+		mapOut[fmt.Sprintf("%v", k)] = ConvertInterfaceValue(v)
 	}
 	return mapOut
 }
 
-func convertMapValue(value interface{}) interface{} {
+func ConvertInterfaceValue(value interface{}) interface{} {
 	switch typedVal := value.(type) {
 	case []interface{}:
 		return convertInterfaceArray(typedVal)
+	case map[string]interface{}:
 	case map[interface{}]interface{}:
 		return ConvertInterfaceMap(typedVal)
+	case bool:
+	case int:
+	case int8:
+	case int16:
+	case int32:
+	case int64:
+	case float32:
+	case float64:
 	case string:
 		return typedVal
 	default:
 		return fmt.Sprintf("%v", typedVal)
 	}
+	return value
 }
 
 // TODO() add a Print function to wskprint that calls this and adds the label
diff --git a/utils/conversion_test.go b/utils/conversion_test.go
new file mode 100644
index 0000000..0036707
--- /dev/null
+++ b/utils/conversion_test.go
@@ -0,0 +1,80 @@
+/*
+ * 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 utils
+
+import (
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+)
+
+const TEST_MSG_INVALID_CONVERSION_VALUE = "Invalid conversion value"
+
+func TestConvertInterfaceValue_String(t *testing.T) {
+	v := "This is a string value"
+	assert.Equal(t, v, ConvertInterfaceValue(v), TEST_MSG_INVALID_CONVERSION_VALUE)
+}
+
+func TestConvertInterfaceValue_Bool(t *testing.T) {
+	assert.Equal(t, true, ConvertInterfaceValue(true), TEST_MSG_INVALID_CONVERSION_VALUE)
+	assert.Equal(t, false, ConvertInterfaceValue(false), TEST_MSG_INVALID_CONVERSION_VALUE)
+}
+
+func TestConvertInterfaceValue_Int(t *testing.T) {
+	assert.Equal(t, 10, ConvertInterfaceValue(10), TEST_MSG_INVALID_CONVERSION_VALUE)
+	assert.Equal(t, 1000000000000, ConvertInterfaceValue(1000000000000), TEST_MSG_INVALID_CONVERSION_VALUE)
+}
+
+func TestConvertInterfaceValue_List(t *testing.T) {
+	l1 := []interface{}{1, 2, 3, 4}
+	assert.Equal(t, l1, ConvertInterfaceValue(l1), TEST_MSG_INVALID_CONVERSION_VALUE)
+	l2 := []interface{}{map[string]interface{}{"payload": "one,two,three"}, map[string]interface{}{"payload": "one,two,three", "separator": ","}}
+	assert.Equal(t, l2, ConvertInterfaceValue(l2), TEST_MSG_INVALID_CONVERSION_VALUE)
+}
+
+func TestConvertInterfaceValue_Map(t *testing.T) {
+	json_1 := map[string]interface{}{"payload": "one,two,three"}
+	assert.Equal(t, json_1, ConvertInterfaceValue(json_1), TEST_MSG_INVALID_CONVERSION_VALUE)
+
+	json_2 := map[string]interface{}{"payload": "one,two,three", "separator": ","}
+	assert.Equal(t, json_2, ConvertInterfaceValue(json_2), TEST_MSG_INVALID_CONVERSION_VALUE)
+
+	json_3 := map[string]interface{}{"payload": "one,two,three", "lines": []interface{}{"one", "two", "three"}}
+	assert.Equal(t, json_3, ConvertInterfaceValue(json_3), TEST_MSG_INVALID_CONVERSION_VALUE)
+
+	json_4 := map[string]interface{}{"p": map[string]interface{}{"a": 1}}
+	assert.Equal(t, json_4, ConvertInterfaceValue(json_4), TEST_MSG_INVALID_CONVERSION_VALUE)
+
+	json_5 := map[string]interface{}{"p": map[string]interface{}{"a": 1, "b": 2}}
+	assert.Equal(t, json_5, ConvertInterfaceValue(json_5), TEST_MSG_INVALID_CONVERSION_VALUE)
+
+	json_6 := map[string]interface{}{"p": map[string]interface{}{"a": 1, "b": map[string]interface{}{"c": 2}}}
+	assert.Equal(t, json_6, ConvertInterfaceValue(json_6), TEST_MSG_INVALID_CONVERSION_VALUE)
+
+	json_7 := map[string]interface{}{"p": map[string]interface{}{"a": 1, "b": map[string]interface{}{"c": 2, "d": 3}}}
+	assert.Equal(t, json_7, ConvertInterfaceValue(json_7), TEST_MSG_INVALID_CONVERSION_VALUE)
+
+	json_8 := map[string]interface{}{"p": map[string]interface{}{"a": 1, "b": map[string]interface{}{"c": 2, "d": []interface{}{3, 4}}}}
+	assert.Equal(t, json_8, ConvertInterfaceValue(json_8), TEST_MSG_INVALID_CONVERSION_VALUE)
+
+	json_9 := map[string]interface{}{"p": map[string]interface{}{"a": 99.99}}
+	assert.Equal(t, json_9, ConvertInterfaceValue(json_9), TEST_MSG_INVALID_CONVERSION_VALUE)
+
+	json_10 := map[string]interface{}{"p": map[string]interface{}{"a": true}}
+	assert.Equal(t, json_10, ConvertInterfaceValue(json_10), TEST_MSG_INVALID_CONVERSION_VALUE)
+}

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