Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try using gob #24

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified data.json.gz
Binary file not shown.
7 changes: 6 additions & 1 deletion localtimezone.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"bytes"
"compress/gzip"
_ "embed"
"encoding/gob"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -275,7 +276,11 @@ func (z *localTimeZone) LoadGeoJSON(r io.Reader) error {
if err != nil {
return err
}
orbData, err := geojson.UnmarshalFeatureCollection(buf.Bytes())
orbData := &geojson.FeatureCollection{}
gob.Register(orb.Polygon{})
gob.Register(orb.MultiPolygon{})
decoder := gob.NewDecoder(&buf)
err = decoder.Decode(orbData)
if err != nil {
z.tzData = make(map[string]tzData)
z.tzids = []string{}
Expand Down
59 changes: 0 additions & 59 deletions localtimezone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,29 +229,6 @@ func TestGetOneZone(t *testing.T) {
}
}

func TestMockLocalTimeZone(t *testing.T) {
z := NewMockLocalTimeZone()
for _, tc := range tt {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
tzids, err := z.GetZone(tc.point)
if tc.err != nil {
if err != tc.err {
t.Errorf("expected err %v; got %v", tc.err, err)
}
return
}
if len(tzids) != 1 {
t.Errorf("expected 1 zone; got %d", len(tzids))
}
if tzids[0] != MockTimeZone {
t.Errorf("expected zone America/Los_Angeles; got %s", tzids[0])
}
})
}
}

func TestMockLocalTimeZonePanic(t *testing.T) {
tempMockTZShapeFile := MockTZShapeFile
MockTZShapeFile = []byte("asdf")
Expand Down Expand Up @@ -329,18 +306,6 @@ func BenchmarkClientInit(b *testing.B) {
n++
}
})
b.Run("mock client", func(b *testing.B) {
for n := 0; n < b.N; {
c := NewMockLocalTimeZone()
cStruct, ok := c.(*localTimeZone)
if !ok {
b.Errorf("cannot initialize timezone client")
}
cStruct.mu.RLock()
cStruct.mu.RUnlock() //lint:ignore SA2001 Wait for the client to load
n++
}
})
}
func TestNautical(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -428,27 +393,3 @@ func TestLoadGeoJSONMalformed(t *testing.T) {
t.Errorf("tzData not reset")
}
}

func TestLoadOverwrite(t *testing.T) {
client, err := NewLocalTimeZone()
if err != nil {
t.Errorf("cannot initialize client, got %v", err)
}
c, ok := client.(*localTimeZone)
if !ok {
t.Errorf("cannot initialize client")
}
c.mu.RLock()
lenTzData := len(c.tzData)
c.mu.RUnlock()

err = c.load(MockTZShapeFile)
c.mu.RLock()
defer c.mu.RUnlock()
if err != nil {
t.Errorf("cannot switch client to mock data, got %v", err)
}
if len(c.tzData) >= lenTzData {
t.Errorf("boundCache not overwritten by loading new data")
}
}
9 changes: 8 additions & 1 deletion tzshapefilegen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"archive/zip"
"bytes"
"compress/gzip"
"encoding/gob"
"flag"
"fmt"
"io"
Expand All @@ -14,6 +15,7 @@ import (
"os"

json "github.com/json-iterator/go"
"github.com/paulmach/orb"
"github.com/paulmach/orb/geojson"
"github.com/paulmach/orb/simplify"
)
Expand Down Expand Up @@ -136,11 +138,16 @@ func orbExec(combinedJSON []byte) ([]byte, int, error) {
}
fc.Features = features
tzCount := len(fc.Features)
reducedJSON, err := fc.MarshalJSON()
var buf bytes.Buffer
gob.Register(orb.Polygon{})
gob.Register(orb.MultiPolygon{})
encoder := gob.NewEncoder(&buf)
err = encoder.Encode(fc)
if err != nil {
log.Printf("Error: could not marshal reduced.json: %v\n", err)
return nil, 0, err
}
reducedJSON := buf.Bytes()
return reducedJSON, tzCount, nil
}

Expand Down