Debian Codegraph @

cty/unknown_as_null.go

raw symbols SCIP
1 package ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/
2
3 // UnknownAsNull returns a value of the same type as the given value but
4 // with any unknown values (including nested values) replaced with null
5 // values of the same type.
6 //
7 // This can be useful if a result is to be serialized in a format that can't
8 // represent unknowns, such as JSON, as long as the caller does not need to
9 // retain the unknown value information.
10 func UnknownAsNullscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/UnknownAsNull().(val Valuescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#) Valuescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value# {
11 ty := val.Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#Type().()
12 switch {
13 case val.IsNullscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#IsNull().():
14 return val
15 case !val.IsKnownscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#IsKnown().():
16 return NullValscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/NullVal().(ty)
17 case ty.IsListTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsListType().() || ty.IsTupleTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsTupleType().() || ty.IsSetTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsSetType().():
18 length := val.LengthIntscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#LengthInt().()
19 if length == 0 {
20 // If there are no elements then we can't have unknowns
21 return val
22 }
23 vals := make([]Valuescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#, 0, length)
24 it := val.ElementIteratorscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#ElementIterator().()
25 for it.Nextscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/ElementIterator#Next.() {
26 _, v := it.Elementscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/ElementIterator#Element.()
27 vals = append(vals, UnknownAsNullscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/UnknownAsNull().(v))
28 }
29 switch {
30 case ty.IsListTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsListType().():
31 return ListValscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/ListVal().(vals)
32 case ty.IsTupleTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsTupleType().():
33 return TupleValscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/TupleVal().(vals)
34 default:
35 return SetValscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/SetVal().(vals)
36 }
37 case ty.IsMapTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsMapType().() || ty.IsObjectTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsObjectType().():
38 var length int
39 switch {
40 case ty.IsMapTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsMapType().():
41 length = val.LengthIntscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#LengthInt().()
42 default:
43 length = len(val.Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#Type().().AttributeTypesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#AttributeTypes().())
44 }
45 if length == 0 {
46 // If there are no elements then we can't have unknowns
47 return val
48 }
49 vals := make(map[string]Valuescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#, length)
50 it := val.ElementIteratorscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#ElementIterator().()
51 for it.Nextscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/ElementIterator#Next.() {
52 k, v := it.Elementscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/ElementIterator#Element.()
53 vals[k.AsStringscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#AsString().()] = UnknownAsNullscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/UnknownAsNull().(v)
54 }
55 switch {
56 case ty.IsMapTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsMapType().():
57 return MapValscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/MapVal().(vals)
58 default:
59 return ObjectValscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/ObjectVal().(vals)
60 }
61 }
62
63 return val
64 }