Skip to content

Commit

Permalink
container refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
zelenin committed Aug 31, 2022
1 parent ed8e74c commit 6e2de7c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 26 deletions.
3 changes: 2 additions & 1 deletion cmd/datasheet-converter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"github.com/ake-persson/mapslice-json"
"github.com/new-world-tools/new-world-tools/datasheet"
"github.com/new-world-tools/new-world-tools/internal"
"github.com/new-world-tools/new-world-tools/localization"
"github.com/new-world-tools/new-world-tools/profiler"
workerpool "github.com/zelenin/go-worker-pool"
Expand All @@ -26,7 +27,7 @@ const (

var (
pool *workerpool.Pool
localizationData *localization.Localization
localizationData *internal.Store[string]
outputDir string
format string
pr *profiler.Profiler
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module github.com/new-world-tools/new-world-tools

go 1.17
go 1.19

require (
github.com/ake-persson/mapslice-json v0.0.0-20210720081907-22c8edf57807
github.com/new-world-tools/go-oodle v0.1.2
github.com/ozankasikci/go-image-merge v0.2.3-0.20220620190453-66fdff081378
github.com/zelenin/go-worker-pool v0.0.1
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ github.com/itchio/lzma v0.0.0-20190703113020-d3e24e3e3d49/go.mod h1:avNrevQMli1p
github.com/kisielk/errcheck v1.6.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/new-world-tools/go-oodle v0.1.2 h1:KDZNiiN0TnkOcuncXQtlnteZQYD2samuv6H3phq2DCA=
github.com/new-world-tools/go-oodle v0.1.2/go.mod h1:G8hCx5mlsQBT8xCYARaiIIyHgt0FI32LLpSN5/DryQM=
github.com/ozankasikci/go-image-merge v0.2.3-0.20220620190453-66fdff081378 h1:zDe456QV/YAw3UA104Z5vtcsL4VTSNTIzWII9sIL4nk=
github.com/ozankasikci/go-image-merge v0.2.3-0.20220620190453-66fdff081378/go.mod h1:NQ2aN0b21buFx3p+5x4dZrKuPSLh2uBukK7F30BrYTo=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
Expand Down
36 changes: 36 additions & 0 deletions internal/store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package internal

type Store[T any] struct {
values map[string]T
keyNormalizer func(key string) string
}

func (store *Store[T]) Add(key string, item T) {
store.values[store.keyNormalizer(key)] = item
}

func (store *Store[T]) Has(key string) bool {
_, ok := store.values[store.keyNormalizer(key)]
return ok
}

func (store *Store[T]) Get(key string) T {
val, _ := store.values[store.keyNormalizer(key)]
return val
}

func NewSimpleStore[T any]() *Store[T] {
return &Store[T]{
values: map[string]T{},
keyNormalizer: func(key string) string {
return key
},
}
}

func NewStore[T any](keyNormalizer func(key string) string) *Store[T] {
return &Store[T]{
values: map[string]T{},
keyNormalizer: keyNormalizer,
}
}
32 changes: 8 additions & 24 deletions localization/localization.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package localization
import (
"encoding/xml"
"errors"
"github.com/new-world-tools/new-world-tools/internal"
"io/fs"
"os"
"path/filepath"
Expand All @@ -14,21 +15,7 @@ var ErrNotFound = errors.New("not found")

var reXml = regexp.MustCompile(`.xml`)

type Localization struct {
values map[string]string
}

func (loc *Localization) Has(key string) bool {
_, ok := loc.values[strings.TrimPrefix(strings.ToLower(key), "@")]
return ok
}

func (loc *Localization) Get(key string) string {
val, _ := loc.values[strings.TrimPrefix(strings.ToLower(key), "@")]
return val
}

func New(root string) (*Localization, error) {
func New(root string) (*internal.Store[string], error) {
files := []string{}

err := filepath.Walk(root, func(path string, info fs.FileInfo, err error) error {
Expand All @@ -49,9 +36,9 @@ func New(root string) (*Localization, error) {
return nil, err
}

localizationData := &Localization{
values: map[string]string{},
}
localizationStore := internal.NewStore[string](func(key string) string {
return strings.TrimPrefix(strings.ToLower(key), "@")
})

for _, xmlPath := range files {
xmlFile, err := os.Open(xmlPath)
Expand All @@ -70,19 +57,16 @@ func New(root string) (*Localization, error) {
continue
}

key := strings.ToLower(resource.Key)

val, ok := localizationData.values[key]
if ok && val != resource.Value {
if localizationStore.Has(resource.Key) && localizationStore.Get(resource.Key) != resource.Value {
//return nil, fmt.Errorf("multiple values: %s = %q and %q", key, val, resource.Value)
}
//if !ok {
localizationData.values[key] = resource.Value
localizationStore.Add(resource.Key, resource.Value)
//}
}
}

return localizationData, nil
return localizationStore, nil
}

type Resources struct {
Expand Down

0 comments on commit 6e2de7c

Please sign in to comment.