Skip to content

Commit 50ce0a5

Browse files
committed
add number type
1 parent e64d1a7 commit 50ce0a5

File tree

3 files changed

+665
-0
lines changed

3 files changed

+665
-0
lines changed

list/schema/attribute.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
// - Int64Attribute
1717
// - ListAttribute
1818
// - MapAttribute
19+
// - NumberAttribute
1920
// - StringAttribute
2021
//
2122
// In practitioner configurations, an equals sign (=) is required to set

list/schema/number_attribute.go

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package schema
5+
6+
import (
7+
"github.com/hashicorp/terraform-plugin-go/tftypes"
8+
9+
"github.com/hashicorp/terraform-plugin-framework/attr"
10+
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
11+
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema/fwxschema"
12+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
13+
"github.com/hashicorp/terraform-plugin-framework/types"
14+
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
15+
)
16+
17+
// Ensure the implementation satisifies the desired interfaces.
18+
var (
19+
_ Attribute = NumberAttribute{}
20+
_ fwxschema.AttributeWithNumberValidators = NumberAttribute{}
21+
)
22+
23+
// NumberAttribute represents a schema attribute that is a generic number with
24+
// up to 512 bits of floating point or integer precision. When retrieving the
25+
// value for this attribute, use types.Number as the value type unless the
26+
// CustomType field is set.
27+
//
28+
// Use Float64Attribute for 64-bit floating point number attributes or
29+
// Int64Attribute for 64-bit integer number attributes.
30+
//
31+
// Terraform configurations configure this attribute using expressions that
32+
// return a number or directly via a floating point or integer value.
33+
//
34+
// example_attribute = 123
35+
//
36+
// Terraform configurations reference this attribute using the attribute name.
37+
//
38+
// .example_attribute
39+
type NumberAttribute struct {
40+
// CustomType enables the use of a custom attribute type in place of the
41+
// default basetypes.NumberType. When retrieving data, the basetypes.NumberValuable
42+
// associated with this custom type must be used in place of types.Number.
43+
CustomType basetypes.NumberTypable
44+
45+
// Required indicates whether the practitioner must enter a value for
46+
// this attribute or not. Required and Optional cannot both be true.
47+
Required bool
48+
49+
// Optional indicates whether the practitioner can choose to enter a value
50+
// for this attribute or not. Optional and Required cannot both be true.
51+
Optional bool
52+
53+
// Description is used in various tooling, like the language server, to
54+
// give practitioners more information about what this attribute is,
55+
// what it's for, and how it should be used. It should be written as
56+
// plain text, with no special formatting.
57+
Description string
58+
59+
// MarkdownDescription is used in various tooling, like the
60+
// documentation generator, to give practitioners more information
61+
// about what this attribute is, what it's for, and how it should be
62+
// used. It should be formatted using Markdown.
63+
MarkdownDescription string
64+
65+
// DeprecationMessage defines warning diagnostic details to display when
66+
// practitioner configurations use this Attribute. The warning diagnostic
67+
// summary is automatically set to "Attribute Deprecated" along with
68+
// configuration source file and line information.
69+
//
70+
// Set this field to a practitioner actionable message such as:
71+
//
72+
// - "Configure other_attribute instead. This attribute will be removed
73+
// in the next major version of the provider."
74+
// - "Remove this attribute's configuration as it no longer is used and
75+
// the attribute will be removed in the next major version of the
76+
// provider."
77+
//
78+
// In Terraform 1.2.7 and later, this warning diagnostic is displayed any
79+
// time a practitioner attempts to configure a value for this attribute and
80+
// certain scenarios where this attribute is referenced.
81+
//
82+
// In Terraform 1.2.6 and earlier, this warning diagnostic is only
83+
// displayed when the Attribute is Required or Optional, and if the
84+
// practitioner configuration sets the value to a known or unknown value
85+
// (which may eventually be null). It has no effect when the Attribute is
86+
// Computed-only (read-only; not Required or Optional).
87+
//
88+
// Across any Terraform version, there are no warnings raised for
89+
// practitioner configuration values set directly to null, as there is no
90+
// way for the framework to differentiate between an unset and null
91+
// configuration due to how Terraform sends configuration information
92+
// across the protocol.
93+
//
94+
// Additional information about deprecation enhancements for read-only
95+
// attributes can be found in:
96+
//
97+
// - https://github.com/hashicorp/terraform/issues/7569
98+
//
99+
DeprecationMessage string
100+
101+
// Validators define value validation functionality for the attribute. All
102+
// elements of the slice of AttributeValidator are run, regardless of any
103+
// previous error diagnostics.
104+
//
105+
// Many common use case validators can be found in the
106+
// github.com/hashicorp/terraform-plugin-framework-validators Go module.
107+
//
108+
// If the Type field points to a custom type that implements the
109+
// xattr.TypeWithValidate interface, the validators defined in this field
110+
// are run in addition to the validation defined by the type.
111+
Validators []validator.Number
112+
}
113+
114+
// ApplyTerraform5AttributePathStep always returns an error as it is not
115+
// possible to step further into a NumberAttribute.
116+
func (a NumberAttribute) ApplyTerraform5AttributePathStep(step tftypes.AttributePathStep) (interface{}, error) {
117+
return a.GetType().ApplyTerraform5AttributePathStep(step)
118+
}
119+
120+
// Equal returns true if the given Attribute is a NumberAttribute
121+
// and all fields are equal.
122+
func (a NumberAttribute) Equal(o fwschema.Attribute) bool {
123+
if _, ok := o.(NumberAttribute); !ok {
124+
return false
125+
}
126+
127+
return fwschema.AttributesEqual(a, o)
128+
}
129+
130+
// GetDeprecationMessage returns the DeprecationMessage field value.
131+
func (a NumberAttribute) GetDeprecationMessage() string {
132+
return a.DeprecationMessage
133+
}
134+
135+
// GetDescription returns the Description field value.
136+
func (a NumberAttribute) GetDescription() string {
137+
return a.Description
138+
}
139+
140+
// GetMarkdownDescription returns the MarkdownDescription field value.
141+
func (a NumberAttribute) GetMarkdownDescription() string {
142+
return a.MarkdownDescription
143+
}
144+
145+
// GetType returns types.NumberType or the CustomType field value if defined.
146+
func (a NumberAttribute) GetType() attr.Type {
147+
if a.CustomType != nil {
148+
return a.CustomType
149+
}
150+
151+
return types.NumberType
152+
}
153+
154+
// IsComputed returns false because it does not apply to ListResource schemas.
155+
func (a NumberAttribute) IsComputed() bool {
156+
return false
157+
}
158+
159+
// IsOptional returns the Optional field value.
160+
func (a NumberAttribute) IsOptional() bool {
161+
return a.Optional
162+
}
163+
164+
// IsRequired returns the Required field value.
165+
func (a NumberAttribute) IsRequired() bool {
166+
return a.Required
167+
}
168+
169+
// IsSensitive returns false because it does not apply to ListResource schemas.
170+
func (a NumberAttribute) IsSensitive() bool {
171+
return false
172+
}
173+
174+
// IsWriteOnly returns false because it does not apply to ListResource schemas.
175+
func (a NumberAttribute) IsWriteOnly() bool {
176+
return false
177+
}
178+
179+
// IsRequiredForImport returns false as this behavior is only relevant
180+
// for managed resource identity schema attributes.
181+
func (a NumberAttribute) IsRequiredForImport() bool {
182+
return false
183+
}
184+
185+
// IsOptionalForImport returns false as this behavior is only relevant
186+
// for managed resource identity schema attributes.
187+
func (a NumberAttribute) IsOptionalForImport() bool {
188+
return false
189+
}
190+
191+
// NumberValidators returns the Validators field value.
192+
func (a NumberAttribute) NumberValidators() []validator.Number {
193+
return a.Validators
194+
}

0 commit comments

Comments
 (0)