Movatterモバイル変換


[0]ホーム

URL:


function

package
v1.17.0Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 5, 2025 License:MITImports:3Imported by:477

Details

Repository

github.com/zclconf/go-cty

Links

Documentation

Overview

Package function builds on the functionality of cty by modeling functionsthat operate on cty Values.

Functions are, at their core, Go anonymous functions. However, this packagewraps around them utility functions for parameter type checking, etc.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

funcNewArgError

func NewArgError(iint, errerror)error

funcNewArgErrorf

func NewArgErrorf(iint, fstring, args ...any)error

Types

typeArgError

type ArgError struct {Indexint// contains filtered or unexported fields}

ArgError represents an error with one of the arguments in a call. Theattribute Index represents the zero-based index of the argument in question.

Its error *may* be a cty.PathError, in which case the error actuallypertains to a nested value within the data structure passed as the argument.

typeFunction

type Function struct {// contains filtered or unexported fields}

Function represents a function. This is the main type in this package.

funcNew

func New(spec *Spec)Function

New creates a new function with the given specification.

After passing a Spec to this function, the caller must no longer read fromor mutate it.

funcUnpredictable

func Unpredictable(fFunction)Function

Unpredictable wraps a given function such that it retains the same argumentsand type checking behavior but will return an unknown value when called.

It is recommended that most functions be "pure", which is to say that theywill always produce the same value given particular input. However,sometimes it is necessary to offer functions whose behavior depends onsome external state, such as reading a file or determining the current time.In such cases, an unpredictable wrapper might be used to stand in forthe function during some sort of prior "checking" phase in order to delaythe actual effect until later.

While Unpredictable can support a function that isn't pure in itsimplementation, it still expects a function to be pure in its type checkingbehavior, except for the special case of returning cty.DynamicPseudoTypeif it is not yet able to predict its return value based on current argumentinformation.

func (Function)Call

func (fFunction) Call(args []cty.Value) (valcty.Value, errerror)

Call actually calls the function with the given arguments, which mustconform to the function's parameter specification or an error will bereturned.

func (Function)Descriptionadded inv1.12.0

func (fFunction) Description()string

Description returns a human-readable description of the function.

func (Function)Params

func (fFunction) Params() []Parameter

Params returns information about the function's fixed positional parameters.This does not include information about any variadic arguments accepted;for that, call VarParam.

func (Function)Proxy

func (fFunction) Proxy()ProxyFunc

Proxy returns a function that can be called with cty.Value argumentsto run the function. This is provided as a convenience for when usinga function directly within Go code.

func (Function)ReturnType

func (fFunction) ReturnType(argTypes []cty.Type) (cty.Type,error)

ReturnType returns the return type of a function given a set of candidateargument types, or returns an error if the given types are unacceptable.

If the caller already knows values for at least some of the argumentsit can be better to call ReturnTypeForValues, since certain functions maydetermine their return types from their values and return DynamicVal ifthe values are unknown.

func (Function)ReturnTypeForValues

func (fFunction) ReturnTypeForValues(args []cty.Value) (tycty.Type, errerror)

ReturnTypeForValues is similar to ReturnType but can be used if the calleralready knows the values of some or all of the arguments, in which casethe function may be able to determine a more definite result if itsreturn type depends on the argument *values*.

For any arguments whose values are not known, pass an Unknown value ofthe appropriate type.

func (Function)VarParam

func (fFunction) VarParam() *Parameter

VarParam returns information about the variadic arguments the functionexpects, or nil if the function is not variadic.

func (Function)WithNewDescriptionsadded inv1.12.0

func (fFunction) WithNewDescriptions(funcDescstring, paramDescs []string)Function

WithNewDescriptions returns a new function that has the same signatureand implementation as the receiver but has the function description andthe parameter descriptions replaced with those given in the arguments.

All descriptions may be given as an empty string to specify that thereshould be no description at all.

The paramDescs argument must match the number of parametersthe reciever expects, or this function will panic. If the function has aVarParam then that counts as one parameter for the sake of this rule. Thegiven descriptions will be assigned in order starting with the positionalarguments in their declared order, followed by the variadic parameter ifany.

As a special case, WithNewDescriptions will accept a paramDescs whichdoes not cover the reciever's variadic parameter (if any), so that it'spossible to add a variadic parameter to a function which didn't previouslyhave one without that being a breaking change for an existing caller usingWithNewDescriptions against that function. In this case the base descriptionof the variadic parameter will be preserved.

typeImplFunc

type ImplFunc func(args []cty.Value, retTypecty.Type) (cty.Value,error)

ImplFunc is a callback type for the main implementation of a function.

"args" are the values for the arguments, and this slice will always be atleast as long as the argument definition slice for the function.

"retType" is the type returned from the Type callback, included as aconvenience to avoid the need to re-compute the return type for genericfunctions whose return type is a function of the arguments.

typePanicError

type PanicError struct {ValueanyStack []byte}

PanicError indicates that a panic occurred while executing either afunction's type or implementation function. This is captured and wrappedinto a normal error so that callers (expected to be language runtimes)are freed from having to deal with panics in buggy functions.

func (PanicError)Error

func (ePanicError) Error()string

typeParameter

type Parameter struct {// Name is an optional name for the argument. This package ignores this// value, but callers may use it for documentation, etc.Namestring// Description is an optional description for the argument.Descriptionstring// A type that any argument for this parameter must conform to.// cty.DynamicPseudoType can be used, either at top-level or nested// in a parameterized type, to indicate that any type should be// permitted, to allow the definition of type-generic functions.Typecty.Type// If AllowNull is set then null values may be passed into this// argument's slot in both the type-check function and the implementation// function. If not set, such values are rejected by the built-in// checking rules.AllowNullbool// If AllowUnknown is set then unknown values may be passed into this// argument's slot in the implementation function. If not set, any// unknown values will cause the function to immediately return// an unkonwn value without calling the implementation function, thus// freeing the function implementer from dealing with this case.AllowUnknownbool// If AllowDynamicType is set then DynamicVal may be passed into this// argument's slot in the implementation function. If not set, any// dynamic values will cause the function to immediately return// DynamicVal value without calling the implementation function, thus// freeing the function implementer from dealing with this case.//// Note that DynamicVal is also unknown, so in order to receive dynamic// *values* it is also necessary to set AllowUnknown.//// However, it is valid to set AllowDynamicType without AllowUnknown, in// which case a dynamic value may be passed to the type checking function// but will not make it to the *implementation* function. Instead, an// unknown value of the type returned by the type-check function will be// returned. This is suggested for functions that have a static return// type since it allows the return value to be typed even if the input// values are not, thus improving the type-check accuracy of derived// values.AllowDynamicTypebool// If AllowMarked is set then marked values may be passed into this// argument's slot in the implementation function. If not set, any// marked value will be unmarked before calling and then the markings// from that value will be applied automatically to the function result,// ensuring that the marks get propagated in a simplistic way even if// a function is unable to handle them.//// For any argument whose parameter has AllowMarked set, it's the// function implementation's responsibility to Unmark the given value// and propagate the marks appropriatedly to the result in order to// avoid losing the marks. Application-specific functions might use// special rules to selectively propagate particular marks.//// The automatic unmarking of values applies only to the main// implementation function. In an application that uses marked values,// the Type implementation for a function must always be prepared to accept// marked values, which is easy to achieve by consulting only the type// and ignoring the value itself.AllowMarkedbool}

Parameter represents a parameter to a function.

typeProxyFunc

type ProxyFunc func(args ...cty.Value) (cty.Value,error)

ProxyFunc the type returned by the method Function.Proxy.

typeSpec

type Spec struct {// Description is an optional description for the function specification.Descriptionstring// Params is a description of the positional parameters for the function.// The standard checking logic rejects any calls that do not provide// arguments conforming to this definition, freeing the function// implementer from dealing with such inconsistencies.Params []Parameter// VarParam is an optional specification of additional "varargs" the// function accepts. If this is non-nil then callers may provide an// arbitrary number of additional arguments (after those matching with// the fixed parameters in Params) that conform to the given specification,// which will appear as additional values in the slices of values// provided to the type and implementation functions.VarParam *Parameter// Type is the TypeFunc that decides the return type of the function// given its arguments, which may be Unknown. See the documentation// of TypeFunc for more information.//// Use StaticReturnType if the function's return type does not vary// depending on its arguments.TypeTypeFunc// RefineResult is an optional callback for describing additional// refinements for the result value beyond what can be described using// a type constraint.//// A refinement callback should always return the same builder it was// given, typically after modifying it using the methods of// [cty.RefinementBuilder].//// Any refinements described by this callback must hold for the entire// range of results from the function. For refinements that only apply// to certain results, use direct refinement within [Impl] instead.RefineResult func(*cty.RefinementBuilder) *cty.RefinementBuilder// Impl is the ImplFunc that implements the function's behavior.//// Functions are expected to behave as pure functions, and not create// any visible side-effects.//// If a TypeFunc is also provided, the value returned from Impl *must*// conform to the type it returns, or a call to the function will panic.ImplImplFunc}

Spec is the specification of a function, used to instantiatea new Function.

typeTypeFunc

type TypeFunc func(args []cty.Value) (cty.Type,error)

TypeFunc is a callback type for determining the return type of a functiongiven its arguments.

Any of the values passed to this function may be unknown, even if theparameters are not configured to accept unknowns.

If any of the given values are *not* unknown, the TypeFunc may use thevalues for pre-validation and for choosing the return type. For example,a hypothetical JSON-unmarshalling function could returncty.DynamicPseudoType if the given JSON string is unknown, but returna concrete type based on the JSON structure if the JSON string is alreadyknown.

funcStaticReturnType

func StaticReturnType(tycty.Type)TypeFunc

StaticReturnType returns a TypeFunc that always returns the given type.

This is provided as a convenience for defining a function whose returntype does not depend on the argument types.

Source Files

View all Source files

Directories

PathSynopsis
Package stdlib is a collection of cty functions that are expected to be generally useful, and are thus factored out into this shared library in the hope that cty-using applications will have consistent behavior when using these functions.
Package stdlib is a collection of cty functions that are expected to be generally useful, and are thus factored out into this shared library in the hope that cty-using applications will have consistent behavior when using these functions.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp