Skip to content

Commit

Permalink
Merge branch 'master' of github.com:CrunchyData/pg_tileserv
Browse files Browse the repository at this point in the history
  • Loading branch information
pramsey committed Mar 27, 2020
2 parents f0dc92d + d931a5c commit 8f77645
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
29 changes: 27 additions & 2 deletions layer_proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type FunctionDetailJson struct {
Schema string `json:"schema"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
Arguments []FunctionArgument `json:"arguments,omitempty"`
Arguments []FunctionArgument `json:"arguments"`
MinZoom int `json:"minzoom"`
MaxZoom int `json:"maxzoom"`
TileUrl string `json:"tileurl"`
Expand Down Expand Up @@ -143,6 +143,7 @@ func (lyr *LayerFunction) getFunctionDetailJson(req *http.Request) (FunctionDeta
Schema: lyr.Schema,
Name: lyr.Function,
Description: lyr.Description,
Arguments: make([]FunctionArgument, 0),
MinZoom: viper.GetInt("DefaultMinZoom"),
MaxZoom: viper.GetInt("DefaultMaxZoom"),
}
Expand Down Expand Up @@ -222,7 +223,7 @@ func GetFunctionLayers() ([]LayerFunction, error) {
order: i - 3,
Name: argnames[i],
Type: argtypes[i],
Default: argdef,
Default: parseArgDefault(argdef),
}
}

Expand All @@ -243,3 +244,27 @@ func GetFunctionLayers() ([]LayerFunction, error) {
rows.Close()
return layerFunctions, nil
}

// parseArgDefault parses a default for an argument to a function-based
// tile layer. Most default arguments don't require special handling,
// but some are returned quoted with a type; e.g. a negative integer
// is `'-123'::integer` instead of `-123`.
func parseArgDefault(arg string) string {
// check for a value in the value::type format
sp := strings.Split(arg, "::")

if len(sp) > 1 {
// join back all but the last split parts.
// this allows for the edge case of a double colon :: in text strings
val := strings.Join(sp[:len(sp)-1], "::")

// check for a value wrapped in single quotes and return the value
// with them stripped. If the value is not wrapped in quotes,
// fall back to returning the value as is.
if val[0] == '\'' && val[len(val)-1] == '\'' {
return val[1 : len(val)-1]
}
}

return arg
}
26 changes: 26 additions & 0 deletions layer_proc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"testing"
)

func TestParseArgDefault(t *testing.T) {
var tests = []struct {
input string
output string
}{
{"foo", "foo"},
{"123", "123"},
{"'-123'::integer", "-123"},
{"'-123.1'::numeric", "-123.1"},
{"'foo'::text", "foo"},
{"'foo::bar'::text", "foo::bar"},
}

for _, test := range tests {
if output := parseArgDefault(test.input); output != test.output {
t.Error("Test Failed: {} inputted, {} expected, recieved: {}", test.input, test.output, output)
}
}

}

0 comments on commit 8f77645

Please sign in to comment.