This commit is contained in:
Thomas Pelletier
2017-12-22 12:45:48 +01:00
committed by GitHub
parent 861c4734ac
commit 0131db6d73
4 changed files with 39 additions and 42 deletions
+31 -31
View File
@@ -23,19 +23,19 @@ var escapeSequenceMap = map[rune]rune{
type parseKeyState int
const (
BARE parseKeyState = iota
BASIC
LITERAL
ESC
UNICODE_4
UNICODE_8
bare parseKeyState = iota
basic
literal
esc
unicode4
unicode8
)
func parseKey(key string) ([]string, error) {
groups := []string{}
var buffer bytes.Buffer
var hex bytes.Buffer
state := BARE
state := bare
wasInQuotes := false
ignoreSpace := true
expectDot := false
@@ -48,66 +48,66 @@ func parseKey(key string) ([]string, error) {
ignoreSpace = false
}
if state == ESC {
if state == esc {
if char == 'u' {
state = UNICODE_4
state = unicode4
hex.Reset()
} else if char == 'U' {
state = UNICODE_8
state = unicode8
hex.Reset()
} else if newChar, ok := escapeSequenceMap[char]; ok {
buffer.WriteRune(newChar)
state = BASIC
state = basic
} else {
return nil, fmt.Errorf(`invalid escape sequence \%c`, char)
}
continue
}
if state == UNICODE_4 || state == UNICODE_8 {
if state == unicode4 || state == unicode8 {
if isHexDigit(char) {
hex.WriteRune(char)
}
if (state == UNICODE_4 && hex.Len() == 4) || (state == UNICODE_8 && hex.Len() == 8) {
if (state == unicode4 && hex.Len() == 4) || (state == unicode8 && hex.Len() == 8) {
if value, err := strconv.ParseInt(hex.String(), 16, 32); err == nil {
buffer.WriteRune(rune(value))
} else {
return nil, err
}
state = BASIC
state = basic
}
continue
}
switch char {
case '\\':
if state == BASIC {
state = ESC
} else if state == LITERAL {
if state == basic {
state = esc
} else if state == literal {
buffer.WriteRune(char)
}
case '\'':
if state == BARE {
state = LITERAL
} else if state == LITERAL {
if state == bare {
state = literal
} else if state == literal {
groups = append(groups, buffer.String())
buffer.Reset()
wasInQuotes = true
state = BARE
state = bare
}
expectDot = false
case '"':
if state == BARE {
state = BASIC
} else if state == BASIC {
if state == bare {
state = basic
} else if state == basic {
groups = append(groups, buffer.String())
buffer.Reset()
state = BARE
state = bare
wasInQuotes = true
}
expectDot = false
case '.':
if state != BARE {
if state != bare {
buffer.WriteRune(char)
} else {
if !wasInQuotes {
@@ -122,13 +122,13 @@ func parseKey(key string) ([]string, error) {
wasInQuotes = false
}
case ' ':
if state == BASIC {
if state == basic {
buffer.WriteRune(char)
} else {
expectDot = true
}
default:
if state == BARE {
if state == bare {
if !isValidBareChar(char) {
return nil, fmt.Errorf("invalid bare character: %c", char)
} else if expectDot {
@@ -140,10 +140,10 @@ func parseKey(key string) ([]string, error) {
}
}
// state must be BARE at the end
if state == ESC {
// state must be bare at the end
if state == esc {
return nil, errors.New("unfinished escape sequence")
} else if state != BARE {
} else if state != bare {
return nil, errors.New("mismatched quotes")
}
+3 -6
View File
@@ -472,21 +472,18 @@ func (d *Decoder) valueFromToml(mtype reflect.Type, tval interface{}) (reflect.V
case *Tree:
if isTree(mtype) {
return d.valueFromTree(mtype, tval.(*Tree))
} else {
return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to a tree", tval, tval)
}
return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to a tree", tval, tval)
case []*Tree:
if isTreeSlice(mtype) {
return d.valueFromTreeSlice(mtype, tval.([]*Tree))
} else {
return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to trees", tval, tval)
}
return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to trees", tval, tval)
case []interface{}:
if isOtherSlice(mtype) {
return d.valueFromOtherSlice(mtype, tval.([]interface{}))
} else {
return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to a slice", tval, tval)
}
return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to a slice", tval, tval)
default:
switch mtype.Kind() {
case reflect.Bool:
+3 -3
View File
@@ -146,8 +146,8 @@ var docData = testDoc{
Second: &subdoc,
},
SubDocList: []testSubDoc{
testSubDoc{"List.First", 0},
testSubDoc{"List.Second", 0},
{"List.First", 0},
{"List.Second", 0},
},
SubDocPtrs: []*testSubDoc{&subdoc},
}
@@ -530,7 +530,7 @@ var strPtr = []*string{&str1, &str2}
var strPtr2 = []*[]*string{&strPtr}
var nestedTestData = nestedMarshalTestStruct{
String: [][]string{[]string{"Five", "Six"}, []string{"One", "Two"}},
String: [][]string{{"Five", "Six"}, {"One", "Two"}},
StringPtr: &strPtr2,
}
+2 -2
View File
@@ -60,7 +60,7 @@ func TestTreeCreateToTree(t *testing.T) {
},
"array": []string{"a", "b", "c"},
"array_uint": []uint{uint(1), uint(2)},
"array_table": []map[string]interface{}{map[string]interface{}{"sub_map": 52}},
"array_table": []map[string]interface{}{{"sub_map": 52}},
"array_times": []time.Time{time.Now(), time.Now()},
"map_times": map[string]time.Time{"now": time.Now()},
"custom_string_map_key": map[customString]interface{}{customString("custom"): "custom"},
@@ -97,7 +97,7 @@ func TestTreeCreateToTreeInvalidArrayMemberType(t *testing.T) {
}
func TestTreeCreateToTreeInvalidTableGroupType(t *testing.T) {
_, err := TreeFromMap(map[string]interface{}{"foo": []map[string]interface{}{map[string]interface{}{"hello": t}}})
_, err := TreeFromMap(map[string]interface{}{"foo": []map[string]interface{}{{"hello": t}}})
expected := "cannot convert type *testing.T to Tree"
if err.Error() != expected {
t.Fatalf("expected error %s, got %s", expected, err.Error())