-
Notifications
You must be signed in to change notification settings - Fork 91
/
map_parameter.go
148 lines (126 loc) · 5.4 KB
/
map_parameter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package function
import (
"context"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/fwfunction"
"github.com/hashicorp/terraform-plugin-framework/internal/fwtype"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
)
// Ensure the implementation satisifies the desired interfaces.
var (
_ Parameter = MapParameter{}
_ fwfunction.ParameterWithValidateImplementation = MapParameter{}
_ ParameterWithMapValidators = MapParameter{}
)
// MapParameter represents a function parameter that is a mapping of a single
// element type. Either the ElementType or CustomType field must be set.
//
// When retrieving the argument value for this parameter:
//
// - If CustomType is set, use its associated value type.
// - If AllowUnknownValues is enabled, you must use the [types.Map] value
// type.
// - Otherwise, use [types.Map] or any Go map value types compatible with
// the element type.
//
// Terraform configurations set this parameter's argument data using expressions
// that return a map or directly via map ("{...}") syntax.
type MapParameter struct {
// ElementType is the type for all elements of the map. This field must be
// set.
//
// Element types that contain a dynamic type (i.e. types.Dynamic) are not supported.
// If underlying dynamic values are required, replace this parameter definition with
// DynamicParameter instead.
ElementType attr.Type
// AllowNullValue when enabled denotes that a null argument value can be
// passed to the function. When disabled, Terraform returns an error if the
// argument value is null.
AllowNullValue bool
// AllowUnknownValues when enabled denotes that an unknown argument value
// can be passed to the function. When disabled, Terraform skips the
// function call entirely and assumes an unknown value result from the
// function.
AllowUnknownValues bool
// CustomType enables the use of a custom data type in place of the
// default [basetypes.MapType]. When retrieving data, the
// [basetypes.MapValuable] implementation associated with this custom
// type must be used in place of [types.Map].
CustomType basetypes.MapTypable
// Description is used in various tooling, like the language server, to
// give practitioners more information about what this parameter is,
// what it is for, and how it should be used. It should be written as
// plain text, with no special formatting.
Description string
// MarkdownDescription is used in various tooling, like the
// documentation generator, to give practitioners more information
// about what this parameter is, what it is for, and how it should be
// used. It should be formatted using Markdown.
MarkdownDescription string
// Name is a short usage name for the parameter, such as "data". This name
// is used in documentation, such as generating a function signature,
// however its usage may be extended in the future.
//
// If no name is provided, this will default to "param" with a suffix of the
// position the parameter is in the function definition. ("param1", "param2", etc.)
// If the parameter is variadic, the default name will be "varparam".
//
// This must be a valid Terraform identifier, such as starting with an
// alphabetical character and followed by alphanumeric or underscore
// characters.
Name string
// Validators is a list of map validators that should be applied to the
// parameter.
Validators []MapParameterValidator
}
// GetValidators returns the list of validators for the parameter.
func (p MapParameter) GetValidators() []MapParameterValidator {
return p.Validators
}
// GetAllowNullValue returns if the parameter accepts a null value.
func (p MapParameter) GetAllowNullValue() bool {
return p.AllowNullValue
}
// GetAllowUnknownValues returns if the parameter accepts an unknown value.
func (p MapParameter) GetAllowUnknownValues() bool {
return p.AllowUnknownValues
}
// GetDescription returns the parameter plaintext description.
func (p MapParameter) GetDescription() string {
return p.Description
}
// GetMarkdownDescription returns the parameter Markdown description.
func (p MapParameter) GetMarkdownDescription() string {
return p.MarkdownDescription
}
// GetName returns the parameter name.
func (p MapParameter) GetName() string {
return p.Name
}
// GetType returns the parameter data type.
func (p MapParameter) GetType() attr.Type {
if p.CustomType != nil {
return p.CustomType
}
return basetypes.MapType{
ElemType: p.ElementType,
}
}
// ValidateImplementation contains logic for validating the
// provider-defined implementation of the parameter to prevent unexpected
// errors or panics. This logic runs during the GetProviderSchema RPC and
// should never include false positives.
func (p MapParameter) ValidateImplementation(ctx context.Context, req fwfunction.ValidateParameterImplementationRequest, resp *fwfunction.ValidateParameterImplementationResponse) {
if p.CustomType == nil && fwtype.ContainsCollectionWithDynamic(p.GetType()) {
var diag diag.Diagnostic
if req.ParameterPosition != nil {
diag = fwtype.ParameterCollectionWithDynamicTypeDiag(*req.ParameterPosition, req.Name)
} else {
diag = fwtype.VariadicParameterCollectionWithDynamicTypeDiag(req.Name)
}
resp.Diagnostics.Append(diag)
}
}