Skip to content

Commit

Permalink
Array value format is added (#4)
Browse files Browse the repository at this point in the history
Co-authored-by: Konstantin Zhernosenko <konstantin.zhernosenko@humans.net>
  • Loading branch information
kainobor and Konstantin Zhernosenko authored Feb 11, 2021
1 parent 6a08a25 commit 285067f
Show file tree
Hide file tree
Showing 21 changed files with 1,433 additions and 188 deletions.
4 changes: 2 additions & 2 deletions cmd/cimp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func main() {
arrayValueFormat, err := cimp.NewFormat(*arrayValueFormatRaw, path)
check(err)

kv := cimp.NewKV()
check(kv.FillFromFile(path, format, arrayValueFormat))
kv := cimp.NewKV("", arrayValueFormat)
check(kv.FillFromFile(path, format))

kv.AddPrefix(*prefixRaw)

Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ module github.com/humans-group/cimp
go 1.15

require (
github.com/go-yaml/yaml v2.1.0+incompatible
github.com/google/btree v1.0.0 // indirect
github.com/hashicorp/consul/api v1.8.1
github.com/hashicorp/golang-lru v0.5.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)
11 changes: 3 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/go-yaml/yaml v2.1.0+incompatible h1:RYi2hDdss1u4YE7GwixGzWwVo47T8UQwnTLB6vQiq+o=
github.com/go-yaml/yaml v2.1.0+incompatible/go.mod h1:w2MrLa16VYP0jy6N7M5kHaCkaLENm+P+Tv+MfurjSw0=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/btree v1.0.0 h1:0udJVsspx3VBr5FwtLhQQtuAsVc79tTq0ocGIPAU6qo=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
Expand Down Expand Up @@ -40,7 +38,6 @@ github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdv
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-uuid v1.0.1 h1:fv1ep09latC32wFoVwnqcnKJGnMSdBanPczbHAYm1BE=
github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/golang-lru v0.5.0 h1:CL2msUPvZTLb5O648aiLNJw3hnBxN2+1Jq8rCOH9wdo=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1 h1:0hERBMJE1eitiLkihrMvRVBYAkpHzc/J3QdDN+dAcgU=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
Expand Down Expand Up @@ -116,7 +113,6 @@ golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190907020128-2ca718005c18/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
Expand All @@ -125,8 +121,7 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 h1:slmdOY3vp8a7KQbHkL+FLbvbkgMqmXojpFUO/jENuqQ=
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3/go.mod h1:oVgVk4OWVDi43qWBEyGhXgYxt7+ED4iYNpTngSLX2Iw=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
55 changes: 0 additions & 55 deletions lib/cimp/fileformat.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
package cimp

import (
"encoding/json"
"fmt"
"path/filepath"

"github.com/go-yaml/yaml"
"olympos.io/encoding/edn"
)

type FileFormat string

const (
JSONFormat FileFormat = "json"
YAMLFormat FileFormat = "yaml"
EDNFormat FileFormat = "edn"
)

func NewFormat(format, path string) (FileFormat, error) {
Expand All @@ -24,8 +19,6 @@ func NewFormat(format, path string) (FileFormat, error) {
return JSONFormat, nil
case YAMLFormat:
return YAMLFormat, nil
case EDNFormat:
return EDNFormat, nil
default:
return "", fmt.Errorf("undefined format: %s", format)
}
Expand All @@ -36,55 +29,7 @@ func NewFormat(format, path string) (FileFormat, error) {
return JSONFormat, nil
case ".yml", ".yaml":
return YAMLFormat, nil
case ".edn":
return EDNFormat, nil
}

return YAMLFormat, nil
}

func UnmarshalWithFormat(format FileFormat, fileData []byte) (map[interface{}]interface{}, error) {
cfgRaw := make(map[interface{}]interface{})
var err error

switch format {
case JSONFormat:
err = json.Unmarshal(fileData, &cfgRaw)
case YAMLFormat:
err = yaml.Unmarshal(fileData, &cfgRaw)
case EDNFormat:
err = edn.Unmarshal(fileData, &cfgRaw)
default:
return nil, fmt.Errorf("unsupported format: %v", format)
}

if err != nil {
return nil, fmt.Errorf("unmarshal with format %q error: %w", format, err)
}

return cfgRaw, nil
}

func MarshalWithFormat(format FileFormat, smth interface{}) ([]byte, error) {
var (
marshaled []byte
err error
)

switch format {
case JSONFormat:
marshaled, err = json.Marshal(smth)
case YAMLFormat:
marshaled, err = yaml.Marshal(smth)
case EDNFormat:
marshaled, err = edn.Marshal(smth)
default:
return nil, fmt.Errorf("unsupported format: %v", format)
}

if err != nil {
return nil, fmt.Errorf("marshal with format %q error: %w", format, err)
}

return marshaled, nil
}
Loading

0 comments on commit 285067f

Please sign in to comment.