dgocty

package module
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2020 License: Apache-2.0 Imports: 4 Imported by: 1

README

dgocty (Transform Dgo to Cty and back)

This module provides conversion routines to convert Dgo values into Cty values and vice versa.

Among other things, the cty values are used by Terraform to represent configurations and states.

Using dgocty as a library

To use dgo, first install the latest version of the library:

go get github.com/lyraproj/dgocty

Documentation

Overview

Package dgocty contains the two function necessary to convert a dgo.Value into a cty.Value and vice versa.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromCty

func FromCty(cv cty.Value) dgo.Value

FromCty converts a cty.Value into its corresponding dgo.Value

Example (Mixed)
package main

import (
	"fmt"

	"github.com/lyraproj/dgocty"
	"github.com/zclconf/go-cty/cty"
	"github.com/zclconf/go-cty/cty/gocty"
)

func main() {
	v, err := gocty.ToCtyValue(map[string]interface{}{"a": 1, "b": []interface{}{"c", 3.14, true}},
		cty.Object(map[string]cty.Type{"a": cty.Number, "b": cty.Tuple([]cty.Type{cty.String, cty.Number, cty.Bool})}))
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(dgocty.FromCty(v))
	}
}
Output:
{"a":1.0,"b":{"c",3.14,true}}

func ToCty

func ToCty(v dgo.Value, attemptExplicit bool) cty.Value

ToCty converts a dgo.Value into its corresponding cty.Value. dgo.Types that are not recognized will be converted to cty.Capsule values. dgo.Sensitive values will be unwrapped.

If attemptExplicit is true, then a cty.ListVal will be used instead of a cty.TupleVal and a cty.MapVal will be used instead of a cty.ObjectVal in cases when all values are of the same cty.Type.

Example (AutoObject)
package main

import (
	"fmt"

	"github.com/lyraproj/dgo/vf"
	"github.com/lyraproj/dgocty"
)

func main() {
	v := vf.Map("a", 1, "b", `two`)
	c := dgocty.ToCty(v, true)
	fmt.Println(c.GoString())
}
Output:
cty.ObjectVal(map[string]cty.Value{"a":cty.NumberIntVal(1), "b":cty.StringVal("two")})
Example (AutoTuple)
package main

import (
	"fmt"

	"github.com/lyraproj/dgo/vf"
	"github.com/lyraproj/dgocty"
)

func main() {
	v := vf.Values("a", 2)
	c := dgocty.ToCty(v, true)
	fmt.Println(c.GoString())
}
Output:
cty.TupleVal([]cty.Value{cty.StringVal("a"), cty.NumberIntVal(2)})
Example (ForcedObject)
package main

import (
	"fmt"

	"github.com/lyraproj/dgo/vf"
	"github.com/lyraproj/dgocty"
)

func main() {
	v := vf.Map("a", 1, "b", 2)
	c := dgocty.ToCty(v, false)
	fmt.Println(c.GoString())
}
Output:
cty.ObjectVal(map[string]cty.Value{"a":cty.NumberIntVal(1), "b":cty.NumberIntVal(2)})
Example (ForcedTuple)
package main

import (
	"fmt"

	"github.com/lyraproj/dgo/vf"
	"github.com/lyraproj/dgocty"
)

func main() {
	v := vf.Strings("a", "b")
	c := dgocty.ToCty(v, false)
	fmt.Println(c.GoString())
}
Output:
cty.TupleVal([]cty.Value{cty.StringVal("a"), cty.StringVal("b")})
Example (List)
package main

import (
	"fmt"

	"github.com/lyraproj/dgo/vf"
	"github.com/lyraproj/dgocty"
)

func main() {
	v := vf.Strings("a", "b")
	c := dgocty.ToCty(v, true)
	fmt.Println(c.GoString())
}
Output:
cty.ListVal([]cty.Value{cty.StringVal("a"), cty.StringVal("b")})
Example (Map)
package main

import (
	"fmt"

	"github.com/lyraproj/dgo/vf"
	"github.com/lyraproj/dgocty"
)

func main() {
	v := vf.Map("a", 1, "b", 2)
	c := dgocty.ToCty(v, true)
	fmt.Println(c.GoString())
}
Output:
cty.MapVal(map[string]cty.Value{"a":cty.NumberIntVal(1), "b":cty.NumberIntVal(2)})
Example (Mixed)
package main

import (
	"fmt"

	"github.com/lyraproj/dgo/vf"
	"github.com/lyraproj/dgocty"
)

func main() {
	v := vf.Map("a", 1, "b", vf.Values("c", 3.14, true))
	c := dgocty.ToCty(v, false)
	fmt.Println(c.GoString())
}
Output:
cty.ObjectVal(map[string]cty.Value{"a":cty.NumberIntVal(1), "b":cty.TupleVal([]cty.Value{cty.StringVal("c"), cty.NumberFloatVal(3.14), cty.True})})
Example (Nil)
package main

import (
	"fmt"

	"github.com/lyraproj/dgo/vf"
	"github.com/lyraproj/dgocty"
)

func main() {
	fmt.Println(dgocty.ToCty(vf.Nil, false).GoString())
}
Output:
cty.NullVal(cty.DynamicPseudoType)

Types

This section is empty.

Jump to

Keyboard shortcuts

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