From 87fbe80853e402e3429ff1e8d175fdbc35ece98b Mon Sep 17 00:00:00 2001 From: Albert Wolszon Date: Wed, 3 Apr 2024 15:53:24 +0200 Subject: [PATCH 1/7] Move converter testdata to proper directory --- .../{ => poe2arb}/testdata/25-retains-placeholders-order.golden | 0 .../{ => poe2arb}/testdata/25-retains-placeholders-order.input | 0 convert/{ => poe2arb}/testdata/attributes-req-attrs.golden | 0 convert/{ => poe2arb}/testdata/attributes-req-attrs.input | 0 convert/{ => poe2arb}/testdata/attributes.golden | 0 convert/{ => poe2arb}/testdata/attributes.input | 0 .../testdata/empty-and-null-translations-no-template.golden | 0 .../testdata/empty-and-null-translations-no-template.input | 0 convert/{ => poe2arb}/testdata/filter-noprefix.golden | 0 convert/{ => poe2arb}/testdata/filter-noprefix.input | 0 convert/{ => poe2arb}/testdata/filter-prefix.golden | 0 convert/{ => poe2arb}/testdata/filter-prefix.input | 0 .../{ => poe2arb}/testdata/named-placeholder-no-template.golden | 0 .../{ => poe2arb}/testdata/named-placeholder-no-template.input | 0 convert/{ => poe2arb}/testdata/named-placeholder.golden | 0 convert/{ => poe2arb}/testdata/named-placeholder.input | 0 convert/{ => poe2arb}/testdata/null-definition.golden | 0 convert/{ => poe2arb}/testdata/null-definition.input | 0 convert/{ => poe2arb}/testdata/only-text-no-template.golden | 0 convert/{ => poe2arb}/testdata/only-text-no-template.input | 0 convert/{ => poe2arb}/testdata/only-text.golden | 0 convert/{ => poe2arb}/testdata/only-text.input | 0 convert/{ => poe2arb}/testdata/positional-placeholder.golden | 0 convert/{ => poe2arb}/testdata/positional-placeholder.input | 0 convert/{ => poe2arb}/testdata/text-with-double-quotes.golden | 0 convert/{ => poe2arb}/testdata/text-with-double-quotes.input | 0 .../{ => poe2arb}/testdata/two-distinct-named-placeholders.golden | 0 .../{ => poe2arb}/testdata/two-distinct-named-placeholders.input | 0 convert/{ => poe2arb}/testdata/two-same-named-placeholders.golden | 0 convert/{ => poe2arb}/testdata/two-same-named-placeholders.input | 0 30 files changed, 0 insertions(+), 0 deletions(-) rename convert/{ => poe2arb}/testdata/25-retains-placeholders-order.golden (100%) rename convert/{ => poe2arb}/testdata/25-retains-placeholders-order.input (100%) rename convert/{ => poe2arb}/testdata/attributes-req-attrs.golden (100%) rename convert/{ => poe2arb}/testdata/attributes-req-attrs.input (100%) rename convert/{ => poe2arb}/testdata/attributes.golden (100%) rename convert/{ => poe2arb}/testdata/attributes.input (100%) rename convert/{ => poe2arb}/testdata/empty-and-null-translations-no-template.golden (100%) rename convert/{ => poe2arb}/testdata/empty-and-null-translations-no-template.input (100%) rename convert/{ => poe2arb}/testdata/filter-noprefix.golden (100%) rename convert/{ => poe2arb}/testdata/filter-noprefix.input (100%) rename convert/{ => poe2arb}/testdata/filter-prefix.golden (100%) rename convert/{ => poe2arb}/testdata/filter-prefix.input (100%) rename convert/{ => poe2arb}/testdata/named-placeholder-no-template.golden (100%) rename convert/{ => poe2arb}/testdata/named-placeholder-no-template.input (100%) rename convert/{ => poe2arb}/testdata/named-placeholder.golden (100%) rename convert/{ => poe2arb}/testdata/named-placeholder.input (100%) rename convert/{ => poe2arb}/testdata/null-definition.golden (100%) rename convert/{ => poe2arb}/testdata/null-definition.input (100%) rename convert/{ => poe2arb}/testdata/only-text-no-template.golden (100%) rename convert/{ => poe2arb}/testdata/only-text-no-template.input (100%) rename convert/{ => poe2arb}/testdata/only-text.golden (100%) rename convert/{ => poe2arb}/testdata/only-text.input (100%) rename convert/{ => poe2arb}/testdata/positional-placeholder.golden (100%) rename convert/{ => poe2arb}/testdata/positional-placeholder.input (100%) rename convert/{ => poe2arb}/testdata/text-with-double-quotes.golden (100%) rename convert/{ => poe2arb}/testdata/text-with-double-quotes.input (100%) rename convert/{ => poe2arb}/testdata/two-distinct-named-placeholders.golden (100%) rename convert/{ => poe2arb}/testdata/two-distinct-named-placeholders.input (100%) rename convert/{ => poe2arb}/testdata/two-same-named-placeholders.golden (100%) rename convert/{ => poe2arb}/testdata/two-same-named-placeholders.input (100%) diff --git a/convert/testdata/25-retains-placeholders-order.golden b/convert/poe2arb/testdata/25-retains-placeholders-order.golden similarity index 100% rename from convert/testdata/25-retains-placeholders-order.golden rename to convert/poe2arb/testdata/25-retains-placeholders-order.golden diff --git a/convert/testdata/25-retains-placeholders-order.input b/convert/poe2arb/testdata/25-retains-placeholders-order.input similarity index 100% rename from convert/testdata/25-retains-placeholders-order.input rename to convert/poe2arb/testdata/25-retains-placeholders-order.input diff --git a/convert/testdata/attributes-req-attrs.golden b/convert/poe2arb/testdata/attributes-req-attrs.golden similarity index 100% rename from convert/testdata/attributes-req-attrs.golden rename to convert/poe2arb/testdata/attributes-req-attrs.golden diff --git a/convert/testdata/attributes-req-attrs.input b/convert/poe2arb/testdata/attributes-req-attrs.input similarity index 100% rename from convert/testdata/attributes-req-attrs.input rename to convert/poe2arb/testdata/attributes-req-attrs.input diff --git a/convert/testdata/attributes.golden b/convert/poe2arb/testdata/attributes.golden similarity index 100% rename from convert/testdata/attributes.golden rename to convert/poe2arb/testdata/attributes.golden diff --git a/convert/testdata/attributes.input b/convert/poe2arb/testdata/attributes.input similarity index 100% rename from convert/testdata/attributes.input rename to convert/poe2arb/testdata/attributes.input diff --git a/convert/testdata/empty-and-null-translations-no-template.golden b/convert/poe2arb/testdata/empty-and-null-translations-no-template.golden similarity index 100% rename from convert/testdata/empty-and-null-translations-no-template.golden rename to convert/poe2arb/testdata/empty-and-null-translations-no-template.golden diff --git a/convert/testdata/empty-and-null-translations-no-template.input b/convert/poe2arb/testdata/empty-and-null-translations-no-template.input similarity index 100% rename from convert/testdata/empty-and-null-translations-no-template.input rename to convert/poe2arb/testdata/empty-and-null-translations-no-template.input diff --git a/convert/testdata/filter-noprefix.golden b/convert/poe2arb/testdata/filter-noprefix.golden similarity index 100% rename from convert/testdata/filter-noprefix.golden rename to convert/poe2arb/testdata/filter-noprefix.golden diff --git a/convert/testdata/filter-noprefix.input b/convert/poe2arb/testdata/filter-noprefix.input similarity index 100% rename from convert/testdata/filter-noprefix.input rename to convert/poe2arb/testdata/filter-noprefix.input diff --git a/convert/testdata/filter-prefix.golden b/convert/poe2arb/testdata/filter-prefix.golden similarity index 100% rename from convert/testdata/filter-prefix.golden rename to convert/poe2arb/testdata/filter-prefix.golden diff --git a/convert/testdata/filter-prefix.input b/convert/poe2arb/testdata/filter-prefix.input similarity index 100% rename from convert/testdata/filter-prefix.input rename to convert/poe2arb/testdata/filter-prefix.input diff --git a/convert/testdata/named-placeholder-no-template.golden b/convert/poe2arb/testdata/named-placeholder-no-template.golden similarity index 100% rename from convert/testdata/named-placeholder-no-template.golden rename to convert/poe2arb/testdata/named-placeholder-no-template.golden diff --git a/convert/testdata/named-placeholder-no-template.input b/convert/poe2arb/testdata/named-placeholder-no-template.input similarity index 100% rename from convert/testdata/named-placeholder-no-template.input rename to convert/poe2arb/testdata/named-placeholder-no-template.input diff --git a/convert/testdata/named-placeholder.golden b/convert/poe2arb/testdata/named-placeholder.golden similarity index 100% rename from convert/testdata/named-placeholder.golden rename to convert/poe2arb/testdata/named-placeholder.golden diff --git a/convert/testdata/named-placeholder.input b/convert/poe2arb/testdata/named-placeholder.input similarity index 100% rename from convert/testdata/named-placeholder.input rename to convert/poe2arb/testdata/named-placeholder.input diff --git a/convert/testdata/null-definition.golden b/convert/poe2arb/testdata/null-definition.golden similarity index 100% rename from convert/testdata/null-definition.golden rename to convert/poe2arb/testdata/null-definition.golden diff --git a/convert/testdata/null-definition.input b/convert/poe2arb/testdata/null-definition.input similarity index 100% rename from convert/testdata/null-definition.input rename to convert/poe2arb/testdata/null-definition.input diff --git a/convert/testdata/only-text-no-template.golden b/convert/poe2arb/testdata/only-text-no-template.golden similarity index 100% rename from convert/testdata/only-text-no-template.golden rename to convert/poe2arb/testdata/only-text-no-template.golden diff --git a/convert/testdata/only-text-no-template.input b/convert/poe2arb/testdata/only-text-no-template.input similarity index 100% rename from convert/testdata/only-text-no-template.input rename to convert/poe2arb/testdata/only-text-no-template.input diff --git a/convert/testdata/only-text.golden b/convert/poe2arb/testdata/only-text.golden similarity index 100% rename from convert/testdata/only-text.golden rename to convert/poe2arb/testdata/only-text.golden diff --git a/convert/testdata/only-text.input b/convert/poe2arb/testdata/only-text.input similarity index 100% rename from convert/testdata/only-text.input rename to convert/poe2arb/testdata/only-text.input diff --git a/convert/testdata/positional-placeholder.golden b/convert/poe2arb/testdata/positional-placeholder.golden similarity index 100% rename from convert/testdata/positional-placeholder.golden rename to convert/poe2arb/testdata/positional-placeholder.golden diff --git a/convert/testdata/positional-placeholder.input b/convert/poe2arb/testdata/positional-placeholder.input similarity index 100% rename from convert/testdata/positional-placeholder.input rename to convert/poe2arb/testdata/positional-placeholder.input diff --git a/convert/testdata/text-with-double-quotes.golden b/convert/poe2arb/testdata/text-with-double-quotes.golden similarity index 100% rename from convert/testdata/text-with-double-quotes.golden rename to convert/poe2arb/testdata/text-with-double-quotes.golden diff --git a/convert/testdata/text-with-double-quotes.input b/convert/poe2arb/testdata/text-with-double-quotes.input similarity index 100% rename from convert/testdata/text-with-double-quotes.input rename to convert/poe2arb/testdata/text-with-double-quotes.input diff --git a/convert/testdata/two-distinct-named-placeholders.golden b/convert/poe2arb/testdata/two-distinct-named-placeholders.golden similarity index 100% rename from convert/testdata/two-distinct-named-placeholders.golden rename to convert/poe2arb/testdata/two-distinct-named-placeholders.golden diff --git a/convert/testdata/two-distinct-named-placeholders.input b/convert/poe2arb/testdata/two-distinct-named-placeholders.input similarity index 100% rename from convert/testdata/two-distinct-named-placeholders.input rename to convert/poe2arb/testdata/two-distinct-named-placeholders.input diff --git a/convert/testdata/two-same-named-placeholders.golden b/convert/poe2arb/testdata/two-same-named-placeholders.golden similarity index 100% rename from convert/testdata/two-same-named-placeholders.golden rename to convert/poe2arb/testdata/two-same-named-placeholders.golden diff --git a/convert/testdata/two-same-named-placeholders.input b/convert/poe2arb/testdata/two-same-named-placeholders.input similarity index 100% rename from convert/testdata/two-same-named-placeholders.input rename to convert/poe2arb/testdata/two-same-named-placeholders.input From 88b1be6362da125eecfbf0bc712786d916836ab4 Mon Sep 17 00:00:00 2001 From: Albert Wolszon Date: Wed, 3 Apr 2024 15:53:46 +0200 Subject: [PATCH 2/7] Remove unused param from convert test --- convert/poe2arb/converter_test.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/convert/poe2arb/converter_test.go b/convert/poe2arb/converter_test.go index 96cb06d..e106e58 100644 --- a/convert/poe2arb/converter_test.go +++ b/convert/poe2arb/converter_test.go @@ -44,7 +44,7 @@ func TestConverterConvert(t *testing.T) { expect := string(golden) // Actual test - actual, err := convert(t, string(source), template, requireResourceAttributes, termPrefix) + actual, err := convert(string(source), template, requireResourceAttributes, termPrefix) assert.NoError(t, err) assert.Equal(t, expect, actual) @@ -70,7 +70,7 @@ func TestConverterConvert(t *testing.T) { ` t.Run("issue 41 template", func(t *testing.T) { - actual, err := convert(t, issue41Source, true, false, "") + actual, err := convert(issue41Source, true, false, "") assert.Error(t, err) assert.EqualError(t, err, `decoding term "testPlural" failed: missing "other" plural category`) @@ -78,7 +78,7 @@ func TestConverterConvert(t *testing.T) { }) t.Run("issue 41 non-template", func(t *testing.T) { - actual, err := convert(t, issue41Source, false, false, "") + actual, err := convert(issue41Source, false, false, "") assert.NoError(t, err) assert.Equal(t, "{\n \"@@locale\": \"en\"\n}\n", actual) @@ -94,7 +94,6 @@ func flutterMustParseLocale(lang string) flutter.Locale { } func convert( - t *testing.T, input string, template bool, requireResourceAttributes bool, From e306c9678fffec8e0aba5a9f0ce28a8841f752e0 Mon Sep 17 00:00:00 2001 From: Albert Wolszon Date: Wed, 3 Apr 2024 15:54:24 +0200 Subject: [PATCH 3/7] Add check for converter test files To prevent the same issue in the future. --- convert/poe2arb/converter_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/convert/poe2arb/converter_test.go b/convert/poe2arb/converter_test.go index e106e58..e7de215 100644 --- a/convert/poe2arb/converter_test.go +++ b/convert/poe2arb/converter_test.go @@ -17,6 +17,9 @@ func TestConverterConvert(t *testing.T) { if err != nil { t.Fatal(err) } + if len(paths) == 0 { + t.Fatal("no test files found") + } for _, path := range paths { _, filename := filepath.Split(path) From 7faca3d38c305ab2ed211e681ddf9d754d3c50af Mon Sep 17 00:00:00 2001 From: Albert Wolszon Date: Wed, 3 Apr 2024 15:58:13 +0200 Subject: [PATCH 4/7] Sort ARB keys alphabetically --- convert/poe2arb/converter.go | 10 ++++++++++ convert/poe2arb/testdata/attributes-req-attrs.golden | 6 +++--- convert/poe2arb/testdata/attributes.golden | 4 ++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/convert/poe2arb/converter.go b/convert/poe2arb/converter.go index 8accdda..1f8a28f 100644 --- a/convert/poe2arb/converter.go +++ b/convert/poe2arb/converter.go @@ -7,6 +7,7 @@ import ( "fmt" "io" "regexp" + "sort" "strings" "github.com/leancodepl/poe2arb/convert" @@ -57,6 +58,15 @@ func (c *Converter) Convert(output io.Writer) error { prefixedRegexp := regexp.MustCompile("(?:([a-zA-Z]+):)?(.*)") var errs []error + // Sort terms by key alphabetically + sort.SliceStable(jsonContents, func(i, j int) bool { + a, b := jsonContents[i], jsonContents[j] + aKey := prefixedRegexp.FindStringSubmatch(a.Term)[2] + bKey := prefixedRegexp.FindStringSubmatch(b.Term)[2] + + return aKey < bKey + }) + for _, term := range jsonContents { // Filter by term prefix matches := prefixedRegexp.FindStringSubmatch(term.Term) diff --git a/convert/poe2arb/testdata/attributes-req-attrs.golden b/convert/poe2arb/testdata/attributes-req-attrs.golden index 6f57654..ee42c36 100644 --- a/convert/poe2arb/testdata/attributes-req-attrs.golden +++ b/convert/poe2arb/testdata/attributes-req-attrs.golden @@ -1,7 +1,5 @@ { "@@locale": "en", - "noAttrs": "Foobar", - "@noAttrs": {}, "attrs": "Foo {bar}", "@attrs": { "placeholders": { @@ -9,5 +7,7 @@ "type": "String" } } - } + }, + "noAttrs": "Foobar", + "@noAttrs": {} } diff --git a/convert/poe2arb/testdata/attributes.golden b/convert/poe2arb/testdata/attributes.golden index 5b849e6..6d4ae6e 100644 --- a/convert/poe2arb/testdata/attributes.golden +++ b/convert/poe2arb/testdata/attributes.golden @@ -1,6 +1,5 @@ { "@@locale": "en", - "noAttrs": "Foobar", "attrs": "Foo {bar}", "@attrs": { "placeholders": { @@ -8,5 +7,6 @@ "type": "String" } } - } + }, + "noAttrs": "Foobar" } From afad73796a2e3a868ba6a65eb998e3843b298459 Mon Sep 17 00:00:00 2001 From: Albert Wolszon Date: Wed, 3 Apr 2024 16:12:30 +0200 Subject: [PATCH 5/7] Bump year in LICENSE --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 85a0e0e..8279aed 100644 --- a/LICENSE +++ b/LICENSE @@ -186,7 +186,7 @@ same "printed page" as the copyright notice for easier identification within third-party archives. - Copyright 2023 LeanCode Sp. z o.o. + Copyright 2024 LeanCode Sp. z o.o. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. From 27c44a3d6aaeb0713fa59c1bd0a6573abadc8b2b Mon Sep 17 00:00:00 2001 From: Albert Wolszon Date: Wed, 3 Apr 2024 16:38:37 +0200 Subject: [PATCH 6/7] Use slices.SortStableFunc instead of sort.SliceStable --- convert/poe2arb/converter.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/convert/poe2arb/converter.go b/convert/poe2arb/converter.go index 1f8a28f..0a86716 100644 --- a/convert/poe2arb/converter.go +++ b/convert/poe2arb/converter.go @@ -2,12 +2,13 @@ package poe2arb import ( + "cmp" "encoding/json" "errors" "fmt" "io" "regexp" - "sort" + "slices" "strings" "github.com/leancodepl/poe2arb/convert" @@ -59,12 +60,10 @@ func (c *Converter) Convert(output io.Writer) error { var errs []error // Sort terms by key alphabetically - sort.SliceStable(jsonContents, func(i, j int) bool { - a, b := jsonContents[i], jsonContents[j] + slices.SortStableFunc(jsonContents, func(a, b *convert.POETerm) int { aKey := prefixedRegexp.FindStringSubmatch(a.Term)[2] bKey := prefixedRegexp.FindStringSubmatch(b.Term)[2] - - return aKey < bKey + return cmp.Compare(aKey, bKey) }) for _, term := range jsonContents { From d39f10bdd38113e0bc8f1d0d171aa85d516fab59 Mon Sep 17 00:00:00 2001 From: Albert Wolszon Date: Wed, 3 Apr 2024 16:51:41 +0200 Subject: [PATCH 7/7] Use natural sort order for ARB keys --- convert/poe2arb/converter.go | 11 +++++++++-- go.mod | 1 + go.sum | 2 ++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/convert/poe2arb/converter.go b/convert/poe2arb/converter.go index 0a86716..d3937a1 100644 --- a/convert/poe2arb/converter.go +++ b/convert/poe2arb/converter.go @@ -2,7 +2,6 @@ package poe2arb import ( - "cmp" "encoding/json" "errors" "fmt" @@ -11,6 +10,7 @@ import ( "slices" "strings" + "facette.io/natsort" "github.com/leancodepl/poe2arb/convert" "github.com/leancodepl/poe2arb/flutter" orderedmap "github.com/wk8/go-ordered-map/v2" @@ -63,7 +63,14 @@ func (c *Converter) Convert(output io.Writer) error { slices.SortStableFunc(jsonContents, func(a, b *convert.POETerm) int { aKey := prefixedRegexp.FindStringSubmatch(a.Term)[2] bKey := prefixedRegexp.FindStringSubmatch(b.Term)[2] - return cmp.Compare(aKey, bKey) + + if aKey == bKey { + return 0 + } else if natsort.Compare(aKey, bKey) { + return -1 + } else { + return 1 + } }) for _, term := range jsonContents { diff --git a/go.mod b/go.mod index bce6a7b..5d54794 100644 --- a/go.mod +++ b/go.mod @@ -21,6 +21,7 @@ require ( ) require ( + facette.io/natsort v0.0.0-20181210072756-2cd4dd1e2dcb github.com/inconshreveable/mousetrap v1.0.1 // indirect github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.8.1 diff --git a/go.sum b/go.sum index 298564a..3798500 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +facette.io/natsort v0.0.0-20181210072756-2cd4dd1e2dcb h1:1pSweJFeR3Pqx7uoelppkzeegfUBXL6I2FFAbfXw570= +facette.io/natsort v0.0.0-20181210072756-2cd4dd1e2dcb/go.mod h1:npRYmtaITVom7rcSo+pRURltHSG2r4TQM1cdqJ2dUB0= github.com/TwiN/go-color v1.4.0 h1:fNbOwOrvup5oj934UragnW0B1WKaAkkB85q19Y7h4ng= github.com/TwiN/go-color v1.4.0/go.mod h1:0QTVEPlu+AoCyTrho7bXbVkrCkVpdQr7YF7PYWEtSxM= github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk=