Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace custom yaml implementation with go package #563

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/newt_dump/answers/bleprph-nrf52840pdk.json
Original file line number Diff line number Diff line change
Expand Up @@ -4206,7 +4206,7 @@
"type": "raw",
"history": [
{
"value": "0xc0ffeeee",
"value": "3237998318",
"package": "@apache-mynewt-core/sys/config"
}
],
Expand Down
2 changes: 1 addition & 1 deletion .github/newt_dump/answers/btshell-nrf52840pdk.json
Original file line number Diff line number Diff line change
Expand Up @@ -3160,7 +3160,7 @@
"type": "raw",
"history": [
{
"value": "0xB7",
"value": "183",
"package": "@apache-mynewt-core/sys/shell"
}
],
Expand Down
2 changes: 1 addition & 1 deletion .github/newt_dump/answers/btshell-nrf52dk.json
Original file line number Diff line number Diff line change
Expand Up @@ -3160,7 +3160,7 @@
"type": "raw",
"history": [
{
"value": "0xB7",
"value": "183",
"package": "@apache-mynewt-core/sys/shell"
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ pkg.req_apis.CRASH_TEST_CLI:
- console
pkg.deps.CRASH_TEST_NEWTMGR:
- "@apache-mynewt-core/mgmt/mgmt"

pkg.deps.CRASH_TEST_NEWTMGR:
- "@apache-mynewt-core/encoding/json"

pkg.init:
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ require (
github.com/spf13/pflag v1.0.5
github.com/tklauser/go-sysconf v0.3.11 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
4 changes: 2 additions & 2 deletions newt/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cast"

"gopkg.in/yaml.v2"
"mynewt.apache.org/newt/newt/interfaces"
"mynewt.apache.org/newt/newt/newtutil"
"mynewt.apache.org/newt/newt/ycfg"
"mynewt.apache.org/newt/util"
"mynewt.apache.org/newt/yaml"
)

const (
Expand All @@ -60,7 +60,7 @@ func readSettings(path string) (map[string]interface{}, error) {
}

settings := map[string]interface{}{}
if err := yaml.Unmarshal(file, &settings); err != nil {
if err := yaml.UnmarshalStrict(file, &settings); err != nil {
return nil, util.FmtNewtError("Failure parsing \"%s\": %s",
path, err.Error())
}
Expand Down
13 changes: 6 additions & 7 deletions newt/pkg/localpackage.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
"mynewt.apache.org/newt/newt/repo"
"mynewt.apache.org/newt/newt/ycfg"
"mynewt.apache.org/newt/util"
"mynewt.apache.org/newt/yaml"
)

var PackageHashIgnoreDirs = map[string]bool{
Expand Down Expand Up @@ -218,7 +217,7 @@ func (pkg *LocalPackage) sequenceString(key string) string {
vals, err := pkg.PkgY.GetValStringSlice(key, nil)
util.OneTimeWarningError(err)
for _, f := range vals {
buffer.WriteString(" - " + yaml.EscapeString(f) + "\n")
buffer.WriteString(" - " + util.EscapeString(f) + "\n")
}

if buffer.Len() == 0 {
Expand Down Expand Up @@ -263,15 +262,15 @@ func (pkg *LocalPackage) Save() error {

// XXX: Just iterate ycfg object's settings rather than calling out
// cached settings individually.
file.WriteString("pkg.name: " + yaml.EscapeString(pkg.Name()) + "\n")
file.WriteString("pkg.name: " + util.EscapeString(pkg.Name()) + "\n")
file.WriteString("pkg.type: " +
yaml.EscapeString(PackageTypeNames[pkg.Type()]) + "\n")
util.EscapeString(PackageTypeNames[pkg.Type()]) + "\n")
file.WriteString("pkg.description: " +
yaml.EscapeString(pkg.Desc().Description) + "\n")
util.EscapeString(pkg.Desc().Description) + "\n")
file.WriteString("pkg.author: " +
yaml.EscapeString(pkg.Desc().Author) + "\n")
util.EscapeString(pkg.Desc().Author) + "\n")
file.WriteString("pkg.homepage: " +
yaml.EscapeString(pkg.Desc().Homepage) + "\n")
util.EscapeString(pkg.Desc().Homepage) + "\n")

file.WriteString("\n")

Expand Down
3 changes: 1 addition & 2 deletions newt/ycfg/ycfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (

"mynewt.apache.org/newt/newt/parse"
"mynewt.apache.org/newt/util"
"mynewt.apache.org/newt/yaml"
)

// YAML configuration object. This is a substitute for a viper configuration
Expand Down Expand Up @@ -729,5 +728,5 @@ func (yc *YCfg) String() string {

// YAML converts the YCfg to a map and encodes the map as YAML.
func (yc *YCfg) YAML() string {
return yaml.MapToYaml(yc.AllSettings())
return util.MapToYaml(yc.AllSettings())
}
59 changes: 59 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -1044,3 +1044,62 @@ func SliceContains(slice interface{}, elem interface{}) bool {
}
return false
}
func EscapeString(s string) string {
special := ":{}[],&*#?|-<>=!%@\\\"'"
if strings.ContainsAny(s, special) {
return "\"" + strings.Replace(s, "\"", "\\\"", -1) + "\""
} else {
return s
}
}

// Converts a key-value pair to a YAML string.
func KvToYaml(key string, val interface{}, indent int) string {
s := ""

if key != "" {
s += fmt.Sprintf("%*s%s:", indent, "", EscapeString(key))
}

switch v := val.(type) {
case []interface{}:
s += "\n"
for _, elem := range v {
s += fmt.Sprintf("%*s- %s\n", indent+4, "", KvToYaml("", elem, 0))
}

case map[interface{}]interface{}:
s += "\n"

subKeys := make([]string, 0, len(v))
for sk, _ := range v {
subKeys = append(subKeys, sk.(string))
}
sort.Strings(subKeys)

for _, sk := range subKeys {
s += KvToYaml(sk, v[sk], indent+4)
}

default:
valStr := EscapeString(fmt.Sprintf("%v", v))
s += fmt.Sprintf(" %v\n", valStr)
}

return s
}

func MapToYaml(m map[string]interface{}) string {
keys := make([]string, 0, len(m))
for k, _ := range m {
keys = append(keys, k)
}
sort.Strings(keys)

s := ""
for _, k := range keys {
s += KvToYaml(k, m[k], 0)
}

return s
}
Loading
Loading