Skip to content

Commit

Permalink
Fallback received city names to latin (#25)
Browse files Browse the repository at this point in the history
This PR enables you to pass latin letters for city names that do not
contain only latin letters.
e.g.:
All the following AbbrCity calls will successfully receive the
abbreviated "Łódź" city name.
```
session.AbbrCity(context.Background(), "Łódź", language.English)
session.AbbrCity(context.Background(), "łódź", language.English)
session.AbbrCity(context.Background(), "lodz", language.English)
```
  • Loading branch information
krisukox authored Aug 12, 2023
1 parent 4f26928 commit 13d62a3
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
26 changes: 25 additions & 1 deletion flights/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
"net/http"
"net/url"
"strconv"
"strings"
"time"

anyascii "github.com/anyascii/go"
"github.com/hashicorp/go-retryablehttp"
"golang.org/x/text/language"
)
Expand Down Expand Up @@ -47,6 +49,28 @@ func abbrCitySchema(city, abbrCity *string) *[][][][]interface{} {
return &[][][][]interface{}{{{{nil, nil, city, nil, abbrCity}}}}
}

func compareStrLatin(lv, rv string) bool {
if lv == rv {
return true
}

lv = anyascii.Transliterate(lv)
rv = anyascii.Transliterate(rv)

lv = strings.ToLower(lv)
rv = strings.ToLower(rv)

if len(lv) != len(rv) {
return false
}
for i := range lv {
if lv[i] != rv[i] {
return false
}
}
return true
}

// AbbrCity serializes the city name by requesting it from the Google Flights API. The city name should
// be provided in the language described by [language.Tag].
//
Expand Down Expand Up @@ -79,7 +103,7 @@ func (s *Session) AbbrCity(ctx context.Context, city string, lang language.Tag)
return "", fmt.Errorf("AbbrCity error during decoding: %v", err)
}

if city != receivedCity {
if !compareStrLatin(city, receivedCity) {
return "", fmt.Errorf("the requested city name didn't match the found. requested: %s found: %s", city, receivedCity)
}

Expand Down
33 changes: 33 additions & 0 deletions flights/city_test.go → flights/location_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,36 @@ func TestAbbrCity(t *testing.T) {
t.Fatalf("Warsaw abbreviated city name not stored in cache")
}
}

func TestAbbrCityLatin(t *testing.T) {
session, err := New()
if err != nil {
t.Fatal(err)
}

expectedAbbrCity := "/m/0c6fz"

abbrCity, err := session.AbbrCity(context.Background(), "Łódź", language.English)
if err != nil {
t.Fatal(err)
}
if abbrCity != expectedAbbrCity {
t.Fatalf("wrong abbreviated city name, expected: %s received: %s", expectedAbbrCity, abbrCity)
}

abbrCity, err = session.AbbrCity(context.Background(), "łódź", language.English)
if err != nil {
t.Fatal(err)
}
if abbrCity != expectedAbbrCity {
t.Fatalf("wrong abbreviated city name, expected: %s received: %s", expectedAbbrCity, abbrCity)
}

abbrCity, err = session.AbbrCity(context.Background(), "lodz", language.English)
if err != nil {
t.Fatal(err)
}
if abbrCity != expectedAbbrCity {
t.Fatalf("wrong abbreviated city name, expected: %s received: %s", expectedAbbrCity, abbrCity)
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.20
require golang.org/x/text v0.11.0

require (
github.com/anyascii/go v0.3.2
github.com/go-test/deep v1.1.0
github.com/hashicorp/go-retryablehttp v0.7.4
google.golang.org/protobuf v1.31.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/anyascii/go v0.3.2 h1:87uFISteh7vwofK02srrPKtAvG6Wx7ozRjNh8uhfa7w=
github.com/anyascii/go v0.3.2/go.mod h1:HDvbMmSpqJyIe+xtSkHmAYTjc8PzvO3l1Jmgx/IFUPs=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-test/deep v1.1.0 h1:WOcxcdHcvdgThNXjw0t76K42FXTU7HpNQWHpA2HHNlg=
github.com/go-test/deep v1.1.0/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
Expand Down

0 comments on commit 13d62a3

Please sign in to comment.