convert first column value

master
Oliver Tonnhofer 2013-05-14 09:56:11 +02:00
parent 84bc8e0fb1
commit 560f659b5b
3 changed files with 33 additions and 10 deletions

View File

@ -220,6 +220,23 @@ func NewMapping(filename string) (*Mapping, error) {
return &mapping, nil
}
func Bool(val string, tags map[string]string, elem interface{}) interface{} {
if val == "" || val == "0" || val == "false" || val == "no" {
return false
}
return true
}
func Direction(val string, tags map[string]string, elem interface{}) interface{} {
if val == "1" || val == "yes" || val == "true" {
return 1
} else if val == "-1" {
return -1
} else {
return 0
}
}
func main() {
// data := `
// {

View File

@ -22,8 +22,9 @@ type DB interface {
}
type ColumnSpec struct {
Name string
Type string
Name string
Type string
Value func(string, map[string]string, interface{}) interface{}
}
type TableSpec struct {
Name string
@ -65,7 +66,11 @@ func (spec *TableSpec) WayValues(way element.Way) []interface{} {
if !ok {
values = append(values, nil)
} else {
values = append(values, v)
if col.Value != nil {
values = append(values, col.Value(v, way.Tags, way))
} else {
values = append(values, v)
}
}
}
return values
@ -216,7 +221,7 @@ func (pg *PostGIS) InsertWays(ways []element.Way, spec TableSpec) error {
for _, w := range ways {
_, err := stmt.Exec(spec.WayValues(w)...)
if err != nil {
return &SQLInsertError{SQLError{sql, err}, w}
return &SQLInsertError{SQLError{sql, err}, spec.WayValues(w)}
}
}

View File

@ -248,26 +248,27 @@ func main() {
waitFill := sync.WaitGroup{}
wayChan := make(chan []element.Way)
waitDb := &sync.WaitGroup{}
config := db.Config{
conf := db.Config{
Type: "postgres",
ConnectionParams: *connection,
Srid: 3857,
Schema: "public",
}
pg, err := db.Open(config)
pg, err := db.Open(conf)
if err != nil {
log.Fatal(err)
}
specs := []db.TableSpec{
{
"goposm_test",
config.Schema,
conf.Schema,
[]db.ColumnSpec{
{"name", "VARCHAR"},
{"highway", "VARCHAR"},
{"name", "VARCHAR", nil},
{"highway", "VARCHAR", nil},
{"oneway", "SMALLINT", config.Direction},
},
"GEOMETRY",
config.Srid,
conf.Srid,
},
}
err = pg.Init(specs)