-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Refactor github.com/kubernetes/helm/pkg/strvals away #926
Comments
Played with this some and came up with a simplistic but somewhat viable replacement: https://play.golang.org/p/6Y23Fyv8gO5 Copying it here, since I'm not sure if Go playground links are permanent: package main
import (
"fmt"
"strconv"
"strings"
)
func main() {
dump(`test=mest,asdf=123, true=false`)
dump(`we are pretty liberal = with what we support, even=" quoted strings with spaces "`)
dump(`noval,noval2=,noval3= , emptyval="",someval=wat`)
dump(`test=mest,foo="bar",int=123,test=overwrite , float=3.14, sub.a=1, , sub.b=2, sub.c="Asdf"`)
dump(`test=mest,test.sub=this is an error`)
}
func dump(s string) {
res, err := strvals(s)
fmt.Printf("err=%v, res=%#v\n", err, res)
}
func strvals(s string) (map[string]interface{}, error) {
res := map[string]interface{}{}
keyvals := strings.Split(s, ",")
for _, kv := range keyvals {
if len(kv) == 0 {
continue
}
kvSplit := strings.SplitN(kv, "=", 2)
if len(kvSplit) == 1 {
kvSplit = append(kvSplit, "")
}
keyParts := strings.Split(strings.TrimSpace(kvSplit[0]), ".")
resLevel := res
for i := 0; i < len(keyParts)-1; i++ {
if newLevel, ok := resLevel[keyParts[i]]; !ok {
newLevel := map[string]interface{}{}
resLevel[keyParts[i]] = newLevel
resLevel = newLevel
} else {
if newLevelMap, ok := newLevel.(map[string]interface{}); ok {
resLevel = newLevelMap
} else {
return nil, fmt.Errorf(
"option '%s' was cannot be both %q and a map",
strings.Join(keyParts[:i+1], "."), newLevel,
)
}
}
}
resLevel[keyParts[len(keyParts)-1]] = getVal(strings.TrimSpace(kvSplit[1]))
}
return res, nil
}
func getVal(val string) interface{} {
if val == "" {
return nil
} else if iVal, err := strconv.ParseInt(val, 10, 64); err == nil {
return iVal
} else if fVal, err := strconv.ParseFloat(val, 64); err == nil {
return fVal
} else if bVal, err := strconv.ParseBool(val); err == nil {
return bVal
} else if len(val) >= 2 && val[0] == '"' && val[len(val)-1] == '"' {
return val[1 : len(val)-1]
} else {
return val
}
} running it will produce:
|
This is a requirement to drop In addition, we need to drop it from some 2 places in our code base and drop This in total drops around 12k LOC. On the other side the kafka output is making a lot of use of it to a point that redoing it is probably not ... possible. But the other places we use it (influxdb and |
I'll close this issue because we no longer depend on strvals in k6 after #2270 🎉 That said, we have hand-crafted a bunch of poor replacements in the few places where we need something like it... It will be much simpler and less error prone to have that type of config parsing to be done by a config library... So I opened grafana/croconf#16, which we'll hopefully get around to again soon 🤞 |
No description provided.
The text was updated successfully, but these errors were encountered: