8 type capsuleType struct {
14 func (t *capsuleType) Equals(other Type) bool {
15 if otherP, ok := other.typeImpl.(*capsuleType); ok {
16 // capsule types compare by pointer identity
22 func (t *capsuleType) FriendlyName() string {
26 func (t *capsuleType) GoString() string {
27 // To get a useful representation of our native type requires some
29 victimVal := reflect.Zero(t.GoType)
30 return fmt.Sprintf("cty.Capsule(%q, reflect.TypeOf(%#v))", t.Name, victimVal.Interface())
33 // Capsule creates a new Capsule type.
35 // A Capsule type is a special type that can be used to transport arbitrary
36 // Go native values of a given type through the cty type system. A language
37 // that uses cty as its type system might, for example, provide functions
38 // that return capsule-typed values and then other functions that operate
41 // From cty's perspective, Capsule types have a few interesting characteristics,
42 // described in the following paragraphs.
44 // Each capsule type has an associated Go native type that it is able to
45 // transport. Capsule types compare by identity, so each call to the
46 // Capsule function creates an entirely-distinct cty Type, even if two calls
47 // use the same native type.
49 // Each capsule-typed value contains a pointer to a value of the given native
50 // type. A capsule-typed value supports no operations except equality, and
51 // equality is implemented by pointer identity of the encapsulated pointer.
53 // The given name is used as the new type's "friendly name". This can be any
54 // string in principle, but will usually be a short, all-lowercase name aimed
55 // at users of the embedding language (i.e. not mention Go-specific details)
56 // and will ideally not create ambiguity with any predefined cty type.
58 // Capsule types are never introduced by any standard cty operation, so a
59 // calling application opts in to including them within its own type system
60 // by creating them and introducing them via its own functions. At that point,
61 // the application is responsible for dealing with any capsule-typed values
62 // that might be returned.
63 func Capsule(name string, nativeType reflect.Type) Type {
72 // IsCapsuleType returns true if this type is a capsule type, as created
74 func (t Type) IsCapsuleType() bool {
75 _, ok := t.typeImpl.(*capsuleType)
79 // EncapsulatedType returns the encapsulated native type of a capsule type,
80 // or panics if the receiver is not a Capsule type.
82 // Is IsCapsuleType to determine if this method is safe to call.
83 func (t Type) EncapsulatedType() reflect.Type {
84 impl, ok := t.typeImpl.(*capsuleType)
86 panic("not a capsule type")