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.
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 UnknownAsNull(val Value) Value {
17 case ty.IsListType() || ty.IsTupleType() || ty.IsSetType():
18 length := val.LengthInt()
20 // If there are no elements then we can't have unknowns
23 vals := make([]Value, 0, length)
24 it := val.ElementIterator()
27 vals = append(vals, UnknownAsNull(v))
32 case ty.IsTupleType():
37 case ty.IsMapType() || ty.IsObjectType():
41 length = val.LengthInt()
43 length = len(val.Type().AttributeTypes())
46 // If there are no elements then we can't have unknowns
49 vals := make(map[string]Value, length)
50 it := val.ElementIterator()
53 vals[k.AsString()] = UnknownAsNull(v)
59 return ObjectVal(vals)