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

[GitHub] mrutkows closed pull request #877: Adding annotation conversion

mrutkows closed pull request #877: Adding annotation conversion
URL: https://github.com/apache/incubator-openwhisk-wskdeploy/pull/877
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/parsers/manifest_parser.go b/parsers/manifest_parser.go
index 3403820d..c275731f 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 113049c6..087edb13 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 00000000..588cab14
--- /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 8b3ce5d4..c22781e2 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 00000000..00367078
--- /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)
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services