ray an
ray an

Reputation: 1288

go template: how to pass the variable returned from a function to next command in pipeline

I have this function.

func OrderedParamsFromUri(uri string) []string {
    matches := pathParamRE.FindAllStringSubmatch(uri, -1)
    result := make([]string, len(matches))
    for i, m := range matches {
        result[i] = m[1]
    }
    return result
}

I want to use this function inside my template to check if the returned result contains items or not. I know I can do something like this:

  ( .OperationId | OrderedParamsFromUri | //here i want to check for the empty slice)
  .OperationId  =>  this is the argument.

I know I can check if the returned slice is empty or not using if not .returnedSlice But how to combine these two?

Upvotes: 1

Views: 672

Answers (1)

mkopriva
mkopriva

Reputation: 38313

Either

{{ if not (.OperationId | OrderedParamsFromUri) }}
    empty
{{ else }}
    {{ (.OperationId | OrderedParamsFromUri) }}
{{ end }}

Or

{{ if not (OrderedParamsFromUri .OperationId) }}
    empty
{{ else }}
    {{ (OrderedParamsFromUri .OperationId) }}
{{ end }}

https://play.golang.com/p/rkt7wP_vS4n

Upvotes: 1

Related Questions