Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/Referencing-Request-Values.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ If you are referencing values for environment, you can use `envname` property to
```
to get the QUERY environment variable set to the `q` parameter passed in the query string.

If you need a default value, you can use `default` property to set a value used in case the value is not found or is empty like so
```json
{
"source": "url",
"name": "q",
"default": "123"
}
```
**Note**: The `default` property is ignored when the `source` is set to `string`, `entire-payload`, `entire-headers` or `entire-query`.

# Special cases
If you want to pass the entire payload as JSON string to your command you can use
```json
Expand Down
38 changes: 32 additions & 6 deletions internal/hook/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ func ReplaceParameter(s string, params, value interface{}) bool {
}

// GetParameter extracts interface{} value based on the passed string
func GetParameter(s string, params interface{}) (interface{}, error) {
func GetParameter(s string, params interface{}, defaultValue string) (interface{}, error) {
if params == nil {
return nil, errors.New("no parameters")
}
Expand All @@ -374,45 +374,70 @@ func GetParameter(s string, params interface{}) (interface{}, error) {
return nil, &ParameterNodeError{s}
}

return GetParameter(p[1], params.([]interface{})[index])
return GetParameter(p[1], params.([]interface{})[index], defaultValue)
}

index, err := strconv.ParseUint(s, 10, 64)

if err != nil || paramsValueSliceLength <= int(index) {
// Return default value if nothing found and defaultValue is set
if defaultValue != "" {
return defaultValue, nil
}

return nil, &ParameterNodeError{s}
}

return params.([]interface{})[index], nil
}

// Return default value if nothing found and defaultValue is set
if defaultValue != "" {
return defaultValue, nil
}

return nil, &ParameterNodeError{s}

case reflect.Map:
// Check for raw key
if v, ok := params.(map[string]interface{})[s]; ok {
// Return default value if found value is empty and defaultValue is set
if (v == "" || v == nil) && defaultValue != "" {
return defaultValue, nil
}

return v, nil
}

// Return default value if no dot inside the string and defaultValue is set
if !strings.Contains(s, ".") && defaultValue != "" {
return defaultValue, nil
}

// Checked for dotted references
p := strings.SplitN(s, ".", 2)
if pValue, ok := params.(map[string]interface{})[p[0]]; ok {
if len(p) > 1 {
return GetParameter(p[1], pValue)
return GetParameter(p[1], pValue, defaultValue)
}

return pValue, nil
}
}

// Return default value if nothing found and defaultValue is set
if defaultValue != "" {
return defaultValue, nil
}

return nil, &ParameterNodeError{s}
}

// ExtractParameterAsString extracts value from interface{} as string based on
// the passed string. Complex data types are rendered as JSON instead of the Go
// Stringer format.
func ExtractParameterAsString(s string, params interface{}) (string, error) {
pValue, err := GetParameter(s, params)
func ExtractParameterAsString(s string, params interface{}, defaultValue string) (string, error) {
pValue, err := GetParameter(s, params, defaultValue)
if err != nil {
return "", err
}
Expand All @@ -436,6 +461,7 @@ func ExtractParameterAsString(s string, params interface{}) (string, error) {
type Argument struct {
Source string `json:"source,omitempty"`
Name string `json:"name,omitempty"`
DefaultValue string `json:"default,omitempty"`
EnvName string `json:"envname,omitempty"`
Base64Decode bool `json:"base64decode,omitempty"`
}
Expand Down Expand Up @@ -503,7 +529,7 @@ func (ha *Argument) Get(r *Request) (string, error) {
}

if source != nil {
return ExtractParameterAsString(key, *source)
return ExtractParameterAsString(key, *source, ha.DefaultValue)
}

return "", errors.New("no source for value retrieval")
Expand Down
Loading