Skip to content

Commit

Permalink
- v1.3.0: Added MessagePack support
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinetwigs committed Jun 14, 2022
1 parent 38362ff commit 6cfc034
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 68 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
104 changes: 55 additions & 49 deletions stres.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"os"
"strings"
"sync"

"github.com/Vinetwigs/stres/types"
)
Expand All @@ -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 (
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand Down Expand Up @@ -370,54 +390,40 @@ func GetQuantityString(name string, count int) string {
return ""
}

idx := -1

val, exists := plural_string_entries[name]

if !exists {
return ""
}

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) {
Expand Down
17 changes: 17 additions & 0 deletions types/msgpack.go
Original file line number Diff line number Diff line change
@@ -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)
}
36 changes: 18 additions & 18 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

0 comments on commit 6cfc034

Please sign in to comment.