2019-12-03 04:50:32 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
2019-12-16 05:46:20 +00:00
|
|
|
|
|
|
|
logging "gopkg.in/op/go-logging.v1"
|
|
|
|
yaml "gopkg.in/yaml.v3"
|
2019-12-03 04:50:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ValueParser interface {
|
2019-12-16 05:46:20 +00:00
|
|
|
Parse(argument string, customTag string) *yaml.Node
|
2019-12-03 04:50:32 +00:00
|
|
|
}
|
|
|
|
|
2019-12-16 05:46:20 +00:00
|
|
|
type valueParser struct {
|
|
|
|
log *logging.Logger
|
|
|
|
}
|
2019-12-03 04:50:32 +00:00
|
|
|
|
2019-12-16 05:46:20 +00:00
|
|
|
func NewValueParser(l *logging.Logger) ValueParser {
|
|
|
|
return &valueParser{log: l}
|
2019-12-03 04:50:32 +00:00
|
|
|
}
|
|
|
|
|
2019-12-16 05:46:20 +00:00
|
|
|
func (v *valueParser) Parse(argument string, customTag string) *yaml.Node {
|
|
|
|
var err interface{}
|
|
|
|
var tag = customTag
|
|
|
|
|
2019-12-03 04:50:32 +00:00
|
|
|
var inQuotes = len(argument) > 0 && argument[0] == '"'
|
2019-12-16 05:46:20 +00:00
|
|
|
if tag == "" && !inQuotes {
|
|
|
|
|
|
|
|
_, err = strconv.ParseBool(argument)
|
2019-12-03 04:50:32 +00:00
|
|
|
if err == nil {
|
2019-12-16 05:46:20 +00:00
|
|
|
tag = "!!bool"
|
2019-12-03 04:50:32 +00:00
|
|
|
}
|
2019-12-16 05:46:20 +00:00
|
|
|
_, err = strconv.ParseFloat(argument, 64)
|
2019-12-03 04:50:32 +00:00
|
|
|
if err == nil {
|
2019-12-16 05:46:20 +00:00
|
|
|
tag = "!!float"
|
|
|
|
}
|
|
|
|
_, err = strconv.ParseInt(argument, 10, 64)
|
|
|
|
if err == nil {
|
|
|
|
tag = "!!int"
|
|
|
|
}
|
|
|
|
|
|
|
|
if argument == "null" {
|
|
|
|
tag = "!!null"
|
2019-12-03 04:50:32 +00:00
|
|
|
}
|
|
|
|
if argument == "[]" {
|
2019-12-16 05:46:20 +00:00
|
|
|
return &yaml.Node{Tag: "!!seq", Kind: yaml.SequenceNode}
|
2019-12-03 04:50:32 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-16 05:46:20 +00:00
|
|
|
v.log.Debugf("parsed value '%v', tag: '%v'", argument, tag)
|
|
|
|
return &yaml.Node{Value: argument, Tag: tag, Kind: yaml.ScalarNode}
|
2019-12-03 04:50:32 +00:00
|
|
|
}
|