Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: migrate types to its own package (#2453)
* chore: migrate types to its own package * Fix
- Loading branch information
Showing
170 changed files
with
1,334 additions
and
1,141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package types | ||
|
||
import "github.com/scaleway/scaleway-sdk-go/scw" | ||
|
||
func FlattenBoolPtr(b *bool) interface{} { | ||
if b == nil { | ||
return nil | ||
} | ||
return *b | ||
} | ||
|
||
func ExpandBoolPtr(data interface{}) *bool { | ||
if data == nil { | ||
return nil | ||
} | ||
return scw.BoolPtr(data.(bool)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package types | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net" | ||
"strconv" | ||
|
||
"github.com/scaleway/scaleway-sdk-go/scw" | ||
) | ||
|
||
func ExpandIPNet(raw string) (scw.IPNet, error) { | ||
if raw == "" { | ||
return scw.IPNet{}, nil | ||
} | ||
var ipNet scw.IPNet | ||
err := json.Unmarshal([]byte(strconv.Quote(raw)), &ipNet) | ||
if err != nil { | ||
return scw.IPNet{}, fmt.Errorf("%s could not be marshaled: %v", raw, err) | ||
} | ||
|
||
return ipNet, nil | ||
} | ||
|
||
func FlattenIPPtr(ip *net.IP) interface{} { | ||
if ip == nil { | ||
return "" | ||
} | ||
return ip.String() | ||
} | ||
|
||
func FlattenIPNet(ipNet scw.IPNet) (string, error) { | ||
raw, err := json.Marshal(ipNet) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(raw[1 : len(raw)-1]), nil // remove quotes | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package types | ||
|
||
func FlattenMap(m map[string]string) interface{} { | ||
if m == nil { | ||
return nil | ||
} | ||
flattenedMap := make(map[string]interface{}) | ||
for k, v := range m { | ||
flattenedMap[k] = v | ||
} | ||
return flattenedMap | ||
} | ||
|
||
func FlattenMapStringStringPtr(m map[string]*string) interface{} { | ||
if m == nil { | ||
return nil | ||
} | ||
flattenedMap := make(map[string]interface{}) | ||
for k, v := range m { | ||
if v != nil { | ||
flattenedMap[k] = *v | ||
} else { | ||
flattenedMap[k] = "" | ||
} | ||
} | ||
return flattenedMap | ||
} | ||
|
||
func ExpandMapPtrStringString(data interface{}) *map[string]string { | ||
if data == nil { | ||
return nil | ||
} | ||
m := make(map[string]string) | ||
for k, v := range data.(map[string]interface{}) { | ||
m[k] = v.(string) | ||
} | ||
return &m | ||
} | ||
|
||
func ExpandMapStringStringPtr(data interface{}) map[string]*string { | ||
if data == nil { | ||
return nil | ||
} | ||
m := make(map[string]*string) | ||
for k, v := range data.(map[string]interface{}) { | ||
m[k] = ExpandStringPtr(v) | ||
} | ||
return m | ||
} | ||
|
||
func ExpandMapStringString(data any) map[string]string { | ||
if data == nil { | ||
return nil | ||
} | ||
m := make(map[string]string) | ||
for k, v := range data.(map[string]interface{}) { | ||
m[k] = v.(string) | ||
} | ||
return m | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package types | ||
|
||
import "github.com/scaleway/scaleway-sdk-go/scw" | ||
|
||
func FlattenInt32Ptr(i *int32) interface{} { | ||
if i == nil { | ||
return 0 | ||
} | ||
return *i | ||
} | ||
|
||
func FlattenUint32Ptr(i *uint32) interface{} { | ||
if i == nil { | ||
return 0 | ||
} | ||
return *i | ||
} | ||
|
||
func ExpandInt32Ptr(data interface{}) *int32 { | ||
if data == nil || data == "" { | ||
return nil | ||
} | ||
return scw.Int32Ptr(int32(data.(int))) | ||
} | ||
|
||
func ExpandUint32Ptr(data interface{}) *uint32 { | ||
if data == nil || data == "" { | ||
return nil | ||
} | ||
return scw.Uint32Ptr(uint32(data.(int))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package types | ||
|
||
import "github.com/scaleway/scaleway-sdk-go/scw" | ||
|
||
func FlattenSize(size *scw.Size) interface{} { | ||
if size == nil { | ||
return 0 | ||
} | ||
return *size | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
package types | ||
|
||
import ( | ||
"github.com/scaleway/scaleway-sdk-go/namegenerator" | ||
"github.com/scaleway/scaleway-sdk-go/scw" | ||
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality" | ||
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal" | ||
) | ||
|
||
func FlattenStringPtr(s *string) interface{} { | ||
if s == nil { | ||
return "" | ||
} | ||
return *s | ||
} | ||
|
||
func ExpandStringPtr(data interface{}) *string { | ||
if data == nil || data == "" { | ||
return nil | ||
} | ||
return scw.StringPtr(data.(string)) | ||
} | ||
|
||
// NewRandomName returns a random name prefixed for terraform. | ||
func NewRandomName(prefix string) string { | ||
return namegenerator.GetRandomName("tf", prefix) | ||
} | ||
|
||
func ExpandOrGenerateString(data interface{}, prefix string) string { | ||
if data == nil || data == "" { | ||
return NewRandomName(prefix) | ||
} | ||
return data.(string) | ||
} | ||
|
||
func ExpandStringWithDefault(data interface{}, defaultValue string) string { | ||
if data == nil || data.(string) == "" { | ||
return defaultValue | ||
} | ||
return data.(string) | ||
} | ||
|
||
func ExpandSliceStringPtr(data interface{}) []*string { | ||
if data == nil { | ||
return nil | ||
} | ||
stringSlice := []*string(nil) | ||
for _, s := range data.([]interface{}) { | ||
stringSlice = append(stringSlice, ExpandStringPtr(s)) | ||
} | ||
return stringSlice | ||
} | ||
|
||
func FlattenSliceStringPtr(s []*string) interface{} { | ||
res := make([]interface{}, 0, len(s)) | ||
for _, strPtr := range s { | ||
res = append(res, FlattenStringPtr(strPtr)) | ||
} | ||
return res | ||
} | ||
|
||
func FlattenSliceString(s []string) interface{} { | ||
res := make([]interface{}, 0, len(s)) | ||
for _, strPtr := range s { | ||
res = append(res, strPtr) | ||
} | ||
return res | ||
} | ||
|
||
func ExpandUpdatedStringPtr(data interface{}) *string { | ||
str := "" | ||
if data != nil { | ||
str = data.(string) | ||
} | ||
return &str | ||
} | ||
|
||
func ExpandStrings(data interface{}) []string { | ||
stringSlice := make([]string, 0, len(data.([]interface{}))) | ||
for _, s := range data.([]interface{}) { | ||
// zero-value is nil, ["foo", ""] | ||
if s == nil { | ||
s = "" | ||
} | ||
stringSlice = append(stringSlice, s.(string)) | ||
} | ||
return stringSlice | ||
} | ||
|
||
func ExpandStringsPtr(data interface{}) *[]string { | ||
stringSlice := make([]string, 0, len(data.([]interface{}))) | ||
if _, ok := data.([]interface{}); !ok || data == nil { | ||
return nil | ||
} | ||
for _, s := range data.([]interface{}) { | ||
// zero-value is nil, ["foo", ""] | ||
if s == nil { | ||
s = "" | ||
} | ||
stringSlice = append(stringSlice, s.(string)) | ||
} | ||
if len(stringSlice) == 0 { | ||
return nil | ||
} | ||
|
||
return &stringSlice | ||
} | ||
|
||
// ExpandUpdatedStringsPtr expands a string slice but will default to an empty list. | ||
// Should be used on schema update so emptying a list will update resource. | ||
func ExpandUpdatedStringsPtr(data interface{}) *[]string { | ||
stringSlice := []string{} | ||
if _, ok := data.([]interface{}); !ok || data == nil { | ||
return &stringSlice | ||
} | ||
for _, s := range data.([]interface{}) { | ||
// zero-value is nil, ["foo", ""] | ||
if s == nil { | ||
s = "" | ||
} | ||
stringSlice = append(stringSlice, s.(string)) | ||
} | ||
return &stringSlice | ||
} | ||
|
||
func ExpandSliceIDsPtr(rawIDs interface{}) *[]string { | ||
stringSlice := make([]string, 0, len(rawIDs.([]interface{}))) | ||
if _, ok := rawIDs.([]interface{}); !ok || rawIDs == nil { | ||
return &stringSlice | ||
} | ||
for _, s := range rawIDs.([]interface{}) { | ||
stringSlice = append(stringSlice, locality.ExpandID(s.(string))) | ||
} | ||
return &stringSlice | ||
} | ||
|
||
func ExpandStringsOrEmpty(data interface{}) []string { | ||
stringSlice := make([]string, 0, len(data.([]interface{}))) | ||
if _, ok := data.([]interface{}); !ok || data == nil { | ||
return stringSlice | ||
} | ||
for _, s := range data.([]interface{}) { | ||
stringSlice = append(stringSlice, s.(string)) | ||
} | ||
return stringSlice | ||
} | ||
|
||
func FlattenSliceIDs(certificates []string, zone scw.Zone) interface{} { | ||
res := []interface{}(nil) | ||
for _, certificateID := range certificates { | ||
res = append(res, zonal.NewIDString(zone, certificateID)) | ||
} | ||
|
||
return res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package types_test | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetRandomName(t *testing.T) { | ||
name := types.NewRandomName("test") | ||
assert.True(t, strings.HasPrefix(name, "tf-test-")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package types | ||
|
||
import "time" | ||
|
||
func FlattenDuration(duration *time.Duration) interface{} { | ||
if duration != nil { | ||
return duration.String() | ||
} | ||
return "" | ||
} | ||
|
||
func ExpandDuration(data interface{}) (*time.Duration, error) { | ||
if data == nil || data == "" { | ||
return nil, nil | ||
} | ||
d, err := time.ParseDuration(data.(string)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &d, nil | ||
} | ||
|
||
func FlattenTime(date *time.Time) interface{} { | ||
if date != nil { | ||
return date.Format(time.RFC3339) | ||
} | ||
return "" | ||
} |
Oops, something went wrong.