Skip to content

Commit

Permalink
Merge pull request #2 from whosonfirst/parse
Browse files Browse the repository at this point in the history
Add `?parse-uri= flag` to reader URI to enable default READFUNC
  • Loading branch information
straup authored Dec 12, 2024
2 parents 2fbba11 + ea4953d commit 7fd93bd
Show file tree
Hide file tree
Showing 18 changed files with 844 additions and 9 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ func main() {
ctx := context.Background()
// You can also enable this specific "read function" by passing
// ?parse-uri=true to the reader URI below.
sql_reader.URI_READFUNC = func(uri string) (string, error) {
id, _ := wof_uri.IdFromPath(uri)
str_id := strconv.FormatInt(id, 10)
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ go 1.23.3
require (
github.com/whosonfirst/go-ioutil v1.0.2
github.com/whosonfirst/go-reader v1.0.2
github.com/whosonfirst/go-whosonfirst-uri v1.3.0
)

require (
github.com/aaronland/go-roster v1.0.0 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/whosonfirst/go-whosonfirst-sources v0.1.0 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ github.com/whosonfirst/go-ioutil v1.0.2 h1:+GJPfa42OFn5A+5yJSc5jQTQIkNV3/MhYyg4p
github.com/whosonfirst/go-ioutil v1.0.2/go.mod h1:2dS1vWdAIkiHDvDF8fYyjv6k2NISmwaIjJJeEDBEdvg=
github.com/whosonfirst/go-reader v1.0.2 h1:eccnKKSMGR+X1SJyHUZN0/7qE7VbFQULqSVQU0Su3xs=
github.com/whosonfirst/go-reader v1.0.2/go.mod h1:2w9l/QusYZSiGuEof3RwCHUFnM492SSOF2H7UxS4YIE=
github.com/whosonfirst/go-whosonfirst-sources v0.1.0 h1:JuKLa6KWke22jBfJ1pM9WQHoz1/3pbDv2C+aR+THPPQ=
github.com/whosonfirst/go-whosonfirst-sources v0.1.0/go.mod h1:EUMHyGzUmqPPxlMmOp+28BFeoBdxxE0HCKRd67lkqGM=
github.com/whosonfirst/go-whosonfirst-uri v1.3.0 h1:LYOVLqP9rWQxauYVkdw65j5LZxEi8OK0GHh/qCEpX4g=
github.com/whosonfirst/go-whosonfirst-uri v1.3.0/go.mod h1:CuVygTCUpMG945MMvqHyqxvc/L5YkDaMrrVpRFr7ZxY=
42 changes: 33 additions & 9 deletions sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package reader
import (
"context"
"database/sql"
"errors"
"fmt"
"github.com/whosonfirst/go-ioutil"
wof_reader "github.com/whosonfirst/go-reader"
"io"
_ "log"
"net/url"
"regexp"
"strconv"
"strings"

"github.com/whosonfirst/go-ioutil"
wof_reader "github.com/whosonfirst/go-reader"
wof_uri "github.com/whosonfirst/go-whosonfirst-uri"
)

type readFunc func(string) (string, error)
Expand Down Expand Up @@ -66,7 +67,7 @@ func NewSQLReader(ctx context.Context, uri string) (wof_reader.Reader, error) {
parts := strings.Split(path, "/")

if len(parts) != 3 {
return nil, errors.New("Invalid path")
return nil, fmt.Errorf("Invalid path")
}

table := parts[0]
Expand All @@ -76,7 +77,7 @@ func NewSQLReader(ctx context.Context, uri string) (wof_reader.Reader, error) {
dsn := q.Get("dsn")

if dsn == "" {
return nil, errors.New("Missing dsn parameter")
return nil, fmt.Errorf("Missing dsn parameter")
}

conn, err := sql.Open(driver, dsn)
Expand All @@ -86,15 +87,38 @@ func NewSQLReader(ctx context.Context, uri string) (wof_reader.Reader, error) {
}

if !VALID_TABLE.MatchString(table) {
return nil, errors.New("Invalid table")
return nil, fmt.Errorf("Invalid table")
}

if !VALID_KEY.MatchString(key) {
return nil, errors.New("Invalid key")
return nil, fmt.Errorf("Invalid key")
}

if !VALID_VALUE.MatchString(value) {
return nil, errors.New("Invalid value")
return nil, fmt.Errorf("Invalid value")
}

if q.Has("parse-uri") {

v, err := strconv.ParseBool(q.Get("parse-uri"))

if err != nil {
return nil, fmt.Errorf("Failed to parse ?parse-uri= parameter, %w", err)
}

if v {

URI_READFUNC = func(k string) (string, error) {

id, _, err := wof_uri.ParseURI(k)

if err != nil {
return "", err
}

return strconv.FormatInt(id, 10), nil
}
}
}

r := &SQLReader{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions vendor/github.com/whosonfirst/go-whosonfirst-sources/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions vendor/github.com/whosonfirst/go-whosonfirst-sources/Makefile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions vendor/github.com/whosonfirst/go-whosonfirst-sources/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

94 changes: 94 additions & 0 deletions vendor/github.com/whosonfirst/go-whosonfirst-sources/sources.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions vendor/github.com/whosonfirst/go-whosonfirst-uri/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions vendor/github.com/whosonfirst/go-whosonfirst-uri/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.
Loading

0 comments on commit 7fd93bd

Please sign in to comment.