You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by cc...@apache.org on 2016/03/04 20:32:58 UTC

[41/50] [abbrv] incubator-mynewt-newt git commit: Allow generic yaml keys to pacify viper unit tests.

Allow generic yaml keys to pacify viper unit tests.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/commit/726d9de5
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/tree/726d9de5
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/diff/726d9de5

Branch: refs/heads/master
Commit: 726d9de56c432d12831ef944bda2e9778f11e13b
Parents: 662be71
Author: Christopher Collins <cc...@gmail.com>
Authored: Mon Feb 29 16:26:59 2016 -0800
Committer: Christopher Collins <cc...@gmail.com>
Committed: Mon Feb 29 16:26:59 2016 -0800

----------------------------------------------------------------------
 yaml/decode.go | 46 ++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 40 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/726d9de5/yaml/decode.go
----------------------------------------------------------------------
diff --git a/yaml/decode.go b/yaml/decode.go
index 47f68e7..8244bd6 100644
--- a/yaml/decode.go
+++ b/yaml/decode.go
@@ -22,6 +22,7 @@ package yaml
 import (
 	"errors"
 	"fmt"
+	"strconv"
 )
 
 type decodeState int
@@ -86,10 +87,40 @@ func sequenceAdd(ctxt *decodeCtxt, value interface{}) {
 // Inserts the specified value into a mapping-context's value map.
 func mappingAdd(ctxt *decodeCtxt, key string, value interface{}) {
 	if ctxt.value == nil {
-		ctxt.value = map[string]interface{}{}
+		ctxt.value = map[interface{}]interface{}{}
 	}
 
-	ctxt.value.(map[string]interface{})[key] = value
+	ctxt.value.(map[interface{}]interface{})[key] = value
+}
+
+func genValue(strVal string) interface{} {
+	intVal, err := strconv.Atoi(strVal)
+	if err == nil {
+		return intVal
+	}
+
+	boolVal, err := strconv.ParseBool(strVal)
+	if err == nil {
+		return boolVal
+	}
+
+	return strVal
+}
+
+func stringValue(value interface{}) string {
+	switch value.(type) {
+	case int:
+		return strconv.FormatInt(value.(int64), 10)
+
+	case bool:
+		return strconv.FormatBool(value.(bool))
+
+	case string:
+		return value.(string)
+
+	default:
+		panic("unexpected type")
+	}
 }
 
 func decodeNoOp(parser *yaml_parser_t, event *yaml_event_t,
@@ -180,7 +211,7 @@ func decodeMappingStart(parser *yaml_parser_t, event *yaml_event_t,
 		if err != nil || subCtxt.state == CTXT_STATE_DONE {
 			return ctxt, err
 		}
-		key := subCtxt.value.(string)
+		key := stringValue(subCtxt.value)
 
 		subCtxt, err = decodeNextValue(parser, &ctxt)
 		if err != nil {
@@ -209,7 +240,10 @@ func decodeMappingEnd(parser *yaml_parser_t, event *yaml_event_t,
 func decodeScalar(parser *yaml_parser_t, event *yaml_event_t,
 	parentCtxt *decodeCtxt) (decodeCtxt, error) {
 
-	ctxt := decodeCtxt{state: CTXT_STATE_SCALAR, value: string(event.value)}
+	ctxt := decodeCtxt{state: CTXT_STATE_SCALAR}
+	strVal := string(event.value)
+	ctxt.value = genValue(strVal)
+
 	return ctxt, nil
 }
 
@@ -255,8 +289,8 @@ func DecodeStream(b []byte, values map[string]interface{}) error {
 		switch subCtxt.state {
 		case CTXT_STATE_MAPPING:
 			// Copy mapping to input variable.
-			for k, v := range subCtxt.value.(map[string]interface{}) {
-				values[k] = v
+			for k, v := range subCtxt.value.(map[interface{}]interface{}) {
+				values[k.(string)] = v
 			}
 
 		case CTXT_STATE_DONE: