-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #303 from nyaruka/legacy_trans
Handle legacy flows with missing translations
- Loading branch information
Showing
6 changed files
with
147 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package legacy | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/nyaruka/goflow/utils" | ||
) | ||
|
||
// Translations is an inline translation map used for localization | ||
type Translations map[utils.Language]string | ||
|
||
// Base looks up the translation in the given base language, or "base" | ||
func (t Translations) Base(baseLanguage utils.Language) string { | ||
val, exists := t[baseLanguage] | ||
if exists { | ||
return val | ||
} | ||
return t["base"] | ||
} | ||
|
||
// UnmarshalJSON unmarshals legacy translations from the given JSON | ||
func (t *Translations) UnmarshalJSON(data []byte) error { | ||
// sometimes legacy flows have a single string instead of a map | ||
if data[0] == '"' { | ||
var asString string | ||
if err := json.Unmarshal(data, &asString); err != nil { | ||
return err | ||
} | ||
*t = Translations{"base": asString} | ||
return nil | ||
} | ||
|
||
asMap := make(map[utils.Language]string) | ||
if err := json.Unmarshal(data, &asMap); err != nil { | ||
return err | ||
} | ||
|
||
*t = asMap | ||
return nil | ||
} | ||
|
||
// DecimalString represents a decimal value which may be provided as a string or floating point value | ||
type DecimalString string | ||
|
||
// UnmarshalJSON unmarshals a decimal string from the given JSON | ||
func (s *DecimalString) UnmarshalJSON(data []byte) error { | ||
if data[0] == '"' { | ||
// data is a quoted string | ||
*s = DecimalString(data[1 : len(data)-1]) | ||
} else { | ||
// data is JSON float | ||
*s = DecimalString(data) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package legacy_test | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/nyaruka/goflow/legacy" | ||
"github.com/nyaruka/goflow/utils" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestTranslations(t *testing.T) { | ||
// can unmarshall from a single string | ||
translations := make(legacy.Translations) | ||
json.Unmarshal([]byte(`"hello"`), &translations) | ||
assert.Equal(t, legacy.Translations{"base": "hello"}, translations) | ||
|
||
// or a map | ||
translations = make(legacy.Translations) | ||
json.Unmarshal([]byte(`{"eng": "hello", "fra": "bonjour"}`), &translations) | ||
assert.Equal(t, legacy.Translations{"eng": "hello", "fra": "bonjour"}, translations) | ||
|
||
// and back to JSON | ||
data, err := json.Marshal(translations) | ||
require.NoError(t, err) | ||
assert.Equal(t, []byte(`{"eng":"hello","fra":"bonjour"}`), data) | ||
|
||
translationSet := []legacy.Translations{ | ||
{"eng": "Yes", "fra": "Oui"}, | ||
{"eng": "No", "fra": "Non"}, | ||
{"eng": "Maybe"}, | ||
{"eng": "Never", "fra": "Jamas"}, | ||
} | ||
assert.Equal(t, map[utils.Language][]string{ | ||
"eng": {"Yes", "No", "Maybe", "Never"}, | ||
"fra": {"Oui", "Non", "", "Jamas"}, | ||
}, legacy.TransformTranslations(translationSet)) | ||
} | ||
|
||
func TestDecimalString(t *testing.T) { | ||
// can unmarshall from a string | ||
var decimal legacy.DecimalString | ||
json.Unmarshal([]byte(`"123.45"`), &decimal) | ||
assert.Equal(t, legacy.DecimalString("123.45"), decimal) | ||
|
||
// or a floating point (JSON number type) | ||
json.Unmarshal([]byte(`567.89`), &decimal) | ||
assert.Equal(t, legacy.DecimalString("567.89"), decimal) | ||
} |