-
Notifications
You must be signed in to change notification settings - Fork 91
/
stringwithvalidate.go
68 lines (57 loc) · 1.77 KB
/
stringwithvalidate.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
package types
import (
"context"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/attr/xattr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)
var (
_ xattr.TypeWithValidate = StringTypeWithValidateError{}
_ xattr.TypeWithValidate = StringTypeWithValidateWarning{}
)
type StringTypeWithValidateError struct {
StringType
}
func (t StringTypeWithValidateError) Equal(o attr.Type) bool {
other, ok := o.(StringTypeWithValidateError)
if !ok {
return false
}
return t == other
}
func (s StringTypeWithValidateError) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error) {
res, err := s.StringType.ValueFromTerraform(ctx, in)
if err != nil {
return nil, err
}
newString := res.(String)
newString.CreatedBy = s
return newString, nil
}
type StringTypeWithValidateWarning struct {
StringType
}
func (t StringTypeWithValidateError) Validate(ctx context.Context, in tftypes.Value, path path.Path) diag.Diagnostics {
return diag.Diagnostics{TestErrorDiagnostic(path)}
}
func (t StringTypeWithValidateWarning) Equal(o attr.Type) bool {
other, ok := o.(StringTypeWithValidateWarning)
if !ok {
return false
}
return t == other
}
func (s StringTypeWithValidateWarning) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error) {
res, err := s.StringType.ValueFromTerraform(ctx, in)
if err != nil {
return nil, err
}
newString := res.(String)
newString.CreatedBy = s
return newString, nil
}
func (t StringTypeWithValidateWarning) Validate(ctx context.Context, in tftypes.Value, path path.Path) diag.Diagnostics {
return diag.Diagnostics{TestWarningDiagnostic(path)}
}