ctydebug

package
v0.0.0-...-8c45e12 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 17, 2024 License: MIT Imports: 6 Imported by: 6

Documentation

Overview

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

Examples

Constants

This section is empty.

Variables

View Source
var CmpOptions cmp.Option

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

func DiffValues(want, got cty.Value) string

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.

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))

}
Output:

{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

func TypeString(ty cty.Type) string

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.

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))

}
Output:

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

func ValueString(v cty.Value) string

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.

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))

}
Output:

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"),
    }),
})

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL