Documentation
¶
Index ¶
- Constants
- Variables
- func BuildBytesObject(data []byte, contentType string) cty.Value
- func Externs() []byte
- func GetBytesFunctions() map[string]function.Function
- func MakeBase64DecodeFunc() function.Function
- func MakeBase64EncodeFunc() function.Function
- func MakeBytesFunc() function.Function
- func NewBytesCapsule(data []byte, contentType string) cty.Value
- type Bytes
Constants ¶
const ExternsFilename = "bytes-cty-type/externs.cty"
ExternsFilename is the name reported for the embedded declarations in diagnostics.
Variables ¶
var BytesCapsuleType = cty.CapsuleWithOps("bytes", reflect.TypeOf(Bytes{}), &cty.CapsuleOps{ GoString: func(val interface{}) string { b := val.(*Bytes) return fmt.Sprintf("bytes(%d bytes)", len(b.Data)) }, TypeGoString: func(_ reflect.Type) string { return "Bytes" }, })
BytesCapsuleType is the cty capsule type for Bytes values.
var BytesObjectType = cty.Object(map[string]cty.Type{ "content_type": cty.String, "_capsule": BytesCapsuleType, })
BytesObjectType is the cty object type returned by bytes-producing functions. It exposes content_type as a direct attribute and carries the underlying capsule in the _capsule attribute for interface dispatch (richcty.Stringable, richcty.Lengthable, etc.).
Functions ¶
func BuildBytesObject ¶
BuildBytesObject returns a cty object with content_type and _capsule attributes.
func Externs ¶ added in v0.2.0
func Externs() []byte
Externs returns the functy `//functy:extern` declarations for the functions GetBytesFunctions provides: their real signatures, which their cty metadata cannot express.
All three take an argument that is a union — a string, or a bytes value — and cty has no union type, so its metadata can only say "dynamic". Two of them also take an optional trailing content type, and cty can only make an argument optional by making it variadic, which erases its name, its type, and its arity; for base64decode, whose return type is a string or a bytes value depending on whether that argument is present, it erases the signature entirely. These declarations say what each really accepts, so that help(), generated documentation, and editor tooling can show it.
The bytes are opaque to this package: it does not import functy, and nothing here parses them. A functy host registers them:
parser.RegisterExterns(bytescty.Externs(), bytescty.ExternsFilename)
func GetBytesFunctions ¶
GetBytesFunctions returns bytes-related cty functions for registration in an eval context.
func MakeBase64DecodeFunc ¶
MakeBase64DecodeFunc returns a base64decode function.
When called with one argument it returns a string, preserving backward compatibility with the stdlib version. When a second (content_type) argument is present — even if it is the empty string — it returns a bytes object.
base64decode(str) - returns string (backward compatible) base64decode(str, "") - returns bytes object, no content type base64decode(str, "image/png") - returns bytes object with content type
func MakeBase64EncodeFunc ¶
MakeBase64EncodeFunc returns a base64encode function that accepts either a string or bytes value.
func MakeBytesFunc ¶
MakeBytesFunc returns a function that creates a bytes object from a UTF-8 string or re-wraps an existing bytes value with a different content type.
bytes(str) - bytes from UTF-8 string, no content type bytes(str, content_type) - bytes from UTF-8 string with content type bytes(b) - copy of bytes value (preserves content type) bytes(b, content_type) - copy of bytes value with overridden content type
Types ¶
type Bytes ¶
Bytes is an immutable byte slice with an optional content/MIME type.
func GetBytesFromCapsule ¶
GetBytesFromCapsule extracts a *Bytes from a cty capsule value.
func GetBytesFromValue ¶
GetBytesFromValue extracts a *Bytes from a bytes object, capsule, or anything accepted by GetCapsuleFromValue.