Unverified Commit 7b103293 authored by bryanl's avatar bryanl
Browse files

Update jsonnet printer



updating for:

* print object comprehensions
* handle sugar for object fields with string ids
Signed-off-by: default avatarbryanl <bryanliles@gmail.com>
parent 1ae91b58
......@@ -261,7 +261,7 @@
"ksonnet-gen/nodemaker",
"ksonnet-gen/printer"
]
revision = "f1aa55533283d55b2967f1e2cfb47048cf6661c8"
revision = "5e07f358ac2de69591818afa41b7623c8cc97ac0"
[[projects]]
name = "github.com/magiconair/properties"
......@@ -675,6 +675,6 @@
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "94b9d39d5d68c27c69668db799d67ce0d0e59bf8ad54f726318b2355d18271ae"
inputs-digest = "e693f795e1fe461827efc6e0d1a0a7e537142eae8440f0e0079d3dc01b4e9887"
solver-name = "gps-cdcl"
solver-version = 1
......@@ -54,7 +54,7 @@
[[constraint]]
name = "github.com/ksonnet/ksonnet-lib"
revision = "f1aa55533283d55b2967f1e2cfb47048cf6661c8"
revision = "5e07f358ac2de69591818afa41b7623c8cc97ac0"
[[constraint]]
name = "github.com/mattn/go-isatty"
......@@ -96,10 +96,6 @@
name = "github.com/yudai/gojsondiff"
revision = "9209d1532c51cabe0439993586a71c207b09a0ac"
[[constraint]]
name = "golang.org/x/crypto"
revision = "0fe963104e9d1877082f8fb38f816fcd97eb1d10"
[[constraint]]
name = "golang.org/x/oauth2"
revision = "cce311a261e6fcf29de72ca96827bdb0b7d9c9e6"
......
......@@ -43,7 +43,6 @@ type Object struct {
var _ Noder = (*Object)(nil)
// KVFromMap creates a object using a map.
// nolint: gocyclo
func KVFromMap(m map[string]interface{}) (*Object, error) {
if m == nil {
return nil, errors.New("map is nil")
......@@ -58,49 +57,50 @@ func KVFromMap(m map[string]interface{}) (*Object, error) {
o := NewObject()
for _, name := range names {
switch t := m[name].(type) {
case string, float64, int, bool:
val, err := convertValueToNoder(t)
if err != nil {
return nil, err
}
o.Set(InheritedKey(name), val)
case []interface{}:
var elements []Noder
for _, val := range t {
noder, err := convertValueToNoder(val)
if err != nil {
return nil, err
}
child, err := ValueToNoder(m[name])
if err != nil {
return nil, errors.Wrap(err, "convert value to noder")
}
elements = append(elements, noder)
}
array := NewArray(elements)
o.Set(InheritedKey(name), array)
case map[interface{}]interface{}:
newMap, err := convertMapToStringKey(t)
if err != nil {
return nil, err
}
child, err := KVFromMap(newMap)
if err != nil {
return nil, err
}
o.Set(InheritedKey(name), child)
}
o.Set(InheritedKey(name), child)
case map[string]interface{}:
child, err := KVFromMap(t)
return o, nil
}
// ValueToNoder converts a value to a Noder.
func ValueToNoder(v interface{}) (Noder, error) {
if v == nil {
return nil, errors.New("value is nil")
}
switch t := v.(type) {
case string, float64, int, bool:
return convertValueToNoder(t)
case []interface{}:
var elements []Noder
for _, val := range t {
noder, err := convertValueToNoder(val)
if err != nil {
return nil, err
}
o.Set(InheritedKey(name), child)
default:
return nil, errors.Errorf("unsupported type %T", t)
elements = append(elements, noder)
}
}
array := NewArray(elements)
return array, nil
case map[interface{}]interface{}:
newMap, err := convertMapToStringKey(t)
if err != nil {
return nil, err
}
return KVFromMap(newMap)
case map[string]interface{}:
return KVFromMap(t)
default:
return nil, errors.Errorf("unsupported type %T", t)
return o, nil
}
}
func convertMapToStringKey(m map[interface{}]interface{}) (map[string]interface{}, error) {
......@@ -806,6 +806,7 @@ func NewCallChain(links ...Chainable) *CallChain {
}
// Node converts the CallChain to a Jsonnet AST node.
// nolint: gocyclo
func (cc *CallChain) Node() ast.Node {
if len(cc.links) == 1 {
return cc.links[0].Node()
......
......@@ -237,6 +237,8 @@ func (p *printer) print(n interface{}) {
case *ast.LiteralNumber:
p.writeString(t.OriginalString)
case *ast.ObjectComp:
p.handleObjectComp(t)
case *ast.Self:
p.writeString("self")
case *ast.Var:
......@@ -307,7 +309,6 @@ func (p *printer) handleIndex(i *ast.Index) {
return
}
p.print(i.Target)
p.writeString(".")
id, err := indexID(i)
if err != nil {
......@@ -359,10 +360,16 @@ func (p *printer) handleLocalFunction(f *ast.Function) {
}
}
func fieldID(expr1 ast.Node, id *ast.Identifier) string {
func fieldID(kind ast.ObjectFieldKind, expr1 ast.Node, id *ast.Identifier) string {
if expr1 != nil {
ls := expr1.(*ast.LiteralString)
return fmt.Sprintf(`"%s"`, ls.Value)
switch t := expr1.(type) {
case *ast.LiteralString:
return fmt.Sprintf(`"%s"`, t.Value)
case *ast.Var:
return string(t.Id)
default:
panic(fmt.Sprintf("unknown Expr1 type %T", t))
}
}
if id != nil {
......@@ -372,6 +379,10 @@ func fieldID(expr1 ast.Node, id *ast.Identifier) string {
return ""
}
func (p *printer) handleObjectComp(oc *ast.ObjectComp) {
p.handleObjectField(oc)
}
func (p *printer) handleObjectField(n interface{}) {
var ofHide ast.ObjectFieldHide
var ofKind ast.ObjectFieldKind
......@@ -380,6 +391,8 @@ func (p *printer) handleObjectField(n interface{}) {
var ofSugar bool
var ofExpr2 ast.Node
var forSpec ast.ForSpec
switch t := n.(type) {
default:
p.err = errors.Errorf("unknown object field type %T", t)
......@@ -387,18 +400,28 @@ func (p *printer) handleObjectField(n interface{}) {
case ast.ObjectField:
ofHide = t.Hide
ofKind = t.Kind
ofID = fieldID(t.Expr1, t.Id)
ofID = fieldID(ofKind, t.Expr1, t.Id)
ofMethod = t.Method
ofSugar = t.SuperSugar
ofExpr2 = t.Expr2
case astext.ObjectField:
ofHide = t.Hide
ofKind = t.Kind
ofID = fieldID(t.Expr1, t.Id)
ofID = fieldID(ofKind, t.Expr1, t.Id)
ofMethod = t.Method
ofSugar = t.SuperSugar
ofExpr2 = t.Expr2
p.writeComment(t.Comment)
case *ast.ObjectComp:
field := t.Fields[0]
ofHide = field.Hide
ofKind = field.Kind
ofID = fieldID(ofKind, field.Expr1, field.Id)
ofMethod = field.Method
ofSugar = field.SuperSugar
ofExpr2 = field.Expr2
forSpec = t.Spec
}
if ofID == "" {
......@@ -454,8 +477,27 @@ func (p *printer) handleObjectField(n interface{}) {
p.writeString(" = ")
p.print(ofExpr2)
case ast.ObjectFieldStr:
p.writeString(fmt.Sprintf(`%s%s `, ofID, fieldType))
p.writeString(ofID)
if ofSugar {
p.writeByte(syntaxSugar, 1)
}
p.writeString(fieldType)
p.writeByte(space, 1)
p.print(ofExpr2)
case ast.ObjectFieldExpr:
// TODO: this can't be correct
p.writeString("{")
p.indentLevel++
p.writeByte(newline, 1)
p.writeString(fmt.Sprintf("[%s]: ", ofID))
p.print(ofExpr2)
p.writeByte(comma, 1)
p.writeByte(space, 1)
p.writeString(fmt.Sprintf("for %s in ", string(forSpec.VarName)))
p.print(forSpec.Expr)
p.indentLevel--
p.writeByte(newline, 1)
p.writeString("}")
}
}
......@@ -505,28 +547,25 @@ func (p *printer) addMethodSignature(method *ast.Function) {
p.writeString(")")
}
func literalStringValue(ls *ast.LiteralString) (string, error) {
if ls == nil {
return "", errors.New("literal string is nil")
}
return ls.Value, nil
}
func indexID(i *ast.Index) (string, error) {
if i == nil {
return "", errors.New("index is nil")
}
if i.Index != nil {
ls, ok := i.Index.(*ast.LiteralString)
if !ok {
return "", errors.New("index is not a literal string")
switch t := i.Index.(type) {
default:
return "", errors.Errorf("can't handle index type %T", t)
case *ast.LiteralString:
if t == nil {
return "", errors.New("string id is nil")
}
return fmt.Sprintf(".%s", t.Value), nil
case *ast.Var:
return fmt.Sprintf("[%s]", string(t.Id)), nil
}
return literalStringValue(ls)
} else if i.Id != nil {
return string(*i.Id), nil
return fmt.Sprintf(".%s", string(*i.Id)), nil
} else {
return "", errors.New("index and id can't both be blank")
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment