From 93ed22418ead50ca8a697eb775047872ee46ad88 Mon Sep 17 00:00:00 2001 From: sfomuseumbot Date: Fri, 19 Jul 2024 10:21:20 -0700 Subject: [PATCH 1/2] remove flags package; improved docs --- filter/flagset.go | 111 ------------------------------------------- filter/query.go | 15 +++--- flags/common.go | 74 ----------------------------- flags/flags.go | 89 ----------------------------------- flags/iterator.go | 35 -------------- flags/query.go | 117 ---------------------------------------------- 6 files changed, 9 insertions(+), 432 deletions(-) delete mode 100644 filter/flagset.go delete mode 100644 flags/common.go delete mode 100644 flags/flags.go delete mode 100644 flags/iterator.go delete mode 100644 flags/query.go diff --git a/filter/flagset.go b/filter/flagset.go deleted file mode 100644 index 25d976d..0000000 --- a/filter/flagset.go +++ /dev/null @@ -1,111 +0,0 @@ -package filter - -import ( - "flag" - - "github.com/sfomuseum/go-flags/lookup" - "github.com/whosonfirst/go-whosonfirst-spatial" - "github.com/whosonfirst/go-whosonfirst-spatial/flags" -) - -func NewSPRFilterFromFlagSet(fs *flag.FlagSet) (spatial.Filter, error) { - - inputs, err := NewSPRInputsFromFlagSet(fs) - - if err != nil { - return nil, err - } - - return NewSPRFilterFromInputs(inputs) -} - -func NewSPRInputsFromFlagSet(fs *flag.FlagSet) (*SPRInputs, error) { - - inputs, err := NewSPRInputs() - - if err != nil { - return nil, err - } - - placetypes, err := lookup.MultiStringVar(fs, flags.PLACETYPES) - - if err != nil { - return nil, err - } - - inputs.Placetypes = placetypes - - inception_date, err := lookup.StringVar(fs, flags.INCEPTION_DATE) - - if err != nil { - return nil, err - } - - inputs.InceptionDate = inception_date - - cessation_date, err := lookup.StringVar(fs, flags.CESSATION_DATE) - - if err != nil { - return nil, err - } - - inputs.CessationDate = cessation_date - - geometries, err := lookup.StringVar(fs, flags.GEOMETRIES) - - if err != nil { - return nil, err - } - - inputs.Geometries = []string{geometries} - - alt_geoms, err := lookup.MultiStringVar(fs, flags.ALTERNATE_GEOMETRIES) - - if err != nil { - return nil, err - } - - inputs.AlternateGeometries = alt_geoms - - is_current, err := lookup.MultiInt64Var(fs, flags.IS_CURRENT) - - if err != nil { - return nil, err - } - - inputs.IsCurrent = is_current - - is_ceased, err := lookup.MultiInt64Var(fs, flags.IS_CEASED) - - if err != nil { - return nil, err - } - - inputs.IsCeased = is_ceased - - is_deprecated, err := lookup.MultiInt64Var(fs, flags.IS_DEPRECATED) - - if err != nil { - return nil, err - } - - inputs.IsDeprecated = is_deprecated - - is_superseded, err := lookup.MultiInt64Var(fs, flags.IS_SUPERSEDED) - - if err != nil { - return nil, err - } - - inputs.IsSuperseded = is_superseded - - is_superseding, err := lookup.MultiInt64Var(fs, flags.IS_SUPERSEDING) - - if err != nil { - return nil, err - } - - inputs.IsSuperseding = is_superseding - - return inputs, nil -} diff --git a/filter/query.go b/filter/query.go index 9c6e979..6d38818 100644 --- a/filter/query.go +++ b/filter/query.go @@ -1,12 +1,15 @@ package filter import ( + "fmt" "net/url" "strconv" "github.com/whosonfirst/go-whosonfirst-spatial" ) +// NewSPRFilterFromQuery will instantiate new `spatial.Filter` instance derived from parameters +// in 'query'. func NewSPRFilterFromQuery(query url.Values) (spatial.Filter, error) { inputs, err := NewSPRInputs() @@ -25,31 +28,31 @@ func NewSPRFilterFromQuery(query url.Values) (spatial.Filter, error) { is_current, err := atoi(query["is_current"]) if err != nil { - return nil, err + return nil, fmt.Errorf("Invalid ?is_current= parameter, %w", err) } is_deprecated, err := atoi(query["is_deprecated"]) if err != nil { - return nil, err + return nil, fmt.Errorf("Invalid ?is_deprecated= parameter, %w", err) } is_ceased, err := atoi(query["is_ceased"]) if err != nil { - return nil, err + return nil, fmt.Errorf("Invalid ?is_ceased= parameter, %w", err) } is_superseded, err := atoi(query["is_superseded"]) if err != nil { - return nil, err + return nil, fmt.Errorf("Invalid ?is_superseded= parameter, %w", err) } is_superseding, err := atoi(query["is_superseding"]) if err != nil { - return nil, err + return nil, fmt.Errorf("Invalid ?is_superseding= parameter, %w", err) } inputs.IsCurrent = is_current @@ -70,7 +73,7 @@ func atoi(strings []string) ([]int64, error) { i, err := strconv.ParseInt(str, 10, 64) if err != nil { - return nil, err + return nil, fmt.Errorf("Failed to parse integer value at offset %d, %w", idx, err) } numbers[idx] = i diff --git a/flags/common.go b/flags/common.go deleted file mode 100644 index 0de388e..0000000 --- a/flags/common.go +++ /dev/null @@ -1,74 +0,0 @@ -package flags - -import ( - "flag" - "fmt" - - "github.com/sfomuseum/go-flags/flagset" - "github.com/sfomuseum/go-flags/lookup" - "github.com/whosonfirst/go-reader" - // "github.com/whosonfirst/go-whosonfirst-spatial/database" -) - -func CommonFlags() (*flag.FlagSet, error) { - - fs := flagset.NewFlagSet("common") - - err := AppendCommonFlags(fs) - - if err != nil { - return nil, fmt.Errorf("Failed to append common application flags, %w", err) - } - - return fs, nil -} - -func AppendCommonFlags(fs *flag.FlagSet) error { - - // spatial databases - - // available_databases := database.Schemes() - desc_databases := fmt.Sprintf("A valid whosonfirst/go-whosonfirst-spatial/data.SpatialDatabase URI. options are: %s", "...") - - fs.String(SpatialDatabaseURIFlag, "", desc_databases) - - available_readers := reader.Schemes() - desc_readers := fmt.Sprintf("A valid whosonfirst/go-reader.Reader URI. Available options are: %s", available_readers) - - fs.String(PropertiesReaderURIFlag, "", fmt.Sprintf("%s. If the value is {spatial-database-uri} then the value of the '-spatial-database-uri' implements the reader.Reader interface and will be used.", desc_readers)) - - fs.Bool(IS_WOF, true, "Input data is WOF-flavoured GeoJSON. (Pass a value of '0' or 'false' if you need to index non-WOF documents.") - - fs.Bool(EnableCustomPlacetypesFlag, false, "Enable wof:placetype values that are not explicitly defined in the whosonfirst/go-whosonfirst-placetypes repository.") - - // Pending changes in the app/placetypes.go package to support - // alternate sources (20210324/thisisaaronland) - // fs.String(CUSTOM_PLACETYPES_SOURCE, "", "...") - - fs.String(CustomPlacetypesFlag, "", "A JSON-encoded string containing custom placetypes defined using the syntax described in the whosonfirst/go-whosonfirst-placetypes repository.") - - fs.Bool(VerboseFlag, false, "Be chatty.") - - return nil -} - -func ValidateCommonFlags(fs *flag.FlagSet) error { - - spatial_database_uri, err := lookup.StringVar(fs, SpatialDatabaseURIFlag) - - if err != nil { - return fmt.Errorf("Failed to lookup %s flag, %w", SpatialDatabaseURIFlag, err) - } - - if spatial_database_uri == "" { - return fmt.Errorf("Invalid or missing -%s flag", SpatialDatabaseURIFlag) - } - - _, err = lookup.StringVar(fs, PropertiesReaderURIFlag) - - if err != nil { - return fmt.Errorf("Failed to lookup %s flag, %w", PropertiesReaderURIFlag, err) - } - - return nil -} diff --git a/flags/flags.go b/flags/flags.go deleted file mode 100644 index 13eb6a3..0000000 --- a/flags/flags.go +++ /dev/null @@ -1,89 +0,0 @@ -package flags - -const AlternateGeometriesFlag string = "alternate-geometry" - -const CessationDateFlag string = "cessation-date" - -const CustomPlacetypesFlag string = "custom-placetypes" - -const CustomPlacetypesSourceFlag string = "custom-placetypes-source" - -const EnableCustomPlacetypesFlag string = "enable-custom-placetypes" - -const GeometriesFlag string = "geometries" - -const InceptionDateFlag string = "inception-date" - -const IsCeasedFlag string = "is-ceased" - -const IsCurrentFlag string = "is-current" - -const IsDeprecatedFlag string = "is-deprecated" - -const IsSupersededFlag string = "is-superseded" - -const IsSupersedingFlag string = "is-superseding" - -const IteratorURIFlag string = "iterator-uri" - -const LatitudeFlag string = "latitude" - -const LongitudeFlag string = "longitude" - -const PlacetypeFlag string = "placetype" - -const PropertyFlag string = "property" - -const SpatialDatabaseURIFlag string = "spatial-database-uri" - -const PropertiesReaderURIFlag string = "properties-reader-uri" - -const VerboseFlag string = "verbose" - -const SortURIFlag string = "sort-uri" - -// Deprecated. These will be removed once this package reaches v1.0.0 - -const ALTERNATE_GEOMETRIES string = AlternateGeometriesFlag - -const CESSATION_DATE string = CessationDateFlag - -const CUSTOM_PLACETYPES string = CustomPlacetypesFlag - -const CUSTOM_PLACETYPES_SOURCE string = CustomPlacetypesSourceFlag - -const ENABLE_CUSTOM_PLACETYPES string = EnableCustomPlacetypesFlag - -const GEOMETRIES string = GeometriesFlag - -const INCEPTION_DATE string = InceptionDateFlag - -const IS_CEASED string = IsCeasedFlag - -const IS_CURRENT string = IsCurrentFlag - -const IS_DEPRECATED string = IsDeprecatedFlag - -const IS_SUPERSEDED string = IsSupersededFlag - -const IS_SUPERSEDING string = IsSupersedingFlag - -const ITERATOR_URI string = IteratorURIFlag - -const LATITUDE string = LatitudeFlag - -const LONGITUDE string = LongitudeFlag - -const PLACETYPES string = PlacetypeFlag - -const PROPERTIES string = PropertyFlag - -const SPATIAL_DATABASE_URI string = SpatialDatabaseURIFlag - -const PROPERTIES_READER_URI string = PropertiesReaderURIFlag - -const VERBOSE string = VerboseFlag - -const IS_WOF string = "is-wof" - -const SORT string = SortURIFlag diff --git a/flags/iterator.go b/flags/iterator.go deleted file mode 100644 index 13d7f78..0000000 --- a/flags/iterator.go +++ /dev/null @@ -1,35 +0,0 @@ -package flags - -import ( - "flag" - "fmt" - "sort" - "strings" - - "github.com/sfomuseum/go-flags/lookup" - "github.com/whosonfirst/go-whosonfirst-iterate/v2/emitter" -) - -func AppendIndexingFlags(fs *flag.FlagSet) error { - - modes := emitter.Schemes() - sort.Strings(modes) - - valid_modes := strings.Join(modes, ", ") - desc_modes := fmt.Sprintf("A valid whosonfirst/go-whosonfirst-iterate/v2 URI. Supported schemes are: %s.", valid_modes) - - fs.String(IteratorURIFlag, "repo://", desc_modes) - - return nil -} - -func ValidateIndexingFlags(fs *flag.FlagSet) error { - - _, err := lookup.StringVar(fs, IteratorURIFlag) - - if err != nil { - return fmt.Errorf("Failed to lookup %s flag, %w", IteratorURIFlag, err) - } - - return nil -} diff --git a/flags/query.go b/flags/query.go deleted file mode 100644 index 044cf13..0000000 --- a/flags/query.go +++ /dev/null @@ -1,117 +0,0 @@ -package flags - -import ( - "flag" - "fmt" - - "github.com/sfomuseum/go-flags/lookup" - "github.com/sfomuseum/go-flags/multi" - "github.com/whosonfirst/go-whosonfirst-spatial/geo" -) - -func AppendQueryFlags(fs *flag.FlagSet) error { - - fs.Float64(LatitudeFlag, 0.0, "A valid latitude.") - fs.Float64(LongitudeFlag, 0.0, "A valid longitude.") - - fs.String(GeometriesFlag, "all", "Valid options are: all, alt, default.") - - fs.String(InceptionDateFlag, "", "A valid EDTF date string.") - fs.String(CessationDateFlag, "", "A valid EDTF date string.") - - var props multi.MultiString - fs.Var(&props, PropertyFlag, "One or more Who's On First properties to append to each result.") - - var placetypes multi.MultiString - fs.Var(&placetypes, PlacetypeFlag, "One or more place types to filter results by.") - - var alt_geoms multi.MultiString - fs.Var(&alt_geoms, AlternateGeometriesFlag, "One or more alternate geometry labels (wof:alt_label) values to filter results by.") - - var is_current multi.MultiInt64 - fs.Var(&is_current, IsCurrentFlag, "One or more existential flags (-1, 0, 1) to filter results by.") - - var is_ceased multi.MultiInt64 - fs.Var(&is_ceased, IsCeasedFlag, "One or more existential flags (-1, 0, 1) to filter results by.") - - var is_deprecated multi.MultiInt64 - fs.Var(&is_deprecated, IsDeprecatedFlag, "One or more existential flags (-1, 0, 1) to filter results by.") - - var is_superseded multi.MultiInt64 - fs.Var(&is_superseded, IsSupersededFlag, "One or more existential flags (-1, 0, 1) to filter results by.") - - var is_superseding multi.MultiInt64 - fs.Var(&is_superseding, IsSupersedingFlag, "One or more existential flags (-1, 0, 1) to filter results by.") - - var sort multi.MultiString - fs.Var(&sort, SortURIFlag, "Zero or more whosonfirst/go-whosonfirst-spr/sort URIs.") - - return nil -} - -func ValidateQueryFlags(fs *flag.FlagSet) error { - - lat, err := lookup.Float64Var(fs, LatitudeFlag) - - if err != nil { - return fmt.Errorf("Failed to lookup %s flag, %w", LatitudeFlag, err) - } - - lon, err := lookup.Float64Var(fs, LongitudeFlag) - - if err != nil { - return fmt.Errorf("Failed to lookup %s flag, %w", LongitudeFlag, err) - } - - if !geo.IsValidLatitude(lat) { - return fmt.Errorf("Invalid latitude") - } - - if !geo.IsValidLongitude(lon) { - return fmt.Errorf("Invalid longitude") - } - - _, err = lookup.StringVar(fs, GeometriesFlag) - - if err != nil { - return fmt.Errorf("Failed to lookup %s flag, %w", GeometriesFlag, err) - } - - _, err = lookup.MultiStringVar(fs, AlternateGeometriesFlag) - - if err != nil { - return fmt.Errorf("Failed to lookup %s flag, %w", AlternateGeometriesFlag, err) - } - - _, err = lookup.MultiInt64Var(fs, IsCurrentFlag) - - if err != nil { - return fmt.Errorf("Failed to lookup %s flag, %w", IsCurrentFlag, err) - } - - _, err = lookup.MultiInt64Var(fs, IsCeasedFlag) - - if err != nil { - return fmt.Errorf("Failed to lookup %s flag, %w", IsCeasedFlag, err) - } - - _, err = lookup.MultiInt64Var(fs, IsDeprecatedFlag) - - if err != nil { - return fmt.Errorf("Failed to lookup %s flag, %w", IsDeprecatedFlag, err) - } - - _, err = lookup.MultiInt64Var(fs, IsSupersededFlag) - - if err != nil { - return fmt.Errorf("Failed to lookup %s flag, %w", IsSupersededFlag, err) - } - - _, err = lookup.MultiInt64Var(fs, IsSupersedingFlag) - - if err != nil { - return fmt.Errorf("Failed to lookup %s flag, %w", IsSupersedingFlag, err) - } - - return nil -} From f821b83d9de72739231957d5b3f5b61f257d8007 Mon Sep 17 00:00:00 2001 From: sfomuseumbot Date: Fri, 19 Jul 2024 11:23:54 -0700 Subject: [PATCH 2/2] update docs to reflect grpc changes --- README.md | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index d7fd003..99d7791 100644 --- a/README.md +++ b/README.md @@ -57,12 +57,39 @@ func main() { } } ``` - Where: * The bulk of the application code is implemented by the `whosonfirst/go-whosonfirst-spatial-www` package. * The specific SQLite implementation of the spatial database is implemented by the `whosonfirst/go-whosonfirst-spatial-sqlite` package. +Here is a another example, implementing a point-in-polygon gRPC service using a SQLite backend: + +``` +package main + +import ( + "context" + "log" + + _ "github.com/whosonfirst/go-whosonfirst-spatial-sqlite" + "github.com/whosonfirst/go-whosonfirst-spatial-grpc/application/server" +) + +func main() { + + ctx := context.Background() + logger := log.Default() + + err := server.Run(ctx, logger) + + if err != nil { + logger.Fatal(err) + } +} +``` + +The only change is that `github.com/whosonfirst/go-whosonfirst-spatial-www/application/server` is replaced by `github.com/whosonfirst/go-whosonfirst-spatial-grpc/application/server`. + The overall motivation for this approach is: * Staying out people's database or delivery choices (or needs) @@ -138,15 +165,14 @@ _Where `flags.*` refers to the [whosonfirst/go-whosonfirst-flags](https://github * https://github.com/whosonfirst/go-whosonfirst-spatial-www -_Remember, this package implements the guts of a web application but does not support any particular database by default. It is meant to be imported by a database-specific implementation (see above) and exposed as a `cmd/server` application (for example) by that package._ +_Remember, this package implements the guts of a web application but does not support any particular database by default. It is meant to be imported by a database-specific implementation (see above) and exposed as a `cmd/http-server` application (for example) by that package._ ### gRPC * https://github.com/whosonfirst/go-whosonfirst-spatial-grpc -* https://github.com/whosonfirst/go-whosonfirst-spatial-grpc-sqlite -* https://github.com/whosonfirst/go-whosonfirst-spatial-grpc-pmtiles -_Note, the gRPC code has not been updated in a while and needs to be refactored to follow the model of the `go-whosonfirst-spatial-www` pacakge._ +_Remember, this package implements the guts of a web application but does not support any particular database by default. It is meant to be imported by a database-specific implementation (see above) and exposed as a `cmd/grpc-server` application (for example) by that package._ + ## See also * https://github.com/whosonfirst/go-whosonfirst-spr