]>
Commit | Line | Data |
---|---|---|
15c0b25d AP |
1 | package function |
2 | ||
3 | import ( | |
4 | "github.com/zclconf/go-cty/cty" | |
5 | ) | |
6 | ||
7 | // Unpredictable wraps a given function such that it retains the same arguments | |
8 | // and type checking behavior but will return an unknown value when called. | |
9 | // | |
10 | // It is recommended that most functions be "pure", which is to say that they | |
11 | // will always produce the same value given particular input. However, | |
12 | // sometimes it is necessary to offer functions whose behavior depends on | |
13 | // some external state, such as reading a file or determining the current time. | |
14 | // In such cases, an unpredictable wrapper might be used to stand in for | |
15 | // the function during some sort of prior "checking" phase in order to delay | |
16 | // the actual effect until later. | |
17 | // | |
18 | // While Unpredictable can support a function that isn't pure in its | |
19 | // implementation, it still expects a function to be pure in its type checking | |
20 | // behavior, except for the special case of returning cty.DynamicPseudoType | |
21 | // if it is not yet able to predict its return value based on current argument | |
22 | // information. | |
23 | func Unpredictable(f Function) Function { | |
24 | newSpec := *f.spec // shallow copy | |
25 | newSpec.Impl = unpredictableImpl | |
26 | return New(&newSpec) | |
27 | } | |
28 | ||
29 | func unpredictableImpl(args []cty.Value, retType cty.Type) (cty.Value, error) { | |
30 | return cty.UnknownVal(retType), nil | |
31 | } |