Skip to content

Commit

Permalink
Merge pull request #1182 from nyaruka/latest_gocommon
Browse files Browse the repository at this point in the history
Update to latest gocommon
  • Loading branch information
rowanseymour authored Sep 4, 2023
2 parents 0e84169 + 642969a commit 295fd8c
Show file tree
Hide file tree
Showing 20 changed files with 35 additions and 48 deletions.
3 changes: 1 addition & 2 deletions cmd/flowrunner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ func RunFlow(eng flows.Engine, assetsPath string, flowUUID assets.FlowUUID, init

// create our environment
la, _ := time.LoadLocation("America/Los_Angeles")
languages := []i18n.Language{flow.Language(), contact.Language()}
env := envs.NewBuilder().WithTimezone(la).WithAllowedLanguages(languages).Build()
env := envs.NewBuilder().WithTimezone(la).WithAllowedLanguages(flow.Language(), contact.Language()).Build()

repro := &Repro{}

Expand Down
22 changes: 1 addition & 21 deletions envs/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func (b *EnvironmentBuilder) WithTimezone(timezone *time.Location) *EnvironmentB
return b
}

func (b *EnvironmentBuilder) WithAllowedLanguages(allowedLanguages []i18n.Language) *EnvironmentBuilder {
func (b *EnvironmentBuilder) WithAllowedLanguages(allowedLanguages ...i18n.Language) *EnvironmentBuilder {
b.env.allowedLanguages = allowedLanguages
return b
}
Expand All @@ -233,23 +233,3 @@ func (b *EnvironmentBuilder) WithRedactionPolicy(redactionPolicy RedactionPolicy

// Build returns the final environment
func (b *EnvironmentBuilder) Build() Environment { return b.env }

// deprecated - can remove when gocommon's date formatting takes an i18n.Locale
func ToBCP47(l i18n.Locale) string {
if l == i18n.NilLocale {
return ""
}

lang, country := l.Split()
lang2 := lang.ISO639_1()

// not all languages have a 2-letter code
if lang2 == "" {
return ""
}

if country != i18n.NilCountry {
lang2 += "-" + string(country)
}
return lang2
}
2 changes: 1 addition & 1 deletion envs/environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func TestEnvironmentBuilder(t *testing.T) {
WithDateFormat(envs.DateFormatDayMonthYear).
WithTimeFormat(envs.TimeFormatHourMinuteSecond).
WithTimezone(kgl).
WithAllowedLanguages([]i18n.Language{i18n.Language("fra"), i18n.Language("eng")}).
WithAllowedLanguages("fra", "eng").
WithDefaultCountry(i18n.Country("RW")).
WithNumberFormat(&envs.NumberFormat{DecimalSymbol: "'"}).
WithRedactionPolicy(envs.RedactionPolicyURNs).
Expand Down
2 changes: 1 addition & 1 deletion excellent/types/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (x XDate) Format(env envs.Environment) string {

// FormatCustom provides customised formatting
func (x XDate) FormatCustom(env envs.Environment, layout string) (string, error) {
return x.Native().Format(layout, envs.ToBCP47(env.DefaultLocale()))
return x.Native().Format(layout, env.DefaultLocale())
}

// MarshalJSON is called when a struct containing this type is marshaled
Expand Down
3 changes: 1 addition & 2 deletions excellent/types/date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"time"

"github.com/nyaruka/gocommon/dates"
"github.com/nyaruka/gocommon/i18n"
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/excellent/types"
"github.com/pkg/errors"
Expand All @@ -14,7 +13,7 @@ import (

func TestXDate(t *testing.T) {
env := envs.NewBuilder().WithDateFormat(envs.DateFormatDayMonthYear).Build()
env2 := envs.NewBuilder().WithDateFormat(envs.DateFormatYearMonthDay).WithAllowedLanguages([]i18n.Language{"spa"}).Build()
env2 := envs.NewBuilder().WithDateFormat(envs.DateFormatYearMonthDay).WithAllowedLanguages("spa").Build()

d1 := types.NewXDate(dates.NewDate(2019, 2, 20))
assert.Equal(t, `date`, d1.Describe())
Expand Down
2 changes: 1 addition & 1 deletion excellent/types/datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (x XDateTime) FormatCustom(env envs.Environment, layout string, tz *time.Lo
dt = dt.In(tz)
}

return dates.Format(dt, layout, envs.ToBCP47(env.DefaultLocale()), dates.DateTimeLayouts)
return dates.Format(dt, layout, env.DefaultLocale(), dates.DateTimeLayouts)
}

// String returns the native string representation of this type
Expand Down
3 changes: 1 addition & 2 deletions excellent/types/datetime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"time"

"github.com/nyaruka/gocommon/dates"
"github.com/nyaruka/gocommon/i18n"
"github.com/nyaruka/gocommon/jsonx"
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/excellent/types"
Expand All @@ -15,7 +14,7 @@ import (

func TestXDateTime(t *testing.T) {
env := envs.NewBuilder().WithDateFormat(envs.DateFormatDayMonthYear).Build()
env2 := envs.NewBuilder().WithDateFormat(envs.DateFormatYearMonthDay).WithAllowedLanguages([]i18n.Language{"spa"}).Build()
env2 := envs.NewBuilder().WithDateFormat(envs.DateFormatYearMonthDay).WithAllowedLanguages("spa").Build()

assert.True(t, types.NewXDateTime(time.Date(2018, 4, 9, 17, 1, 30, 123456789, time.UTC)).Truthy())

Expand Down
2 changes: 1 addition & 1 deletion excellent/types/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (x XTime) Format(env envs.Environment) string {

// FormatCustom provides customised formatting
func (x XTime) FormatCustom(env envs.Environment, layout string) (string, error) {
return x.Native().Format(layout, envs.ToBCP47(env.DefaultLocale()))
return x.Native().Format(layout, env.DefaultLocale())
}

// MarshalJSON is called when a struct containing this type is marshaled
Expand Down
2 changes: 1 addition & 1 deletion flows/actions/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func testActionType(t *testing.T, assetsJSON json.RawMessage, typeName string) {
}

envBuilder := envs.NewBuilder().
WithAllowedLanguages([]i18n.Language{"eng", "spa"}).
WithAllowedLanguages("eng", "spa").
WithDefaultCountry("RW")

if tc.RedactURNs {
Expand Down
2 changes: 1 addition & 1 deletion flows/contact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ func TestReevaluateQueryBasedGroups(t *testing.T) {

for _, tc := range tests {
envBuilder := envs.NewBuilder().
WithAllowedLanguages([]i18n.Language{"eng", "spa"}).
WithAllowedLanguages("eng", "spa").
WithDefaultCountry("RW")

if tc.RedactURNs {
Expand Down
2 changes: 1 addition & 1 deletion flows/environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func TestSessionEnvironment(t *testing.T) {
tzUK, _ := time.LoadLocation("Europe/London")

env := envs.NewBuilder().
WithAllowedLanguages([]i18n.Language{"eng", "fra", "kin"}).
WithAllowedLanguages("eng", "fra", "kin").
WithDefaultCountry("RW").
WithTimezone(tzRW).
Build()
Expand Down
2 changes: 1 addition & 1 deletion flows/msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func TestBroadcastTranslations(t *testing.T) {
baseLanguage := i18n.Language("eng")

assertTranslation := func(contactLanguage i18n.Language, allowedLanguages []i18n.Language, expectedText string, expectedLang i18n.Language) {
env := envs.NewBuilder().WithAllowedLanguages(allowedLanguages).Build()
env := envs.NewBuilder().WithAllowedLanguages(allowedLanguages...).Build()
sa, err := engine.NewSessionAssets(env, static.NewEmptySource(), nil)
require.NoError(t, err)

Expand Down
2 changes: 1 addition & 1 deletion flows/runs/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ func TestTranslation(t *testing.T) {
assetsJSON, _ := os.ReadFile("testdata/translation_assets.json")
assetsJSON = test.JSONReplace(assetsJSON, []string{"flows", "[0]", "nodes", "[0]", "actions", "[0]"}, tc.msgAction)

env := envs.NewBuilder().WithAllowedLanguages(tc.envLangs).Build()
env := envs.NewBuilder().WithAllowedLanguages(tc.envLangs...).Build()
_, _, sp := test.NewSessionBuilder().
WithEnvironment(env).
WithContact("2efa1803-ae4d-4a58-ba54-b523e53e40f3", 123, "Bob", tc.contactLang, "tel+1234567890").
Expand Down
3 changes: 1 addition & 2 deletions flows/translation/flows.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/nyaruka/gocommon/dates"
"github.com/nyaruka/gocommon/i18n"
"github.com/nyaruka/gocommon/uuids"
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/utils"
"github.com/nyaruka/goflow/utils/po"
Expand Down Expand Up @@ -196,7 +195,7 @@ func poFromExtracted(sources []flows.Flow, initialComment string, lang i18n.Lang
flowUUIDs[i] = string(f.UUID())
}

header := po.NewHeader(initialComment, dates.Now(), envs.ToBCP47(i18n.NewLocale(lang, i18n.NilCountry)))
header := po.NewHeader(initialComment, dates.Now(), i18n.NewLocale(lang, i18n.NilCountry))
header.Custom["Source-Flows"] = strings.Join(flowUUIDs, "; ")
header.Custom["Language-3"] = string(lang)
p := po.NewPO(header)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/blevesearch/segment v0.9.1
github.com/buger/jsonparser v1.1.1
github.com/go-playground/validator/v10 v10.14.1
github.com/nyaruka/gocommon v1.41.0
github.com/nyaruka/gocommon v1.41.1
github.com/olivere/elastic/v7 v7.0.32
github.com/pkg/errors v0.9.1
github.com/sergi/go-diff v1.3.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNa
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/nyaruka/gocommon v1.41.0 h1:ixojG8fiUoE1ksIOKy/opg3kMby6uptrmqLugEcX89o=
github.com/nyaruka/gocommon v1.41.0/go.mod h1:cJ2XmEX+FDOzBvE19IW+hG8EFVsSrNgCp7NrxAlP4Xg=
github.com/nyaruka/gocommon v1.41.1 h1:SpIXqLCBF3Un/AjzIiqC/DO4jU7Zt7SyDh/t9SyjIrQ=
github.com/nyaruka/gocommon v1.41.1/go.mod h1:cJ2XmEX+FDOzBvE19IW+hG8EFVsSrNgCp7NrxAlP4Xg=
github.com/nyaruka/null/v2 v2.0.3 h1:rdmMRQyVzrOF3Jff/gpU/7BDR9mQX0lcLl4yImsA3kw=
github.com/nyaruka/null/v2 v2.0.3/go.mod h1:OCVeCkCXwrg5/qE6RU0c1oUVZBy+ZDrT+xYg1XSaIWA=
github.com/nyaruka/phonenumbers v1.1.8 h1:mjFu85FeoH2Wy18aOMUvxqi1GgAqiQSJsa/cCC5yu2s=
Expand Down
2 changes: 1 addition & 1 deletion mobile/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func NewEnvironment(dateFormat string, timeFormat string, timezone string, allow
WithDateFormat(envs.DateFormat(dateFormat)).
WithTimeFormat(envs.TimeFormat(timeFormat)).
WithTimezone(tz).
WithAllowedLanguages(langs).
WithAllowedLanguages(langs...).
WithDefaultCountry(i18n.Country(defaultCountry)).
WithRedactionPolicy(envs.RedactionPolicy(redactionPolicy)).
Build(),
Expand Down
4 changes: 1 addition & 3 deletions services/classification/bothub/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ import (

"github.com/nyaruka/gocommon/dates"
"github.com/nyaruka/gocommon/httpx"
"github.com/nyaruka/gocommon/i18n"
"github.com/nyaruka/gocommon/uuids"
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/services/classification/bothub"
"github.com/nyaruka/goflow/test"

"github.com/shopspring/decimal"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -71,7 +69,7 @@ func TestService(t *testing.T) {
"f96abf2f-3b53-4766-8ea6-09a655222a02",
)

env := envs.NewBuilder().WithAllowedLanguages([]i18n.Language{"spa"}).WithDefaultCountry("US").Build()
env := envs.NewBuilder().WithAllowedLanguages("spa").WithDefaultCountry("US").Build()
httpLogger := &flows.HTTPLogger{}

classification, err := svc.Classify(env, "book my flight to Quito", httpLogger.Log)
Expand Down
2 changes: 1 addition & 1 deletion test/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ func NewSessionBuilder() *SessionBuilder {
env := envs.NewBuilder().
WithDateFormat(envs.DateFormatDayMonthYear).
WithDefaultCountry("US").
WithAllowedLanguages([]i18n.Language{"eng", "spa"}).
WithAllowedLanguages("eng", "spa").
WithInputCollation(envs.CollationConfusables).
Build()

Expand Down
17 changes: 15 additions & 2 deletions utils/po/po.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"sort"
"strings"
"time"

"github.com/nyaruka/gocommon/i18n"
)

const datetimeformat = "2006-01-02 15:04-0700"
Expand All @@ -22,11 +24,22 @@ type Header struct {
}

// NewHeader creates a new PO header with the given values
func NewHeader(initialComment string, creationDate time.Time, lang string) *Header {
func NewHeader(initialComment string, creationDate time.Time, locale i18n.Locale) *Header {
// use 2-letter lang code if it has one
language := string(locale)
lang, country := locale.Split()
lang2 := lang.ISO639_1()
if lang2 != "" {
language = lang2
if country != "" {
language += "-" + string(country)
}
}

return &Header{
InitialComment: initialComment + "\n",
POTCreationDate: creationDate,
Language: lang,
Language: language,
MIMEVersion: "1.0",
ContentType: "text/plain; charset=UTF-8",
Custom: make(map[string]string),
Expand Down

0 comments on commit 295fd8c

Please sign in to comment.