Skip to content

Commit

Permalink
Update to latest gocommon
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed May 8, 2024
1 parent 99708e6 commit 9612e66
Show file tree
Hide file tree
Showing 24 changed files with 78 additions and 80 deletions.
2 changes: 1 addition & 1 deletion assets/static/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewTelChannel(uuid assets.ChannelUUID, name string, address string, roles [
UUID_: uuid,
Name_: name,
Address_: address,
Schemes_: []string{urns.TelScheme},
Schemes_: []string{urns.Phone.Prefix},
Roles_: roles,
Features_: []assets.ChannelFeature{},
Country_: country,
Expand Down
9 changes: 4 additions & 5 deletions cmd/docgen/docs/editor_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,10 @@ func createContextPathListFile(outputDir string, c *completion.Completion) error
}

func createURNsType(gettext func(string) string) completion.Type {
properties := make([]*completion.Property, 0, len(urns.ValidSchemes))
for k := range urns.ValidSchemes {
name := strings.Title(k)
help := strings.ReplaceAll(gettext("{type} URN for the contact"), "{type}", name)
properties = append(properties, completion.NewProperty(k, help, "text"))
properties := make([]*completion.Property, 0, len(urns.Schemes))
for _, s := range urns.Schemes {
help := strings.ReplaceAll(gettext("{type} URN for the contact"), "{type}", s.Name)
properties = append(properties, completion.NewProperty(s.Prefix, help, "text"))
}
sort.SliceStable(properties, func(i, j int) bool { return properties[i].Key < properties[j].Key })

Expand Down
2 changes: 1 addition & 1 deletion cmd/transferairtime/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func main() {
os.Exit(1)
}

destination, err := urns.NewTelURNForCountry(args[0], "")
destination, err := urns.ParsePhone(args[0], "")

Check warning on line 38 in cmd/transferairtime/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/transferairtime/main.go#L38

Added line #L38 was not covered by tests
if err != nil {
fmt.Printf("%s isn't a valid phone number\n", args[0])
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion contactql/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ func ParseQuery(env envs.Environment, text string, resolver Resolver) (*ContactQ

// if query is a valid number, rewrite as a tel = query
if env.RedactionPolicy() != envs.RedactionPolicyURNs {
if number := utils.ParsePhoneNumber(text, string(env.DefaultCountry())); number != "" {
if number := utils.ParsePhoneNumber(text, env.DefaultCountry()); number != "" {
text = fmt.Sprintf(`tel = %s`, number)
}
}
Expand Down
2 changes: 1 addition & 1 deletion contactql/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (v *visitor) VisitImplicitCondition(ctx *gen.ImplicitConditionContext) any
} else if implicitIsPhoneNumberRegex.MatchString(value) {
value = cleanPhoneNumberRegex.ReplaceAllLiteralString(value, "")

return NewCondition(urns.TelScheme, PropertyTypeScheme, OpContains, value)
return NewCondition(urns.Phone.Prefix, PropertyTypeScheme, OpContains, value)
}

// convert to contains condition only if we have the right tokens, otherwise make equals check
Expand Down
7 changes: 3 additions & 4 deletions flows/actions/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,12 @@ func (a *otherContactsAction) resolveRecipients(run flows.Run, logEvent flows.Ev
// next up try it as a URN
urn := urns.URN(evaluatedLegacyVar)
if urn.Validate() == nil {
urn = urn.Normalize(string(run.Session().MergedEnvironment().DefaultCountry()))
urnList = append(urnList, urn)
urnList = append(urnList, urn.Normalize())
} else {
// if that fails, try to parse as phone number
parsedTel := utils.ParsePhoneNumber(evaluatedLegacyVar, string(run.Session().MergedEnvironment().DefaultCountry()))
parsedTel := utils.ParsePhoneNumber(evaluatedLegacyVar, run.Session().MergedEnvironment().DefaultCountry())
if parsedTel != "" {
urn, _ := urns.NewURNFromParts(urns.TelScheme, parsedTel, "", "")
urn, _ := urns.New(urns.Phone, parsedTel)
urnList = append(urnList, urn)
} else {
logEvent(events.NewErrorf("'%s' couldn't be resolved to a contact, group or URN", evaluatedLegacyVar))
Expand Down
6 changes: 3 additions & 3 deletions flows/actions/transfer_airtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ func (a *TransferAirtimeAction) transfer(run flows.Run, logEvent flows.EventCall
contact := run.Contact()

// fail if the contact doesn't have a tel URN
telURNs := contact.URNs().WithScheme(urns.TelScheme)
telURNs := contact.URNs().WithScheme(urns.Phone.Prefix)
if len(telURNs) == 0 {
return nil, errors.New("can't transfer airtime to contact without a tel URN")
}

// if contact's preferred channel is tel, use that as the sender
// if contact's preferred channel is a phone number, use that as the sender
var sender urns.URN
channel := contact.PreferredChannel()
if channel != nil && channel.SupportsScheme(urns.TelScheme) {
if channel != nil && channel.SupportsScheme(urns.Phone.Prefix) {
sender, _ = urns.Parse("tel:" + channel.Address())
}

Expand Down
4 changes: 2 additions & 2 deletions flows/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ func (s *ChannelAssets) GetForURN(urn *ContactURN, role assets.ChannelRole) *Cha
}

// tel is a special case because we do number based matching
if urn.URN().Scheme() == urns.TelScheme {
if urn.URN().Scheme() == urns.Phone.Prefix {
countryCode := i18n.DeriveCountryFromTel(urn.URN().Path())
candidates := make([]*Channel, 0)

for _, ch := range s.all {
// skip if not tel and not sendable
if !ch.SupportsScheme(urns.TelScheme) || !ch.HasRole(role) {
if !ch.SupportsScheme(urns.Phone.Prefix) || !ch.HasRole(role) {
continue
}
// skip if international and channel doesn't allow that
Expand Down
6 changes: 3 additions & 3 deletions flows/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func (c *Contact) Country() i18n.Country {
}

for _, u := range c.urns {
if u.urn.Scheme() == urns.TelScheme {
if u.urn.Scheme() == urns.Phone.Prefix {

Check warning on line 179 in flows/contact.go

View check run for this annotation

Codecov / codecov/patch

flows/contact.go#L179

Added line #L179 was not covered by tests
c := i18n.DeriveCountryFromTel(u.urn.Path())
if c != i18n.NilCountry {
return c
Expand Down Expand Up @@ -262,7 +262,7 @@ func (c *Contact) RemoveURN(urn urns.URN) bool {

// HasURN checks whether the contact has the given URN
func (c *Contact) HasURN(urn urns.URN) bool {
urn = urn.Normalize("")
urn = urn.Normalize()

for _, u := range c.urns {
if u.URN().Identity() == urn.Identity() {
Expand Down Expand Up @@ -433,7 +433,7 @@ func (c *Contact) UpdatePreferredChannel(channel *Channel) bool {

for _, urn := range c.urns {
// tel URNs can be re-assigned, other URN schemes are considered channel specific
if urn.URN().Scheme() == urns.TelScheme && channel.SupportsScheme(urns.TelScheme) {
if urn.URN().Scheme() == urns.Phone.Prefix && channel.SupportsScheme(urns.Phone.Prefix) {
urn.SetChannel(channel)
}

Expand Down
1 change: 0 additions & 1 deletion flows/contact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ func TestContact(t *testing.T) {
"mailto": nil,
"rocketchat": nil,
"slack": nil,
"teams": nil,
"tel": flows.NewContactURN(urns.URN("tel:+12024561111?channel=294a14d4-c998-41e5-a314-5941b97b89d7"), nil).ToXValue(env),
"telegram": nil,
"twitter": flows.NewContactURN(urns.URN("twitter:joey"), nil).ToXValue(env),
Expand Down
11 changes: 6 additions & 5 deletions flows/definition/legacy/expressions/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ type mapping struct {
var mappings []mapping

func init() {
schemes := make([]string, 0, len(urns.ValidSchemes))
for s := range urns.ValidSchemes {
schemes = append(schemes, s)
schemes := make([]string, len(urns.Schemes))
for i, s := range urns.Schemes {
schemes[i] = s.Prefix
}

schemesRe := strings.Join(schemes, `|`)
Expand Down Expand Up @@ -103,8 +103,9 @@ func MigrateContextReference(path string, rawDates bool) string {
var numericLookupRegex = regexp.MustCompile(`\.\d+\w*`)

// fixes property lookups
// .1 => ["1"]
// .1foo => ["1foo"]
//
// .1 => ["1"]
// .1foo => ["1foo"]
func fixLookups(path string) string {
return numericLookupRegex.ReplaceAllStringFunc(path, func(lookup string) string {
return `["` + lookup[1:] + `"]`
Expand Down
4 changes: 1 addition & 3 deletions flows/engine/testdata/templates.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
},
{
"template": "@urns",
"output": "{discord: , ext: , facebook: , fcm: , freshchat: , instagram: , jiochat: , line: , mailto: mailto:[email protected], rocketchat: , slack: , teams: , tel: tel:+12024561111, telegram: , twitter: , twitterid: twitterid:54784326227#nyaruka, viber: , vk: , webchat: , wechat: , whatsapp: }"
"output": "{discord: , ext: , facebook: , fcm: , freshchat: , instagram: , jiochat: , line: , mailto: mailto:[email protected], rocketchat: , slack: , tel: tel:+12024561111, telegram: , twitter: , twitterid: twitterid:54784326227#nyaruka, viber: , vk: , webchat: , wechat: , whatsapp: }"
},
{
"template": "@urns.tel",
Expand Down Expand Up @@ -813,7 +813,6 @@
"mailto": "mailto:[email protected]",
"rocketchat": null,
"slack": null,
"teams": null,
"tel": "tel:+12024561111",
"telegram": null,
"twitter": null,
Expand Down Expand Up @@ -910,7 +909,6 @@
"mailto": null,
"rocketchat": null,
"slack": null,
"teams": null,
"tel": "tel:+12024562222",
"telegram": null,
"twitter": null,
Expand Down
3 changes: 1 addition & 2 deletions flows/modifiers/urns.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ func (m *URNsModifier) Apply(eng flows.Engine, env envs.Environment, sa flows.Se
}

for _, urn := range m.URNs {
// normalize the URN
urn := urn.Normalize(string(env.DefaultCountry()))
urn := urn.Normalize()

if err := urn.Validate(); err != nil {
log(events.NewErrorf("'%s' is not valid URN", urn))
Expand Down
2 changes: 1 addition & 1 deletion flows/resumes/testdata/_assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
"type": "switch",
"wait": {
"type": "dial",
"phone": "(206)5551212"
"phone": "1(206)5551212"
},
"result_name": "Redirect",
"categories": [
Expand Down
2 changes: 1 addition & 1 deletion flows/resumes/testdata/dial.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"flow_uuid": "0af51032-1caa-40fe-9907-ab7c6af2bf01",
"wait": {
"type": "dial",
"phone": "(206)5551212"
"phone": "1(206)5551212"
},
"resume": {
"type": "dial",
Expand Down
3 changes: 2 additions & 1 deletion flows/routers/cases/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"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/functions"
Expand Down Expand Up @@ -542,7 +543,7 @@ func HasPhone(env envs.Environment, text *types.XText, args ...types.XValue) typ
}

// try to find a phone number
numbers := utils.FindPhoneNumbers(text.Native(), country.Native())
numbers := utils.FindPhoneNumbers(text.Native(), i18n.Country(country.Native()))
if len(numbers) == 0 {
return FalseResult
}
Expand Down
2 changes: 1 addition & 1 deletion flows/routers/cases/tests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ func TestHasPhone(t *testing.T) {
{"0954 1053 7962", "ID", "+6295410537962"}, // Invalid but possible Indonesian number
{"0811-1005-611", "ID", "+628111005611"}, // Valid with 11 digits
{"10000", "US", ""},
{"12067799294", "BW", ""},
{"12067799294", "BW", "+12067799294"},
{"oui", "CD", ""},
}

Expand Down
3 changes: 2 additions & 1 deletion flows/routers/waits/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ func (w *DialWait) AllowedFlowTypes() []flows.FlowType {
// Begin beings waiting at this wait
func (w *DialWait) Begin(run flows.Run, log flows.EventCallback) bool {
phone, _ := run.EvaluateTemplate(w.phone, log)
country := run.Session().MergedEnvironment().DefaultCountry()

urn, err := urns.NewTelURNForCountry(phone, string(run.Session().MergedEnvironment().DefaultCountry()))
urn, err := urns.ParsePhone(phone, country)
if err != nil {
log(events.NewError(err))
return false
Expand Down
4 changes: 2 additions & 2 deletions flows/runs/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func TestRunContext(t *testing.T) {
},
{
`@(json(urns))`,
`{"discord":null,"ext":null,"facebook":null,"fcm":null,"freshchat":null,"instagram":null,"jiochat":null,"line":null,"mailto":"mailto:[email protected]","rocketchat":null,"slack":null,"teams":null,"tel":"tel:+12024561111","telegram":null,"twitter":null,"twitterid":"twitterid:54784326227#nyaruka","viber":null,"vk":null,"webchat":null,"wechat":null,"whatsapp":null}`,
`{"discord":null,"ext":null,"facebook":null,"fcm":null,"freshchat":null,"instagram":null,"jiochat":null,"line":null,"mailto":"mailto:[email protected]","rocketchat":null,"slack":null,"tel":"tel:+12024561111","telegram":null,"twitter":null,"twitterid":"twitterid:54784326227#nyaruka","viber":null,"vk":null,"webchat":null,"wechat":null,"whatsapp":null}`,
},
{
`@(json(results.favorite_color))`,
Expand All @@ -215,7 +215,7 @@ func TestRunContext(t *testing.T) {
},
{
`@(json(parent.urns))`,
`{"discord":null,"ext":null,"facebook":null,"fcm":null,"freshchat":null,"instagram":null,"jiochat":null,"line":null,"mailto":null,"rocketchat":null,"slack":null,"teams":null,"tel":"tel:+12024562222","telegram":null,"twitter":null,"twitterid":null,"viber":null,"vk":null,"webchat":null,"wechat":null,"whatsapp":null}`,
`{"discord":null,"ext":null,"facebook":null,"fcm":null,"freshchat":null,"instagram":null,"jiochat":null,"line":null,"mailto":null,"rocketchat":null,"slack":null,"tel":"tel:+12024562222","telegram":null,"twitter":null,"twitterid":null,"viber":null,"vk":null,"webchat":null,"wechat":null,"whatsapp":null}`,
},
{
`@(json(parent.fields))`,
Expand Down
10 changes: 5 additions & 5 deletions flows/urn.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (u *ContactURN) SetChannel(channel *Channel) {
parsedQuery.Del("channel")
}

urn, _ := urns.NewURNFromParts(scheme, path, parsedQuery.Encode(), display)
urn, _ := urns.NewFromParts(scheme, path, parsedQuery.Encode(), display)
u.urn = urn
}

Expand All @@ -111,7 +111,7 @@ func (u *ContactURN) withoutQuery(redact bool) urns.URN {
return urns.URN(fmt.Sprintf("%s:%s", scheme, redacted))
}

urn, _ := urns.NewURNFromParts(scheme, path, "", display)
urn, _ := urns.NewFromParts(scheme, path, "", display)

return urn
}
Expand Down Expand Up @@ -207,9 +207,9 @@ func (l URNList) MapContext(env envs.Environment) map[string]types.XValue {
}

// and add nils for all other schemes
for scheme := range urns.ValidSchemes {
if _, seen := byScheme[scheme]; !seen {
byScheme[scheme] = nil
for _, scheme := range urns.Schemes {
if _, seen := byScheme[scheme.Prefix]; !seen {
byScheme[scheme.Prefix] = nil
}
}

Expand Down
18 changes: 9 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ require (
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20221202181307-76fa05c21b12
github.com/blevesearch/segment v0.9.1
github.com/buger/jsonparser v1.1.1
github.com/go-playground/validator/v10 v10.19.0
github.com/nyaruka/gocommon v1.53.2
github.com/go-playground/validator/v10 v10.20.0
github.com/nyaruka/gocommon v1.54.6
github.com/olivere/elastic/v7 v7.0.32
github.com/pkg/errors v0.9.1
github.com/sergi/go-diff v1.3.1
github.com/shopspring/decimal v1.4.0
github.com/stretchr/testify v1.9.0
golang.org/x/exp v0.0.0-20240409090435-93d18d7e34b8
golang.org/x/net v0.24.0
golang.org/x/text v0.14.0
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842
golang.org/x/net v0.25.0
golang.org/x/text v0.15.0
)

require (
Expand All @@ -32,11 +32,11 @@ require (
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/nyaruka/null/v2 v2.0.3 // indirect
github.com/nyaruka/phonenumbers v1.3.4 // indirect
github.com/nyaruka/phonenumbers v1.3.5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/sys v0.19.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/sys v0.20.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 9612e66

Please sign in to comment.