/
mapwithsemanticequals.go
116 lines (87 loc) · 3.01 KB
/
mapwithsemanticequals.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package types
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)
var (
_ basetypes.MapTypable = MapTypeWithSemanticEquals{}
_ basetypes.MapValuableWithSemanticEquals = MapValueWithSemanticEquals{}
)
// MapTypeWithSemanticEquals is a MapType associated with
// MapValueWithSemanticEquals, which implements semantic equality logic that
// returns the SemanticEquals boolean for testing.
type MapTypeWithSemanticEquals struct {
basetypes.MapType
SemanticEquals bool
SemanticEqualsDiagnostics diag.Diagnostics
}
func (t MapTypeWithSemanticEquals) Equal(o attr.Type) bool {
other, ok := o.(MapTypeWithSemanticEquals)
if !ok {
return false
}
if t.SemanticEquals != other.SemanticEquals {
return false
}
return t.MapType.Equal(other.MapType)
}
func (t MapTypeWithSemanticEquals) String() string {
return fmt.Sprintf("MapTypeWithSemanticEquals(%t)", t.SemanticEquals)
}
func (t MapTypeWithSemanticEquals) ValueFromMap(ctx context.Context, in basetypes.MapValue) (basetypes.MapValuable, diag.Diagnostics) {
var diags diag.Diagnostics
value := MapValueWithSemanticEquals{
MapValue: in,
SemanticEquals: t.SemanticEquals,
SemanticEqualsDiagnostics: t.SemanticEqualsDiagnostics,
}
return value, diags
}
func (t MapTypeWithSemanticEquals) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error) {
attrValue, err := t.MapType.ValueFromTerraform(ctx, in)
if err != nil {
return nil, err
}
stringValue, ok := attrValue.(basetypes.MapValue)
if !ok {
return nil, fmt.Errorf("unexpected value type of %T", attrValue)
}
stringValuable, diags := t.ValueFromMap(ctx, stringValue)
if diags.HasError() {
return nil, fmt.Errorf("unexpected error converting MapValue to MapValuable: %v", diags)
}
return stringValuable, nil
}
func (t MapTypeWithSemanticEquals) ValueType(ctx context.Context) attr.Value {
return MapValueWithSemanticEquals{
SemanticEquals: t.SemanticEquals,
SemanticEqualsDiagnostics: t.SemanticEqualsDiagnostics,
}
}
type MapValueWithSemanticEquals struct {
basetypes.MapValue
SemanticEquals bool
SemanticEqualsDiagnostics diag.Diagnostics
}
func (v MapValueWithSemanticEquals) Equal(o attr.Value) bool {
other, ok := o.(MapValueWithSemanticEquals)
if !ok {
return false
}
return v.MapValue.Equal(other.MapValue)
}
func (v MapValueWithSemanticEquals) MapSemanticEquals(ctx context.Context, otherV basetypes.MapValuable) (bool, diag.Diagnostics) {
return v.SemanticEquals, v.SemanticEqualsDiagnostics
}
func (v MapValueWithSemanticEquals) Type(ctx context.Context) attr.Type {
return MapTypeWithSemanticEquals{
SemanticEquals: v.SemanticEquals,
SemanticEqualsDiagnostics: v.SemanticEqualsDiagnostics,
}
}