Skip to content

Commit

Permalink
Better API
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanb committed Nov 10, 2022
1 parent 27600dc commit 719140f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
11 changes: 6 additions & 5 deletions osmshortlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ const (
// in Table 1 of RFC 2045.
var intToBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~"

func CreateOSMShortLinkFull(latitude float32, longitude float32, zoom int) (string, error) {
s, err := CreateOSMShortLinkString(latitude, longitude, zoom)
// Create a short link to OSM map with marker at he given position and zoom
func Create(latitude float32, longitude float32, zoom int) (string, error) {
s, err := Encode(latitude, longitude, zoom)
if err != nil {
return "", err
}
return BASE_SHORT_OSM_URL + s + "?m", nil
}

// createShortLinkStringgiven a location and zoom, return a short string representing it.
func CreateOSMShortLinkString(latitude float32, longitude float32, zoom int) (string, error) {
// Encode a location and zoom, return a short string representing it.
func Encode(latitude float32, longitude float32, zoom int) (string, error) {
if zoom < 0 || zoom > 20 {
return "", fmt.Errorf("invalid zoom %d", zoom)
}
Expand Down Expand Up @@ -60,7 +61,7 @@ func interleaveBits(x uint32, y uint32) uint64 {
return c
}

func DecodeShortLinkString(s string) (float64, float64, int, error) {
func Decode(s string) (float64, float64, int, error) {
if len(s) < 1 {
return 0, 0, 0, fmt.Errorf("invalid osm short link string %q", s)
}
Expand Down
10 changes: 5 additions & 5 deletions osmshortlink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func Test_CreateOSMShortLinkFull(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got, err := CreateOSMShortLinkFull(tt.args.latitude, tt.args.longitude, tt.args.zoom); got != tt.want {
if got, err := Create(tt.args.latitude, tt.args.longitude, tt.args.zoom); got != tt.want {
t.Errorf("Encode() = %v, want %v", got, tt.want)
if err != nil {
t.Errorf("Error: %v", err)
Expand Down Expand Up @@ -74,7 +74,7 @@ func Fuzz_CreateDecodeShortLink(f *testing.F) {
f.Add(float32(46.05141922831535), float32(14.506048858165741), 19)

f.Fuzz(func(t *testing.T, origLat, origLon float32, origZoom int) {
enc, encErr := CreateOSMShortLinkString(origLat, origLon, origZoom)
enc, encErr := Encode(origLat, origLon, origZoom)
if origLat >= 90 || origLat < -90 || origLon >= 180 || origLon < -180 || origZoom > 20 || origZoom < 0 {
if encErr == nil {
t.Error("missing an encode error")
Expand All @@ -85,7 +85,7 @@ func Fuzz_CreateDecodeShortLink(f *testing.F) {
// return
}

decLat, decLon, decZoom, decErr := DecodeShortLinkString(enc)
decLat, decLon, decZoom, decErr := Decode(enc)
if encErr != nil && decErr == nil {
t.Errorf("missing a deocode error after %v", encErr)
}
Expand Down Expand Up @@ -146,7 +146,7 @@ func Test_CreateOSMShortLinkString(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got, err := CreateOSMShortLinkString(tt.args.latitude, tt.args.longitude, tt.args.zoom); got != tt.want {
if got, err := Encode(tt.args.latitude, tt.args.longitude, tt.args.zoom); got != tt.want {
t.Errorf("CreateOSMShortLinkString() = %v, want %v", got, tt.want)
if err != nil {
t.Errorf("Error: %v", err)
Expand Down Expand Up @@ -183,7 +183,7 @@ func TestDecodeShortLinkString(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1, got2, err := DecodeShortLinkString(tt.args.s)
got, got1, got2, err := Decode(tt.args.s)
if err != nil {
t.Errorf("DecodeShortLinkString() got error %v", err)
}
Expand Down

0 comments on commit 719140f

Please sign in to comment.