package ctydebug
import "github.com/zclconf/go-cty-debug/ctydebug"
Package ctydebug contains some utilities for cty that are aimed at debugging and test code rather than at production code.
A common characteristic of the functions here is that they are optimized for ease of use by having good defaults, as opposed to flexibility via lots of configuration arguments.
Don't depend on the exact output of any functions in this package in tests, because the details may change in future versions in order to improve the output for human readers.
Index ¶
- Variables
- func DiffValues(want, got cty.Value) string
- func TypeString(ty cty.Type) string
- func ValueString(v cty.Value) string
Examples ¶
Variables ¶
CmpOptions is a set of options for package github.com/google/go-cmp/cmp that help it to work well with cty.Type and cty.Value when they appear as part of a pair of data structures being compared.
Firstly, it converts collection and structural types into Go named types for either map[string]cty.Value or []cty.Value, so that type checking will still find these types to be distinct but cmp is able to understand how to recursively check inside them.
Secondly, it knows how to compare leaf cty.Type and cty.Value values using their built-in definitions of equality.
Functions ¶
func DiffValues ¶
DiffValues returns a human-oriented description of the differences between the two given values. It's guaranteed to return an empty string if the two values are RawEqual.
Don't depend on the exact formatting of the result. It is likely to change
in future releases.
Code:play
Output:Example¶
package main
import (
"fmt"
"github.com/zclconf/go-cty-debug/ctydebug"
"github.com/zclconf/go-cty/cty"
)
func main() {
a := cty.ObjectVal(map[string]cty.Value{
"foo": cty.StringVal("a"),
"bar": cty.StringVal("b"),
"baz": cty.MapVal(map[string]cty.Value{
"hello": cty.StringVal("world"),
}),
})
b := cty.ObjectVal(map[string]cty.Value{
"bar": cty.EmptyObjectVal,
"baz": cty.MapVal(map[string]cty.Value{
"hello": cty.UnknownVal(cty.String),
"goodbye": cty.StringVal("moon"),
}),
})
fmt.Print(ctydebug.DiffValues(b, a))
}
{cty.Value}["bar"]
got: cty.StringVal("b")
want: ctydebug.ctyObjectVal{}
{cty.Value}["baz"]["goodbye"]
got: (no value)
want: cty.StringVal("moon")
{cty.Value}["baz"]["hello"]
got: cty.StringVal("world")
want: cty.UnknownVal(cty.String)
{cty.Value}["foo"]
got: cty.StringVal("a")
want: (no value)
func TypeString ¶
TypeString returns a string representation of a given type that is reminiscent of Go syntax calling into the cty package but is mainly intended for easy human inspection of values in tests, debug output, etc.
The resulting string will include newlines and indentation in order to
increase the readability of complex structures. It always ends with a
newline, so you can print this result directly to your output.
Code:play
Output:Example¶
package main
import (
"fmt"
"github.com/zclconf/go-cty-debug/ctydebug"
"github.com/zclconf/go-cty/cty"
)
func main() {
ty := cty.Object(map[string]cty.Type{
"source_account": cty.String,
"fee": cty.Number,
"sequence_number": cty.Number,
"time_bounds": cty.Tuple([]cty.Type{cty.String, cty.String}),
"memo": cty.DynamicPseudoType,
"payments": cty.List(cty.Object(map[string]cty.Type{
"destination_account": cty.String,
"asset": cty.String,
"amount": cty.Number,
})),
})
fmt.Print(ctydebug.TypeString(ty))
}
cty.Object(map[string]cty.Type{
"fee": cty.Number,
"memo": cty.DynamicPseudoType,
"payments": cty.List(
cty.Object(map[string]cty.Type{
"amount": cty.Number,
"asset": cty.String,
"destination_account": cty.String,
}),
),
"sequence_number": cty.Number,
"source_account": cty.String,
"time_bounds": cty.Tuple([]cty.Type{
cty.String,
cty.String,
}),
})
func ValueString ¶
ValueString returns a string representation of a given value that is reminiscent of Go syntax calling into the cty package but is mainly intended for easy human inspection of values in tests, debug output, etc.
The resulting string will include newlines and indentation in order to
increase the readability of complex structures. It always ends with a
newline, so you can print this result directly to your output.
Code:play
Output:Example¶
package main
import (
"fmt"
"github.com/zclconf/go-cty-debug/ctydebug"
"github.com/zclconf/go-cty/cty"
)
func main() {
v := cty.ObjectVal(map[string]cty.Value{
"source_account": cty.StringVal("GBAMSPIE6NRUVRV4ZIPI2ZFR3NAIAIXQHGCMVLPSVQCM46IPWTHEVOID"),
"fee": cty.NumberIntVal(2),
"sequence_number": cty.NumberIntVal(4523452343),
"time_bounds": cty.TupleVal([]cty.Value{
cty.StringVal("2019-12-14T00:00:00Z"),
cty.StringVal("2019-12-14T00:05:00Z"),
}),
"memo": cty.MapValEmpty(cty.String),
"payments": cty.ListVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"destination_account": cty.StringVal("GALAXYVOIDAOPZTDLHILAJQKCVVFMD4IKLXLSZV5YHO7VY74IWZILUTO"),
"asset": cty.StringVal("XLM"),
"amount": cty.NumberIntVal(55442098181),
}),
}),
})
fmt.Print(ctydebug.ValueString(v))
}
cty.ObjectVal(map[string]cty.Value{
"fee": cty.NumberIntVal(2),
"memo": cty.MapValEmpty(cty.String),
"payments": cty.ListVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"amount": cty.NumberIntVal(5.5442098181e+10),
"asset": cty.StringVal("XLM"),
"destination_account": cty.StringVal("GALAXYVOIDAOPZTDLHILAJQKCVVFMD4IKLXLSZV5YHO7VY74IWZILUTO"),
}),
}),
"sequence_number": cty.NumberIntVal(4.523452343e+09),
"source_account": cty.StringVal("GBAMSPIE6NRUVRV4ZIPI2ZFR3NAIAIXQHGCMVLPSVQCM46IPWTHEVOID"),
"time_bounds": cty.TupleVal([]cty.Value{
cty.StringVal("2019-12-14T00:00:00Z"),
cty.StringVal("2019-12-14T00:05:00Z"),
}),
})
Source Files ¶
cmp.go ctydebug.go diff.go type_string.go value_string.go
- Version
- v0.0.0-20240509010212-0d6042c53940 (latest)
- Published
- May 9, 2024
- Platform
- linux/amd64
- Imports
- 6 packages
- Last checked
- now –
Tools for package owners.