mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-04 19:35:38 +00:00
wip
This commit is contained in:
parent
9e17cd683f
commit
69076dfe81
@ -3,6 +3,7 @@ package yqlib
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"math/big"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -112,11 +113,16 @@ func convertHclExprToNode(expr hclsyntax.Expression, src []byte) *CandidateNode
|
|||||||
b := v.True()
|
b := v.True()
|
||||||
return createScalarNode(b, strconv.FormatBool(b))
|
return createScalarNode(b, strconv.FormatBool(b))
|
||||||
case v.Type() == cty.Number:
|
case v.Type() == cty.Number:
|
||||||
// represent numbers as float string
|
// prefer integers when the numeric value is integral
|
||||||
bf := v.AsBigFloat()
|
bf := v.AsBigFloat()
|
||||||
if bf == nil {
|
if bf == nil {
|
||||||
// fallback to string
|
// fallback to string
|
||||||
return createScalarNode(nil, v.GoString())
|
return createStringScalarNode(v.GoString())
|
||||||
|
}
|
||||||
|
// check if bf represents an exact integer
|
||||||
|
if intVal, acc := bf.Int(nil); acc == big.Exact {
|
||||||
|
s := intVal.String()
|
||||||
|
return createScalarNode(int64(intVal.Int64()), s)
|
||||||
}
|
}
|
||||||
s := bf.Text('g', -1)
|
s := bf.Text('g', -1)
|
||||||
return createScalarNode(0.0, s)
|
return createScalarNode(0.0, s)
|
||||||
@ -144,7 +150,7 @@ func convertHclExprToNode(expr hclsyntax.Expression, src []byte) *CandidateNode
|
|||||||
default:
|
default:
|
||||||
// fallback to string
|
// fallback to string
|
||||||
s := v.GoString()
|
s := v.GoString()
|
||||||
return createScalarNode(nil, s)
|
return createStringScalarNode(s)
|
||||||
}
|
}
|
||||||
case *hclsyntax.TemplateExpr:
|
case *hclsyntax.TemplateExpr:
|
||||||
// join parts; if single literal, return that string
|
// join parts; if single literal, return that string
|
||||||
@ -177,9 +183,9 @@ func convertHclExprToNode(expr hclsyntax.Expression, src []byte) *CandidateNode
|
|||||||
end := r.End.Byte
|
end := r.End.Byte
|
||||||
if start > 0 && end >= start && end <= len(src) {
|
if start > 0 && end >= start && end <= len(src) {
|
||||||
text := string(src[start-1 : end])
|
text := string(src[start-1 : end])
|
||||||
return createScalarNode(nil, text)
|
return createStringScalarNode(text)
|
||||||
}
|
}
|
||||||
return createScalarNode(nil, fmt.Sprintf("%v", expr))
|
return createStringScalarNode(fmt.Sprintf("%v", expr))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -196,7 +202,11 @@ func convertCtyValueToNode(v cty.Value) *CandidateNode {
|
|||||||
case v.Type() == cty.Number:
|
case v.Type() == cty.Number:
|
||||||
bf := v.AsBigFloat()
|
bf := v.AsBigFloat()
|
||||||
if bf == nil {
|
if bf == nil {
|
||||||
return createScalarNode(nil, v.GoString())
|
return createStringScalarNode(v.GoString())
|
||||||
|
}
|
||||||
|
if intVal, acc := bf.Int(nil); acc == big.Exact {
|
||||||
|
s := intVal.String()
|
||||||
|
return createScalarNode(int64(intVal.Int64()), s)
|
||||||
}
|
}
|
||||||
s := bf.Text('g', -1)
|
s := bf.Text('g', -1)
|
||||||
return createScalarNode(0.0, s)
|
return createScalarNode(0.0, s)
|
||||||
@ -219,7 +229,7 @@ func convertCtyValueToNode(v cty.Value) *CandidateNode {
|
|||||||
}
|
}
|
||||||
return m
|
return m
|
||||||
default:
|
default:
|
||||||
return createScalarNode(nil, v.GoString())
|
return createStringScalarNode(v.GoString())
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,6 +5,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
|
|||||||
@ -13,6 +13,42 @@ var hclFormatScenarios = []formatScenario{
|
|||||||
expected: "io_mode: async\n",
|
expected: "io_mode: async\n",
|
||||||
scenarioType: "decode",
|
scenarioType: "decode",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
description: "number attribute",
|
||||||
|
input: `port = 8080`,
|
||||||
|
expected: "port: 8080\n",
|
||||||
|
scenarioType: "decode",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "float attribute",
|
||||||
|
input: `pi = 3.14`,
|
||||||
|
expected: "pi: 3.14\n",
|
||||||
|
scenarioType: "decode",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "boolean attribute",
|
||||||
|
input: `enabled = true`,
|
||||||
|
expected: "enabled: true\n",
|
||||||
|
scenarioType: "decode",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "list of strings",
|
||||||
|
input: `tags = ["a", "b"]`,
|
||||||
|
expected: "tags: ' [\"a\", \"b\"]'\n",
|
||||||
|
scenarioType: "decode",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "object/map attribute",
|
||||||
|
input: `obj = { a = 1, b = "two" }`,
|
||||||
|
expected: "obj: ' { a = 1, b = \"two\" }'\n",
|
||||||
|
scenarioType: "decode",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "nested block",
|
||||||
|
input: `server { port = 8080 }`,
|
||||||
|
expected: "server:\n port: 8080\n",
|
||||||
|
scenarioType: "decode",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func testHclScenario(t *testing.T, s formatScenario) {
|
func testHclScenario(t *testing.T, s formatScenario) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user