Skip to content

Commit

Permalink
Review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bemyak committed Oct 17, 2022
1 parent 478f41f commit 35e3024
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 38 deletions.
10 changes: 5 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,19 +211,19 @@ func (c *Config) Validate() error {
// we can only have the same provider for all layers.
// This allow us to track what the first found provider
// is.
currProvider := ""
currentProvider := ""
isMVTProvider := false
for layerKey, l := range m.Layers {
pname, _, err := l.ProviderLayerName()
if err != nil {
return err
}

if currProvider == "" {
if currentProvider == "" {
// This is the first provider we found.
// For MVTProviders all others need to be the same, so store it
// so we can check later
currProvider = pname
currentProvider = pname
}

isMvt, doesExists := mvtproviders[pname]
Expand All @@ -240,13 +240,13 @@ func (c *Config) Validate() error {
isMVTProvider = isMVTProvider || isMvt

// only need to do this check if we are dealing with MVTProviders
if isMVTProvider && pname != currProvider {
if isMVTProvider && pname != currentProvider {
// for mvt_providers we can only have the same provider
// for all layers
// check to see
if mvtproviders[pname] || isMVTProvider {
return ErrMVTDifferentProviders{
Original: currProvider,
Original: currentProvider,
Current: pname,
}
}
Expand Down
11 changes: 4 additions & 7 deletions provider/query_parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,22 @@ func (param *QueryParameter) ToValue(rawValue string) (QueryParameterValue, erro
func (param *QueryParameter) ToDefaultValue() (QueryParameterValue, error) {
if len(param.DefaultValue) > 0 {
val, err := ParamTypeDecoders[param.Type](param.DefaultValue)
if err != nil {
return QueryParameterValue{}, err
}
return QueryParameterValue{
Token: param.Token,
SQL: param.SQL,
Value: val,
RawParam: param.Name,
RawValue: "",
}, nil
} else if len(param.DefaultSQL) > 0 {
}, err
}
if len(param.DefaultSQL) > 0 {
return QueryParameterValue{
Token: param.Token,
SQL: param.DefaultSQL,
Value: nil,
RawParam: param.Name,
RawValue: "",
}, nil
} else {
return QueryParameterValue{}, fmt.Errorf("the required parameter %s is not specified", param.Name)
}
return QueryParameterValue{}, fmt.Errorf("the required parameter %s is not specified", param.Name)
}
57 changes: 33 additions & 24 deletions provider/query_parameter_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

// Query parameter holds normalized parameter data ready to be inserted in the
// final query
// final query
type QueryParameterValue struct {
// Token to replace e.g., !TOKEN!
Token string
Expand All @@ -24,42 +24,51 @@ type QueryParameterValue struct {
type Params map[string]QueryParameterValue

// ReplaceParams substitutes configured query parameter tokens for their values
// within the provided SQL string
// within the provided SQL string
func (params Params) ReplaceParams(sql string, args *[]interface{}) string {
if params == nil {
return sql
}

cache := make(map[string]string)
var (
cache = make(map[string]string)
sb strings.Builder
)

for _, token := range ParameterTokenRegexp.FindAllString(sql, -1) {
resultSQL := ""
resultSQL, ok := cache[token]
if ok {
// Already have it cached, replace the token and move on.
sql = strings.ReplaceAll(sql, token, resultSQL)
continue
}

if t, ok := cache[token]; ok {
resultSQL = t
} else if param, ok := params[token]; !ok {
param, ok := params[token]
if !ok {
// Unknown token, ignoring
continue
} else {
// Replace every ? in the param's SQL with a positional argument
sb := strings.Builder{}
sb.Grow(len(param.SQL))
argFound := false
for _, c := range param.SQL {
if c == '?' {
if !argFound {
*args = append(*args, param.Value)
argFound = true
}
sb.WriteString(fmt.Sprintf("$%d", len(*args)))
} else {
sb.WriteRune(c)
}
}

sb.Reset()
sb.Grow(len(param.SQL))
argFound := false

// Replace every `?` in the param's SQL with a positional argument
for _, c := range param.SQL {
if c != '?' {
sb.WriteRune(c)
continue
}

if !argFound {
*args = append(*args, param.Value)
argFound = true
}
resultSQL = sb.String()
cache[token] = resultSQL
sb.WriteString(fmt.Sprintf("$%d", len(*args)))
}

resultSQL = sb.String()
cache[token] = resultSQL
sql = strings.ReplaceAll(sql, token, resultSQL)
}

Expand Down
4 changes: 2 additions & 2 deletions tile.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const (

var UnknownConversionError = fmt.Errorf("do not know how to convert value to requested value")

//Tile slippy map tilenames
// Tile slippy map tilenames
// http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
type Tile struct {
Z uint
Expand Down Expand Up @@ -198,7 +198,7 @@ func (t *Tile) ZLevel() uint {
return t.Z
}

//ZRes takes a web mercator zoom level and returns the pixel resolution for that
// ZRes takes a web mercator zoom level and returns the pixel resolution for that
// scale, assuming t.Extent x t.Extent pixel tiles. Non-integer zoom levels are accepted.
// ported from: https://raw.githubusercontent.com/mapbox/postgis-vt-util/master/postgis-vt-util.sql
// 40075016.6855785 is the equator in meters for WGS84 at z=0
Expand Down

0 comments on commit 35e3024

Please sign in to comment.