From 41990aefe4172335677f7d0a7c178ef69f09cfbb Mon Sep 17 00:00:00 2001 From: Oliver Tonnhofer Date: Fri, 31 May 2013 14:24:22 +0200 Subject: [PATCH] add string_suffixreplace type --- mapping/fields.go | 63 +++++++++++++++++++++++++++++++++--------- mapping/fields_test.go | 21 ++++++++++++++ 2 files changed, 71 insertions(+), 13 deletions(-) diff --git a/mapping/fields.go b/mapping/fields.go index 202e45d..5e3f837 100644 --- a/mapping/fields.go +++ b/mapping/fields.go @@ -2,27 +2,31 @@ package mapping import ( "errors" + "fmt" "goposm/element" "log" + "regexp" "strconv" + "strings" ) var AvailableFieldTypes map[string]FieldType func init() { AvailableFieldTypes = map[string]FieldType{ - "bool": {"bool", "bool", Bool, nil}, - "boolint": {"boolint", "int8", BoolInt, nil}, - "id": {"id", "int64", Id, nil}, - "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}, - "geometry": {"geometry", "geometry", Geometry, nil}, - "wayzorder": {"wayzorder", "int32", WayZOrder, nil}, - "pseudoarea": {"pseudoarea", "float32", PseudoArea, nil}, - "zorder": {"zorder", "int32", nil, MakeZOrder}, + "bool": {"bool", "bool", Bool, nil}, + "boolint": {"boolint", "int8", BoolInt, nil}, + "id": {"id", "int64", Id, nil}, + "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}, + "geometry": {"geometry", "geometry", Geometry, nil}, + "wayzorder": {"wayzorder", "int32", WayZOrder, nil}, + "pseudoarea": {"pseudoarea", "float32", PseudoArea, nil}, + "zorder": {"zorder", "int32", nil, MakeZOrder}, + "string_suffixreplace": {"string_suffixreplace", "string", nil, MakeSuffixReplace}, } } @@ -194,7 +198,6 @@ func WayZOrder(val string, elem *element.OSMElem, match Match) interface{} { } func MakeZOrder(fieldName string, fieldType FieldType, field Field) (MakeValue, error) { - ranks := make(map[string]int) _rankList, ok := field.Args["ranks"] if !ok { return nil, errors.New("missing ranks in args for zorder") @@ -205,6 +208,7 @@ func MakeZOrder(fieldName string, fieldType FieldType, field Field) (MakeValue, return nil, errors.New("ranks in args for zorder not a list") } + ranks := make(map[string]int) for i, rank := range rankList { rankName, ok := rank.(string) if !ok { @@ -222,3 +226,36 @@ func MakeZOrder(fieldName string, fieldType FieldType, field Field) (MakeValue, return zOrder, nil } + +func MakeSuffixReplace(fieldName string, fieldType FieldType, field Field) (MakeValue, error) { + _changes, ok := field.Args["suffixes"] + if !ok { + return nil, errors.New("missing suffixes in args for string_suffixreplace") + } + fmt.Printf("%#v\n", _changes) + + changes, ok := _changes.(map[string]string) + if !ok { + return nil, errors.New("suffixes in args for string_suffixreplace not a list") + } + + var suffixes []string + for k, _ := range changes { + suffixes = append(suffixes, k) + } + reStr := `(` + strings.Join(suffixes, "|") + `)\b` + re := regexp.MustCompile(reStr) + + replFunc := func(match string) string { + return changes[match] + } + + suffixReplace := func(val string, elem *element.OSMElem, match Match) interface{} { + if val != "" { + return re.ReplaceAllStringFunc(val, replFunc) + } + return val + } + + return suffixReplace, nil +} diff --git a/mapping/fields_test.go b/mapping/fields_test.go index 54a3412..0ceaf02 100644 --- a/mapping/fields_test.go +++ b/mapping/fields_test.go @@ -35,3 +35,24 @@ func TestBool(t *testing.T) { } } + +func TestMakeSuffixReplace(t *testing.T) { + field := Field{ + "name", "name", "string_suffixreplace", + map[string]interface{}{"suffixes": map[string]string{"Straße": "Str.", "straße": "str."}}} + suffixReplace, err := MakeSuffixReplace("name", FieldType{}, field) + + if err != nil { + t.Fatal(err) + } + + if result := suffixReplace("Hauptstraße", nil, Match{}); result != "Hauptstr." { + t.Fatal(result) + } + if result := suffixReplace("", nil, Match{}); result != "" { + t.Fatal(result) + } + if result := suffixReplace("Foostraßeee", nil, Match{}); result != "Foostraßeee" { + t.Fatal(result) + } +}