Skip to content

Commit

Permalink
feat!: return interface instead of structs (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
ringsaturn authored Mar 26, 2023
1 parent ff120b5 commit a1ffcb2
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 19 deletions.
4 changes: 2 additions & 2 deletions cmd/comparetzpb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
)

var (
defaultfinder *tzf.DefaultFinder
fullFinder *tzf.Finder
defaultfinder tzf.F
fullFinder tzf.F
)

func init() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/tzf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"google.golang.org/protobuf/proto"
)

var finder *tzf.Finder
var finder tzf.F

func init() {
input := &pb.CompressedTimezones{}
Expand Down
7 changes: 7 additions & 0 deletions f.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package tzf

type F interface {
GetTimezoneName(lng float64, lat float64) string
GetTimezoneNames(lng float64, lat float64) ([]string, error)
TimezoneNames() []string
}
6 changes: 3 additions & 3 deletions tzf.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ 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
}
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)
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions tzf_default_finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tzf_default_finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

var (
defaultFinder *tzf.DefaultFinder
defaultFinder tzf.F
)

func init() {
Expand Down
2 changes: 1 addition & 1 deletion tzf_fuzzy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion tzf_fuzzy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

var (
fuzzyFinder *tzf.FuzzyFinder
fuzzyFinder tzf.F
)

func init() {
Expand Down
10 changes: 5 additions & 5 deletions tzf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
)

var (
finder *tzf.Finder
fullFinder *tzf.Finder
finder tzf.F
fullFinder tzf.F
)

func init() {
Expand Down Expand Up @@ -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 <nil>
}

Expand All @@ -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 <nil>
}
Expand All @@ -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 {
Expand Down

0 comments on commit a1ffcb2

Please sign in to comment.