From a1ffcb2846520ffdbead787aca2ca333f74e5b4c Mon Sep 17 00:00:00 2001 From: ringsaturn Date: Sun, 26 Mar 2023 18:40:17 +0800 Subject: [PATCH] feat!: return interface instead of structs (#109) --- cmd/comparetzpb/main.go | 4 ++-- cmd/tzf/main.go | 2 +- f.go | 7 +++++++ tzf.go | 6 +++--- tzf_default_finder.go | 10 +++++----- tzf_default_finder_test.go | 2 +- tzf_fuzzy.go | 2 +- tzf_fuzzy_test.go | 2 +- tzf_test.go | 10 +++++----- 9 files changed, 26 insertions(+), 19 deletions(-) create mode 100644 f.go diff --git a/cmd/comparetzpb/main.go b/cmd/comparetzpb/main.go index 3c7afeb..cc85675 100644 --- a/cmd/comparetzpb/main.go +++ b/cmd/comparetzpb/main.go @@ -13,8 +13,8 @@ import ( ) var ( - defaultfinder *tzf.DefaultFinder - fullFinder *tzf.Finder + defaultfinder tzf.F + fullFinder tzf.F ) func init() { diff --git a/cmd/tzf/main.go b/cmd/tzf/main.go index a03877d..965a6f0 100644 --- a/cmd/tzf/main.go +++ b/cmd/tzf/main.go @@ -11,7 +11,7 @@ import ( "google.golang.org/protobuf/proto" ) -var finder *tzf.Finder +var finder tzf.F func init() { input := &pb.CompressedTimezones{} diff --git a/f.go b/f.go new file mode 100644 index 0000000..a1c5c68 --- /dev/null +++ b/f.go @@ -0,0 +1,7 @@ +package tzf + +type F interface { + GetTimezoneName(lng float64, lat float64) string + GetTimezoneNames(lng float64, lat float64) ([]string, error) + TimezoneNames() []string +} diff --git a/tzf.go b/tzf.go index a17f35d..42ef70a 100644 --- a/tzf.go +++ b/tzf.go @@ -98,7 +98,7 @@ type Finder struct { opt *Option } -func NewFinderFromRawJSON(input *convert.BoundaryFile, opts ...OptionFunc) (*Finder, error) { +func NewFinderFromRawJSON(input *convert.BoundaryFile, opts ...OptionFunc) (F, error) { timezones, err := convert.Do(input) if err != nil { return nil, err @@ -106,7 +106,7 @@ func NewFinderFromRawJSON(input *convert.BoundaryFile, opts ...OptionFunc) (*Fin return NewFinderFromPB(timezones, opts...) } -func NewFinderFromPB(input *pb.Timezones, opts ...OptionFunc) (*Finder, error) { +func NewFinderFromPB(input *pb.Timezones, opts ...OptionFunc) (F, error) { now := time.Now() items := make([]*tzitem, 0) names := make([]string, 0) @@ -184,7 +184,7 @@ func NewFinderFromPB(input *pb.Timezones, opts ...OptionFunc) (*Finder, error) { return finder, nil } -func NewFinderFromCompressed(input *pb.CompressedTimezones, opts ...OptionFunc) (*Finder, error) { +func NewFinderFromCompressed(input *pb.CompressedTimezones, opts ...OptionFunc) (F, error) { tzs, err := reduce.Decompress(input) if err != nil { return nil, err diff --git a/tzf_default_finder.go b/tzf_default_finder.go index 8369d0b..237035b 100644 --- a/tzf_default_finder.go +++ b/tzf_default_finder.go @@ -12,12 +12,12 @@ import ( // // It's designed for performance first and allow some not so correct return at some area. type DefaultFinder struct { - fuzzyFinder *FuzzyFinder - finder *Finder + fuzzyFinder F + finder F } -func NewDefaultFinder() (*DefaultFinder, error) { - fuzzyFinder, err := func() (*FuzzyFinder, error) { +func NewDefaultFinder() (F, error) { + fuzzyFinder, err := func() (F, error) { input := &pb.PreindexTimezones{} if err := proto.Unmarshal(tzfrel.PreindexData, input); err != nil { panic(err) @@ -28,7 +28,7 @@ func NewDefaultFinder() (*DefaultFinder, error) { return nil, err } - finder, err := func() (*Finder, error) { + finder, err := func() (F, error) { input := &pb.CompressedTimezones{} if err := proto.Unmarshal(tzfrel.LiteCompressData, input); err != nil { panic(err) diff --git a/tzf_default_finder_test.go b/tzf_default_finder_test.go index 70b0669..27bc89c 100644 --- a/tzf_default_finder_test.go +++ b/tzf_default_finder_test.go @@ -14,7 +14,7 @@ import ( ) var ( - defaultFinder *tzf.DefaultFinder + defaultFinder tzf.F ) func init() { diff --git a/tzf_fuzzy.go b/tzf_fuzzy.go index 2205e71..7436e4f 100644 --- a/tzf_fuzzy.go +++ b/tzf_fuzzy.go @@ -18,7 +18,7 @@ type FuzzyFinder struct { names []string } -func NewFuzzyFinderFromPB(input *pb.PreindexTimezones) (*FuzzyFinder, error) { +func NewFuzzyFinderFromPB(input *pb.PreindexTimezones) (F, error) { f := &FuzzyFinder{ m: make(map[maptile.Tile][]string), idxZoom: int(input.IdxZoom), diff --git a/tzf_fuzzy_test.go b/tzf_fuzzy_test.go index 0677cd3..efb8401 100644 --- a/tzf_fuzzy_test.go +++ b/tzf_fuzzy_test.go @@ -14,7 +14,7 @@ import ( ) var ( - fuzzyFinder *tzf.FuzzyFinder + fuzzyFinder tzf.F ) func init() { diff --git a/tzf_test.go b/tzf_test.go index ca2fe44..f6993db 100644 --- a/tzf_test.go +++ b/tzf_test.go @@ -20,8 +20,8 @@ import ( ) var ( - finder *tzf.Finder - fullFinder *tzf.Finder + finder tzf.F + fullFinder tzf.F ) func init() { @@ -127,7 +127,7 @@ func ExampleFinder_GetTimezoneLoc() { panic(err) } finder, _ := tzf.NewFinderFromPB(input) - fmt.Println(finder.GetTimezoneLoc(116.6386, 40.0786)) + fmt.Println(finder.(*tzf.Finder).GetTimezoneLoc(116.6386, 40.0786)) // Output: Asia/Shanghai } @@ -141,7 +141,7 @@ func ExampleFinder_GetTimezoneShapeByName() { panic(err) } finder, _ := tzf.NewFinderFromPB(input) - pbtz, err := finder.GetTimezoneShapeByName("Asia/Shanghai") + pbtz, err := finder.(*tzf.Finder).GetTimezoneShapeByName("Asia/Shanghai") fmt.Printf("%v %v\n", pbtz.Name, err) // Output: Asia/Shanghai } @@ -156,7 +156,7 @@ func ExampleFinder_GetTimezoneShapeByShift() { panic(err) } finder, _ := tzf.NewFinderFromPB(input) - pbtzs, _ := finder.GetTimezoneShapeByShift(28800) + pbtzs, _ := finder.(*tzf.Finder).GetTimezoneShapeByShift(28800) pbnames := make([]string, 0) for _, pbtz := range pbtzs {