You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
986 B

package flattens
import (
"context"
"fmt"
"strconv"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
// Meta flattens []interface{} golang struct to a list of terraform framework types.StringType
func Meta(ctx context.Context, m []interface{}) types.List {
tflog.Info(ctx, "Start flattenMeta")
tempSlice := make([]string, 0, len(m))
for _, item := range m {
switch d := item.(type) {
case string:
tempSlice = append(tempSlice, d)
case int:
tempSlice = append(tempSlice, strconv.Itoa(d))
case int64:
tempSlice = append(tempSlice, strconv.FormatInt(d, 10))
case float64:
tempSlice = append(tempSlice, strconv.FormatInt(int64(d), 10))
default:
tempSlice = append(tempSlice, "")
}
}
res, diags := types.ListValueFrom(ctx, types.StringType, tempSlice)
if diags.HasError() {
tflog.Error(ctx, fmt.Sprint("Error flattenMeta:", diags))
}
tflog.Info(ctx, "End flattenMeta")
return res
}