add Key and Value type

master
Oliver Tonnhofer 2014-04-30 16:33:07 +02:00
parent 4fc6dfdac6
commit 21a389b2cd
4 changed files with 34 additions and 31 deletions

View File

@ -9,7 +9,7 @@ import (
type Field struct {
Name string `json:"name"`
Key string `json:"key"`
Key Key `json:"key"`
Type string `json:"type"`
Args map[string]interface{} `json:"args"`
}
@ -17,14 +17,14 @@ type Field struct {
type Table struct {
Name string
Type TableType `json:"type"`
Mapping map[string][]string `json:"mapping"`
Mapping map[Key][]Value `json:"mapping"`
Mappings map[string]SubMapping `json:"mappings"`
Fields []*Field `json:"fields"`
Filters *Filters `json:"filters"`
}
type SubMapping struct {
Mapping map[string][]string
Mapping map[Key][]Value
}
type GeneralizedTable struct {
@ -49,7 +49,7 @@ type Mapping struct {
type ElementFilter func(tags *element.Tags) bool
type TagTables map[string]map[string][]DestTable
type TagTables map[Key]map[Value][]DestTable
type DestTable struct {
Name string
@ -85,8 +85,8 @@ func NewMapping(filename string) (*Mapping, error) {
return &mapping, nil
}
func (t *Table) ExtraTags() map[string]bool {
tags := make(map[string]bool)
func (t *Table) ExtraTags() map[Key]bool {
tags := make(map[Key]bool)
for _, field := range t.Fields {
if field.Key != "" {
tags[field.Key] = true
@ -116,14 +116,14 @@ func (m *Mapping) prepare() error {
return nil
}
func (tt TagTables) addFromMapping(mapping map[string][]string, table DestTable) {
func (tt TagTables) addFromMapping(mapping map[Key][]Value, table DestTable) {
for key, vals := range mapping {
for _, v := range vals {
vals, ok := tt[key]
if ok {
vals[v] = append(vals[v], table)
} else {
tt[key] = make(map[string][]DestTable)
tt[key] = make(map[Value][]DestTable)
tt[key][v] = append(tt[key][v], table)
}
}
@ -154,7 +154,7 @@ func (m *Mapping) tables(tableType TableType) map[string]*TableFields {
return result
}
func (m *Mapping) extraTags(tableType TableType, tags map[string]bool) {
func (m *Mapping) extraTags(tableType TableType, tags map[Key]bool) {
for _, t := range m.Tables {
if t.Type != tableType {
continue
@ -164,7 +164,7 @@ func (m *Mapping) extraTags(tableType TableType, tags map[string]bool) {
}
if t.Filters != nil && t.Filters.ExcludeTags != nil {
for _, keyVal := range *t.Filters.ExcludeTags {
tags[keyVal[0]] = true
tags[Key(keyVal[0])] = true
}
}
}

View File

@ -21,8 +21,8 @@ func init() {
"string": {"string", "string", String, nil},
"direction": {"direction", "int8", Direction, nil},
"integer": {"integer", "int32", Integer, nil},
"mapping_key": {"mapping_key", "string", Key, nil},
"mapping_value": {"mapping_value", "string", Value, nil},
"mapping_key": {"mapping_key", "string", KeyName, nil},
"mapping_value": {"mapping_value", "string", ValueName, nil},
"geometry": {"geometry", "geometry", Geometry, nil},
"validated_geometry": {"validated_geometry", "validated_geometry", Geometry, nil},
"wayzorder": {"wayzorder", "int32", WayZOrder, nil},
@ -36,14 +36,17 @@ type MakeValue func(string, *element.OSMElem, Match) interface{}
type MakeMakeValue func(string, FieldType, Field) (MakeValue, error)
type Key string
type Value string
type FieldSpec struct {
Key string
Key Key
Type FieldType
}
func (f *FieldSpec) Value(elem *element.OSMElem, match Match) interface{} {
if f.Type.Func != nil {
return f.Type.Func(elem.Tags[f.Key], elem, match)
return f.Type.Func(elem.Tags[string(f.Key)], elem, match)
}
return nil
}
@ -130,11 +133,11 @@ func Id(val string, elem *element.OSMElem, match Match) interface{} {
return elem.Id
}
func Key(val string, elem *element.OSMElem, match Match) interface{} {
func KeyName(val string, elem *element.OSMElem, match Match) interface{} {
return match.Key
}
func Value(val string, elem *element.OSMElem, match Match) interface{} {
func ValueName(val string, elem *element.OSMElem, match Match) interface{} {
return match.Value
}

View File

@ -5,32 +5,32 @@ import (
)
func (m *Mapping) NodeTagFilter() *TagFilter {
mappings := make(map[string]map[string][]DestTable)
mappings := make(map[Key]map[Value][]DestTable)
m.mappings("point", mappings)
tags := make(map[string]bool)
tags := make(map[Key]bool)
m.extraTags("point", tags)
return &TagFilter{mappings, tags}
}
func (m *Mapping) WayTagFilter() *TagFilter {
mappings := make(map[string]map[string][]DestTable)
mappings := make(map[Key]map[Value][]DestTable)
m.mappings("linestring", mappings)
m.mappings("polygon", mappings)
tags := make(map[string]bool)
tags := make(map[Key]bool)
m.extraTags("linestring", tags)
m.extraTags("polygon", tags)
return &TagFilter{mappings, tags}
}
func (m *Mapping) RelationTagFilter() *RelationTagFilter {
mappings := make(map[string]map[string][]DestTable)
mappings := make(map[Key]map[Value][]DestTable)
m.mappings("linestring", mappings)
m.mappings("polygon", mappings)
tags := make(map[string]bool)
tags := make(map[Key]bool)
m.extraTags("linestring", tags)
m.extraTags("polygon", tags)
// do not filter out type tag
mappings["type"] = map[string][]DestTable{
mappings["type"] = map[Value][]DestTable{
"multipolygon": []DestTable{},
"boundary": []DestTable{},
"land_area": []DestTable{},
@ -39,8 +39,8 @@ func (m *Mapping) RelationTagFilter() *RelationTagFilter {
}
type TagFilter struct {
mappings map[string]map[string][]DestTable
extraTags map[string]bool
mappings map[Key]map[Value][]DestTable
extraTags map[Key]bool
}
type RelationTagFilter struct {
@ -53,18 +53,18 @@ func (f *TagFilter) Filter(tags *element.Tags) bool {
}
foundMapping := false
for k, v := range *tags {
values, ok := f.mappings[k]
values, ok := f.mappings[Key(k)]
if ok {
if _, ok := values["__any__"]; ok {
foundMapping = true
continue
} else if _, ok := values[v]; ok {
} else if _, ok := values[Value(v)]; ok {
foundMapping = true
continue
} else if _, ok := f.extraTags[k]; !ok {
} else if _, ok := f.extraTags[Key(k)]; !ok {
delete(*tags, k)
}
} else if _, ok := f.extraTags[k]; !ok {
} else if _, ok := f.extraTags[Key(k)]; !ok {
delete(*tags, k)
}
}

View File

@ -46,14 +46,14 @@ func (tagMatcher *TagMatcher) Match(tags *element.Tags) []Match {
tables := make(map[DestTable]Match)
for k, v := range *tags {
values, ok := tagMatcher.mappings[k]
values, ok := tagMatcher.mappings[Key(k)]
if ok {
if tbls, ok := values["__any__"]; ok {
for _, t := range tbls {
tables[t] = Match{k, v, t, tagMatcher.tables[t.Name]}
}
}
if tbls, ok := values[v]; ok {
if tbls, ok := values[Value(v)]; ok {
for _, t := range tbls {
tables[t] = Match{k, v, t, tagMatcher.tables[t.Name]}
}