Skip to content

Commit 8aa69fc

Browse files
committed
Handles quoted values when writing
1 parent 690442d commit 8aa69fc

File tree

3 files changed

+28
-28
lines changed

3 files changed

+28
-28
lines changed

data_navigator_test.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"gopkg.in/yaml.v2"
66
"os"
7-
"strings"
87
"testing"
98
)
109

@@ -51,22 +50,6 @@ func TestWrite_simple(t *testing.T) {
5150
assertResult(t, "4", b["c"].(string))
5251
}
5352

54-
var getValueTests = []struct {
55-
argument string
56-
expectedResult interface{}
57-
testDescription string
58-
}{
59-
{"true", true, "boolean"},
60-
{"3.4", 3.4, "number"},
61-
}
62-
63-
func TestGetValue(t *testing.T) {
64-
for _, tt := range getValueTests {
65-
assertResultWithContext(t, tt.expectedResult, getValue(tt.argument, false), tt.testDescription)
66-
assertResultWithContext(t, tt.argument, getValue(tt.argument, true), strings.Join([]string{tt.testDescription, "with forceString"}, " "))
67-
}
68-
}
69-
7053
func assertResult(t *testing.T, expectedValue interface{}, actualValue interface{}) {
7154
if expectedValue != actualValue {
7255
t.Error("Expected <", expectedValue, "> but got <", actualValue, ">")

yaml.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,17 @@ func writeProperty(c *cli.Context) {
6464
log.Fatalf("Must provide <filename> <path_to_update> <value>")
6565
}
6666

67-
var forceString bool
68-
var argumentLength = len(c.Args())
69-
if argumentLength >= 4 && c.Args()[argumentLength-1] == "forceString" {
70-
forceString = true
71-
}
72-
7367
var paths = parsePath(c.Args()[1])
7468

75-
write(parsedData, paths[0], paths[1:len(paths)], getValue(c.Args()[2], forceString))
69+
write(parsedData, paths[0], paths[1:len(paths)], getValue(c.Args()[2]))
7670

7771
printYaml(parsedData, c.Bool("trim"))
7872
}
7973

80-
func getValue(argument string, forceString bool) interface{} {
74+
func getValue(argument string) interface{} {
8175
var value, err interface{}
82-
83-
if !forceString {
76+
var inQuotes = argument[0] == '"'
77+
if !inQuotes {
8478
value, err = strconv.ParseFloat(argument, 64)
8579
if err == nil {
8680
return value
@@ -89,8 +83,9 @@ func getValue(argument string, forceString bool) interface{} {
8983
if err == nil {
9084
return value
9185
}
86+
return argument
9287
}
93-
return argument
88+
return argument[1 : len(argument)-1]
9489
}
9590

9691
func printYaml(context interface{}, trim bool) {

yaml_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
)
6+
7+
var getValueTests = []struct {
8+
argument string
9+
expectedResult interface{}
10+
testDescription string
11+
}{
12+
{"true", true, "boolean"},
13+
{"\"true\"", "true", "boolean as string"},
14+
{"3.4", 3.4, "number"},
15+
{"\"3.4\"", "3.4", "number as string"},
16+
}
17+
18+
func TestGetValue(t *testing.T) {
19+
for _, tt := range getValueTests {
20+
assertResultWithContext(t, tt.expectedResult, getValue(tt.argument), tt.testDescription)
21+
}
22+
}

0 commit comments

Comments
 (0)