Skip to content

Commit 26ee41c

Browse files
committed
Add typeutils package
1 parent e7d0ceb commit 26ee41c

File tree

4 files changed

+29
-28
lines changed

4 files changed

+29
-28
lines changed

internal/kibana/security_list/models.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/elastic/terraform-provider-elasticstack/generated/kbapi"
99
"github.com/elastic/terraform-provider-elasticstack/internal/clients"
1010
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
11+
"github.com/elastic/terraform-provider-elasticstack/internal/utils/typeutils"
1112
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
1213
"github.com/hashicorp/terraform-plugin-framework/diag"
1314
"github.com/hashicorp/terraform-plugin-framework/types"
@@ -120,10 +121,10 @@ func (m *SecurityListModel) fromAPI(ctx context.Context, apiList *kbapi.Security
120121
}
121122
m.ID = types.StringValue(compId.String())
122123

123-
m.ListID = utils.StringishValue(apiList.Id)
124-
m.Name = utils.StringishValue(apiList.Name)
125-
m.Description = utils.StringishValue(apiList.Description)
126-
m.Type = utils.StringishValue(apiList.Type)
124+
m.ListID = typeutils.StringishValue(apiList.Id)
125+
m.Name = typeutils.StringishValue(apiList.Name)
126+
m.Description = typeutils.StringishValue(apiList.Description)
127+
m.Type = typeutils.StringishValue(apiList.Type)
127128
m.Immutable = types.BoolValue(apiList.Immutable)
128129
m.Version = types.Int64Value(int64(apiList.Version))
129130
m.TieBreakerID = types.StringValue(apiList.TieBreakerId)
@@ -133,11 +134,11 @@ func (m *SecurityListModel) fromAPI(ctx context.Context, apiList *kbapi.Security
133134
m.UpdatedBy = types.StringValue(apiList.UpdatedBy)
134135

135136
// Set optional _version field
136-
m.VersionID = utils.StringishPointerValue(apiList.UnderscoreVersion)
137+
m.VersionID = typeutils.StringishPointerValue(apiList.UnderscoreVersion)
137138

138-
m.Deserializer = utils.StringishPointerValue(apiList.Deserializer)
139+
m.Deserializer = typeutils.StringishPointerValue(apiList.Deserializer)
139140

140-
m.Serializer = utils.StringishPointerValue(apiList.Serializer)
141+
m.Serializer = typeutils.StringishPointerValue(apiList.Serializer)
141142

142143
if apiList.Meta != nil {
143144
metaBytes, err := json.Marshal(apiList.Meta)

internal/kibana/security_list_item/models.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/elastic/terraform-provider-elasticstack/generated/kbapi"
88
"github.com/elastic/terraform-provider-elasticstack/internal/clients"
99
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
10+
"github.com/elastic/terraform-provider-elasticstack/internal/utils/typeutils"
1011
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
1112
"github.com/hashicorp/terraform-plugin-framework/diag"
1213
"github.com/hashicorp/terraform-plugin-framework/types"
@@ -98,20 +99,16 @@ func (m *SecurityListItemModel) fromAPIModel(ctx context.Context, apiItem *kbapi
9899
ResourceId: string(apiItem.Id),
99100
}
100101
m.ID = types.StringValue(compId.String())
101-
m.ListItemID = types.StringValue(string(apiItem.Id))
102-
m.ListID = types.StringValue(string(apiItem.ListId))
103-
m.Value = types.StringValue(string(apiItem.Value))
102+
m.ListItemID = typeutils.StringishValue(apiItem.Id)
103+
m.ListID = typeutils.StringishValue(apiItem.ListId)
104+
m.Value = typeutils.StringishValue(apiItem.Value)
104105
m.CreatedAt = types.StringValue(apiItem.CreatedAt.Format("2006-01-02T15:04:05.000Z"))
105106
m.CreatedBy = types.StringValue(apiItem.CreatedBy)
106107
m.UpdatedAt = types.StringValue(apiItem.UpdatedAt.Format("2006-01-02T15:04:05.000Z"))
107108
m.UpdatedBy = types.StringValue(apiItem.UpdatedBy)
108109

109110
// Set version if available
110-
if apiItem.UnderscoreVersion != nil {
111-
m.VersionID = types.StringValue(string(*apiItem.UnderscoreVersion))
112-
} else {
113-
m.VersionID = types.StringNull()
114-
}
111+
m.VersionID = typeutils.StringishPointerValue(apiItem.UnderscoreVersion)
115112

116113
// Set meta if available
117114
if apiItem.Meta != nil {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package typeutils
2+
3+
import "github.com/hashicorp/terraform-plugin-framework/types"
4+
5+
// StringishPointerValue converts a pointer to a string-like type to a Terraform types.String value.
6+
func StringishPointerValue[T ~string](ptr *T) types.String {
7+
if ptr == nil {
8+
return types.StringNull()
9+
}
10+
return types.StringValue(string(*ptr))
11+
}
12+
13+
// StringishValue converts a value of any string-like type T to a Terraform types.String.
14+
func StringishValue[T ~string](value T) types.String {
15+
return types.StringValue(string(value))
16+
}

internal/utils/utils.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -231,16 +231,3 @@ func NonNilSlice[T any](s []T) []T {
231231
func TimeToStringValue(t time.Time) types.String {
232232
return types.StringValue(FormatStrictDateTime(t))
233233
}
234-
235-
// StringishPointerValue converts a pointer to a string-like type to a Terraform types.String value.
236-
func StringishPointerValue[T ~string](ptr *T) types.String {
237-
if ptr == nil {
238-
return types.StringNull()
239-
}
240-
return types.StringValue(string(*ptr))
241-
}
242-
243-
// StringishValue converts a value of any string-like type T to a Terraform types.String.
244-
func StringishValue[T ~string](value T) types.String {
245-
return types.StringValue(string(value))
246-
}

0 commit comments

Comments
 (0)