| 1 | package ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/ |
| 2 | |
| 3 | import ( |
| 4 | "fmtscip-go gomod github.com/golang/go/src go1.18 fmt/" |
| 5 | "reflectscip-go gomod github.com/golang/go/src go1.18 reflect/" |
| 6 | ) |
| 7 | |
| 8 | type capsuleTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/capsuleType# struct { |
| 9 | typeImplSigilscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/capsuleType#typeImplSigil. |
| 10 | Namescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/capsuleType#Name. string |
| 11 | GoTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/capsuleType#GoType. reflectscip-go gomod github.com/golang/go/src go1.18 reflect/.Typescip-go gomod github.com/golang/go/src go1.18 reflect/Type# |
| 12 | Opsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/capsuleType#Ops. *CapsuleOpsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/CapsuleOps# |
| 13 | } |
| 14 | |
| 15 | func (t *capsuleTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/capsuleType#) Equalsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/capsuleType#Equals().(other Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#) bool { |
| 16 | if otherP, ok := other.typeImplscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#typeImpl..(*capsuleTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/capsuleType#); ok { |
| 17 | // capsule types compare by pointer identity |
| 18 | return otherP == t |
| 19 | } |
| 20 | return false |
| 21 | } |
| 22 | |
| 23 | func (t *capsuleTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/capsuleType#) FriendlyNamescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/capsuleType#FriendlyName().(mode friendlyTypeNameModescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/friendlyTypeNameMode#) string { |
| 24 | return t.Namescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/capsuleType#Name. |
| 25 | } |
| 26 | |
| 27 | func (t *capsuleTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/capsuleType#) GoStringscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/capsuleType#GoString().() string { |
| 28 | impl := t.Opsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/capsuleType#Ops..TypeGoStringscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/CapsuleOps#TypeGoString. |
| 29 | if impl == nil { |
| 30 | // To get a useful representation of our native type requires some |
| 31 | // shenanigans. |
| 32 | victimVal := reflectscip-go gomod github.com/golang/go/src go1.18 reflect/.Zeroscip-go gomod github.com/golang/go/src go1.18 reflect/Zero().(t.GoTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/capsuleType#GoType.) |
| 33 | if t.Opsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/capsuleType#Ops. == noCapsuleOpsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/noCapsuleOps. { |
| 34 | return fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.18 fmt/Sprintf().("cty.Capsule(%q, reflect.TypeOf(%#v))", t.Namescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/capsuleType#Name., victimVal.Interfacescip-go gomod github.com/golang/go/src go1.18 reflect/Value#Interface().()) |
| 35 | } else { |
| 36 | // Including the operations in the output will make this _very_ long, |
| 37 | // so in practice any capsule type with ops ought to provide a |
| 38 | // TypeGoString function to override this with something more |
| 39 | // reasonable. |
| 40 | return fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.18 fmt/Sprintf().("cty.CapsuleWithOps(%q, reflect.TypeOf(%#v), %#v)", t.Namescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/capsuleType#Name., victimVal.Interfacescip-go gomod github.com/golang/go/src go1.18 reflect/Value#Interface().(), t.Opsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/capsuleType#Ops.) |
| 41 | } |
| 42 | } |
| 43 | return impl(t.GoTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/capsuleType#GoType.) |
| 44 | } |
| 45 | |
| 46 | // Capsule creates a new Capsule type. |
| 47 | // |
| 48 | // A Capsule type is a special type that can be used to transport arbitrary |
| 49 | // Go native values of a given type through the cty type system. A language |
| 50 | // that uses cty as its type system might, for example, provide functions |
| 51 | // that return capsule-typed values and then other functions that operate |
| 52 | // on those values. |
| 53 | // |
| 54 | // From cty's perspective, Capsule types have a few interesting characteristics, |
| 55 | // described in the following paragraphs. |
| 56 | // |
| 57 | // Each capsule type has an associated Go native type that it is able to |
| 58 | // transport. Capsule types compare by identity, so each call to the |
| 59 | // Capsule function creates an entirely-distinct cty Type, even if two calls |
| 60 | // use the same native type. |
| 61 | // |
| 62 | // Each capsule-typed value contains a pointer to a value of the given native |
| 63 | // type. A capsule-typed value by default supports no operations except |
| 64 | // equality, and equality is implemented by pointer identity of the |
| 65 | // encapsulated pointer. A capsule type can optionally have its own |
| 66 | // implementations of certain operations if it is created with CapsuleWithOps |
| 67 | // instead of Capsule. |
| 68 | // |
| 69 | // The given name is used as the new type's "friendly name". This can be any |
| 70 | // string in principle, but will usually be a short, all-lowercase name aimed |
| 71 | // at users of the embedding language (i.e. not mention Go-specific details) |
| 72 | // and will ideally not create ambiguity with any predefined cty type. |
| 73 | // |
| 74 | // Capsule types are never introduced by any standard cty operation, so a |
| 75 | // calling application opts in to including them within its own type system |
| 76 | // by creating them and introducing them via its own functions. At that point, |
| 77 | // the application is responsible for dealing with any capsule-typed values |
| 78 | // that might be returned. |
| 79 | func Capsulescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Capsule().(name string, nativeType reflectscip-go gomod github.com/golang/go/src go1.18 reflect/.Typescip-go gomod github.com/golang/go/src go1.18 reflect/Type#) Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type# { |
| 80 | return Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#{ |
| 81 | &capsuleTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/capsuleType#{ |
| 82 | Namescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/capsuleType#Name.: name, |
| 83 | GoTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/capsuleType#GoType.: nativeType, |
| 84 | Opsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/capsuleType#Ops.: noCapsuleOpsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/noCapsuleOps., |
| 85 | }, |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // CapsuleWithOps is like Capsule except the caller may provide an object |
| 90 | // representing some overloaded operation implementations to associate with |
| 91 | // the given capsule type. |
| 92 | // |
| 93 | // All of the other caveats and restrictions for capsule types still apply, but |
| 94 | // overloaded operations can potentially help a capsule type participate better |
| 95 | // in cty operations. |
| 96 | func CapsuleWithOpsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/CapsuleWithOps().(name string, nativeType reflectscip-go gomod github.com/golang/go/src go1.18 reflect/.Typescip-go gomod github.com/golang/go/src go1.18 reflect/Type#, ops *CapsuleOpsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/CapsuleOps#) Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type# { |
| 97 | // Copy the operations to make sure the caller can't modify them after |
| 98 | // we're constructed. |
| 99 | ourOps := *ops |
| 100 | ourOps.assertValidscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/CapsuleOps#assertValid().() |
| 101 | |
| 102 | return Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#{ |
| 103 | &capsuleTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/capsuleType#{ |
| 104 | Namescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/capsuleType#Name.: name, |
| 105 | GoTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/capsuleType#GoType.: nativeType, |
| 106 | Opsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/capsuleType#Ops.: &ourOps, |
| 107 | }, |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // IsCapsuleType returns true if this type is a capsule type, as created |
| 112 | // by cty.Capsule . |
| 113 | func (t Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#) IsCapsuleTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsCapsuleType().() bool { |
| 114 | _, ok := t.typeImplscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#typeImpl..(*capsuleTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/capsuleType#) |
| 115 | return ok |
| 116 | } |
| 117 | |
| 118 | // EncapsulatedType returns the encapsulated native type of a capsule type, |
| 119 | // or panics if the receiver is not a Capsule type. |
| 120 | // |
| 121 | // Is IsCapsuleType to determine if this method is safe to call. |
| 122 | func (t Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#) EncapsulatedTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#EncapsulatedType().() reflectscip-go gomod github.com/golang/go/src go1.18 reflect/.Typescip-go gomod github.com/golang/go/src go1.18 reflect/Type# { |
| 123 | impl, ok := t.typeImplscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#typeImpl..(*capsuleTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/capsuleType#) |
| 124 | if !ok { |
| 125 | panic("not a capsule type") |
| 126 | } |
| 127 | return impl.GoTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/capsuleType#GoType. |
| 128 | } |