From 6cfc03447905d89929bb761c0d038d13157e2209 Mon Sep 17 00:00:00 2001 From: Vinetwigs Date: Tue, 14 Jun 2022 15:42:30 +0200 Subject: [PATCH] - v1.3.0: Added MessagePack support --- CHANGELOG.md | 11 +++++ go.mod | 7 +++- go.sum | 2 + stres.go | 104 +++++++++++++++++++++++++---------------------- types/msgpack.go | 17 ++++++++ types/types.go | 36 ++++++++-------- 6 files changed, 109 insertions(+), 68 deletions(-) create mode 100644 types/msgpack.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 18214c8..f560d84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,17 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.3.0] - 2022-06-14 + +### Added + +- Support for [MessagePack file format](https://msgpack.org/) + +### Changed + +- LoadValues function now uses goroutines to load values into the internal dictionaries faster +- Reduced GetQuantityString function complexity + ## [1.2.1] - 2022-06-11 ### Fixed diff --git a/go.mod b/go.mod index dbf1d25..5e07c8b 100644 --- a/go.mod +++ b/go.mod @@ -6,4 +6,9 @@ require gopkg.in/yaml.v3 v3.0.1 require github.com/pelletier/go-toml/v2 v2.0.2 -require github.com/genkami/watson v1.0.0 +require ( + github.com/genkami/watson v1.0.0 + github.com/vmihailenco/msgpack/v5 v5.2.0 +) + +require github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect diff --git a/go.sum b/go.sum index 1444e94..eeac2c0 100644 --- a/go.sum +++ b/go.sum @@ -13,7 +13,9 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s= github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals= +github.com/vmihailenco/msgpack/v5 v5.2.0 h1:ZhIAtVUP1mme8GIlpiAnmTzjSWMexA/uNF2We85DR0w= github.com/vmihailenco/msgpack/v5 v5.2.0/go.mod h1:fEM7KuHcnm0GvDCztRpw9hV0PuoO2ciTismP6vjggcM= +github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g= github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds= github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/stres.go b/stres.go index 9514b49..b98a3f3 100644 --- a/stres.go +++ b/stres.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "os" "strings" + "sync" "github.com/Vinetwigs/stres/types" ) @@ -22,11 +23,12 @@ var plural_string_entries map[string]types.Plural = make(map[string]types.Plural var few_threshold = 20 const ( - XML types.FileType = "xml" - YAML types.FileType = "yml" - JSON types.FileType = "json" - TOML types.FileType = "toml" - WATSON types.FileType = "watson" + XML types.FileType = "xml" + YAML types.FileType = "yml" + JSON types.FileType = "json" + TOML types.FileType = "toml" + WATSON types.FileType = "watson" + MSGPACK types.FileType = "msgpack" ) var ( @@ -64,20 +66,34 @@ func LoadValues(t types.FileType) error { return err } + var wg sync.WaitGroup + wg.Add(3) + // Load strings - for i := 0; i < len(n.Strings); i++ { - string_entries[n.Strings[i].Name] = n.Strings[i].Value - } + go func() { + defer wg.Done() + for i := 0; i < len(n.Strings); i++ { + string_entries[n.Strings[i].Name] = n.Strings[i].Value + } + }() // Load string arrays - for i := 0; i < len(n.StringsArray); i++ { - string_array_entries[n.StringsArray[i].Name] = *n.StringsArray[i] - } + go func() { + defer wg.Done() + for i := 0; i < len(n.StringsArray); i++ { + string_array_entries[n.StringsArray[i].Name] = *n.StringsArray[i] + } + }() // Load quantity strings - for i := 0; i < len(n.Plurals); i++ { - plural_string_entries[n.Plurals[i].Name] = *n.Plurals[i] - } + go func() { + defer wg.Done() + for i := 0; i < len(n.Plurals); i++ { + plural_string_entries[n.Plurals[i].Name] = *n.Plurals[i] + } + }() + + wg.Wait() return nil } @@ -88,26 +104,30 @@ func LoadValues(t types.FileType) error { func SetResourceType(t types.FileType) { switch t { - case "xml": + case XML: xmlED := &types.XMLStrategy{} encDec.SetStrategy(xmlED) fileType = XML - case "json": + case JSON: jsonED := &types.JSONStrategy{} encDec.SetStrategy(jsonED) fileType = JSON - case "yml": + case YAML: yamlED := &types.YAMLStrategy{} encDec.SetStrategy(yamlED) fileType = YAML - case "toml": + case TOML: tomlED := &types.TOMLStrategy{} encDec.SetStrategy(tomlED) fileType = TOML - case "watson": + case WATSON: watsonED := &types.WatsonStrategy{} encDec.SetStrategy(watsonED) fileType = WATSON + case MSGPACK: + msgpackED := &types.MsgPackStrategy{} + encDec.SetStrategy(msgpackED) + fileType = MSGPACK default: xmlED := &types.XMLStrategy{} encDec.SetStrategy(xmlED) @@ -370,6 +390,8 @@ func GetQuantityString(name string, count int) string { return "" } + idx := -1 + val, exists := plural_string_entries[name] if !exists { @@ -377,47 +399,31 @@ func GetQuantityString(name string, count int) string { } if count == 0 { - for i := 0; i < len(val.Items); i++ { - if val.Items[i].Quantity == quantityValues[0] { - return val.Items[i].Value - } - } - return "" + idx = 0 } if count == 1 { - for i := 0; i < len(val.Items); i++ { - if val.Items[i].Quantity == quantityValues[1] { - return val.Items[i].Value - } - } - return "" + idx = 1 } if count == 2 { - for i := 0; i < len(val.Items); i++ { - if val.Items[i].Quantity == quantityValues[2] { - return val.Items[i].Value - } - } - return "" + idx = 2 } if count > 2 && count <= few_threshold { - for i := 0; i < len(val.Items); i++ { - if val.Items[i].Quantity == quantityValues[3] { - return val.Items[i].Value - } - } - return "" - } else { - for i := 0; i < len(val.Items); i++ { - if val.Items[i].Quantity == quantityValues[4] { - return val.Items[i].Value - } + idx = 3 + } + + if idx == -1 { + idx = 4 + } + + for i := 0; i < len(val.Items); i++ { + if val.Items[i].Quantity == quantityValues[idx] { + return val.Items[i].Value } - return "" } + return "" } func readBytes(path string) ([]byte, error) { diff --git a/types/msgpack.go b/types/msgpack.go new file mode 100644 index 0000000..fa6d359 --- /dev/null +++ b/types/msgpack.go @@ -0,0 +1,17 @@ +package types + +import "github.com/vmihailenco/msgpack/v5" + +type MsgPackStrategy struct{} + +func (m *MsgPackStrategy) encode(n *Nesting) ([]byte, error) { + return msgpack.Marshal(n) +} + +func (m *MsgPackStrategy) decode(data []byte, v interface{}) error { + if len(data) == 0 { + return nil + } + + return msgpack.Unmarshal(data, v) +} diff --git a/types/types.go b/types/types.go index ba947c9..1f8680f 100644 --- a/types/types.go +++ b/types/types.go @@ -5,37 +5,37 @@ import "encoding/xml" type FileType string type Plural struct { - XMLName xml.Name `xml:"plurals" json:"plurals" yaml:"plurals" toml:"plurals" watson:"plurals"` - Name string `xml:"name,attr" json:"name" yaml:"name" toml:"name" watson:"name"` - Items []*PluralItem `xml:"item" json:"items" yaml:"items,flow" toml:"items,multiline" watson:"items"` + XMLName xml.Name `xml:"plurals" json:"plurals" yaml:"plurals" toml:"plurals" watson:"plurals" msgpack:"plurals"` + Name string `xml:"name,attr" json:"name" yaml:"name" toml:"name" watson:"name" msgpack:"name"` + Items []*PluralItem `xml:"item" json:"items" yaml:"items,flow" toml:"items,multiline" watson:"items" msgpack:"items,as_array"` } type PluralItem struct { - XMLName xml.Name `xml:"item" json:"item" yaml:"item" toml:"item" watson:"item"` - Quantity string `xml:"quantity,attr" json:"quantity" yaml:"quantity" toml:"quantity" watson:"quantity"` - Value string `xml:",innerxml" json:"value" yaml:"value" toml:"value" watson:"value"` + XMLName xml.Name `xml:"item" json:"item" yaml:"item" toml:"item" watson:"item" msgpack:"item"` + Quantity string `xml:"quantity,attr" json:"quantity" yaml:"quantity" toml:"quantity" watson:"quantity" msgpack:"quantity"` + Value string `xml:",innerxml" json:"value" yaml:"value" toml:"value" watson:"value" msgpack:"value"` } type Item struct { - XMLName xml.Name `xml:"item" json:"item" yaml:"item" toml:"item" watson:"item"` - Value string `xml:",innerxml" json:"value" yaml:"value" toml:"value" watson:"value"` + XMLName xml.Name `xml:"item" json:"item" yaml:"item" toml:"item" watson:"item" msgpack:"item"` + Value string `xml:",innerxml" json:"value" yaml:"value" toml:"value" watson:"value" msgpack:"value"` } type StringArray struct { - XMLName xml.Name `xml:"string-array" json:"string-array" yaml:"string-array" toml:"string-array" watson:"string-array"` - Name string `xml:"name,attr" json:"name" yaml:"name" toml:"name" watson:"name"` - Items []*Item `xml:"item" json:"items" yaml:"items,flow" toml:"items,multiline" watson:"items"` + XMLName xml.Name `xml:"string-array" json:"string-array" yaml:"string-array" toml:"string-array" watson:"string-array" msgpack:"string-array"` + Name string `xml:"name,attr" json:"name" yaml:"name" toml:"name" watson:"name" msgpack:"name"` + Items []*Item `xml:"item" json:"items" yaml:"items,flow" toml:"items,multiline" watson:"items" msgpack:"items,as_array" ` } type String struct { - XMLName xml.Name `xml:"string" json:"string" yaml:"string" toml:"string" watson:"string"` - Name string `xml:"name,attr" json:"name" yaml:"name" toml:"name" watson:"name"` - Value string `xml:",innerxml" json:"value" yaml:"value" toml:"value" watson:"value"` + XMLName xml.Name `xml:"string" json:"string" yaml:"string" toml:"string" watson:"string" msgpack:"string"` + Name string `xml:"name,attr" json:"name" yaml:"name" toml:"name" watson:"name" msgpack:"name"` + Value string `xml:",innerxml" json:"value" yaml:"value" toml:"value" watson:"value" msgpack:"value"` } type Nesting struct { - XMLName xml.Name `xml:"resources" json:"resources" yaml:"resources" toml:"resources" watson:"resources"` - Strings []*String `xml:"string" json:"string" yaml:"string,flow" toml:"string,multiline" watson:"string"` - StringsArray []*StringArray `xml:"string-array" json:"string-array" yaml:"string-array,flow" toml:"string-array,multiline" watson:"string-array"` - Plurals []*Plural `xml:"plurals" json:"plurals" yaml:"plurals,flow" toml:"plurals,multiline" watson:"plurals"` + XMLName xml.Name `xml:"resources" json:"resources" yaml:"resources" toml:"resources" watson:"resources" msgpack:"resources"` + Strings []*String `xml:"string" json:"string" yaml:"string,flow" toml:"string,multiline" watson:"string" msgpack:"string,as_array"` + StringsArray []*StringArray `xml:"string-array" json:"string-array" yaml:"string-array,flow" toml:"string-array,multiline" watson:"string-array" msgpack:"string-array,as_array"` + Plurals []*Plural `xml:"plurals" json:"plurals" yaml:"plurals,flow" toml:"plurals,multiline" watson:"plurals" msgpack:"plurals,as_array"` }