Hey, @Andrea_Bisello I’m not entirely sure I understand what you’re trying to accomplish, but if can make these all arrays you can get the first item in the array (thus treating it like a string) with index, like this:
{{ feature := index (.Params.features ) 0 }}
I didn’t test it, but something like that should work. Hope that helps, and if not, maybe you can clarify a bit more.
Thanks for your help,
but in some of my content, feature is a string, in other feature is an array.
i cannot restore content.
i the template i need to loop over the feature array if feature is array, or i need to print the feature string is feature is a string.
pseudo code :
{{ if .Params.feature is Array }}
// loop
{{ end }}
{{ if .Params.feature is String }}
// print
{{ end }}
In the original post you have “feature” and “features”. Those are two different variables, so you should be okay, ne? You can check for features, and if it isn’t set, you can load feature.
And if that isn’t how it is set up, maybe you ought to set it up that way.
I’m also surprised that there’s no mechanism for determining data types (like reflect.TypeOf), but I think I found a work around.
Using isset with -1 for the second parameter seems to consistently return true for arrays, and false for everything else. Passing 0, it returns true for a non-empty array, but still returns false for an empty array:
WARNING: calling IsSet with unsupported type "invalid" (<nil>) will always return false.
WARNING: calling IsSet with unsupported type "string" (string) will always return false.
WARNING: calling IsSet with unsupported type "int64" (int64) will always return false.
So, in the strictest sense, using isset with -1 seems to work as an “is array” function. However, I’d be inclined to use 0 as that’s probably good enough for most cases and it’s maybe a bit less of a hack.
To make @kaushalmodi’s findings a specific answer to the OP’s question:
{{ $type := (printf "%T" Params.feature) }} // insert variable here
{{ if eq $type "[]interface {}" }}
// do something with array
{{ end }}
{{ if eq $type "string" }}
// do something with string
{{ end }}