From d6e6001d6b8e31976e0d4659fc7f7a7337eb3368 Mon Sep 17 00:00:00 2001 From: andrey1s Date: Mon, 26 Apr 2021 16:07:41 +0300 Subject: [PATCH] first commit --- .drone.yml | 13 +++++++++++ .gitignore | 17 +++++++++++++++ .golangci.yml | 31 ++++++++++++++++++++++++++ LICENSE | 19 ++++++++++++++++ README.md | 5 +++++ encoding.go | 11 ++++++++++ go.mod | 9 ++++++++ go.sum | 14 ++++++++++++ gob/encoding.go | 19 ++++++++++++++++ gob/unmarshal.go | 28 ++++++++++++++++++++++++ json/encoding.go | 19 ++++++++++++++++ test/decode_suite.go | 51 +++++++++++++++++++++++++++++++++++++++++++ toml/encoding.go | 22 +++++++++++++++++++ toml/encoding_test.go | 19 ++++++++++++++++ unmarshal.go | 7 ++++++ xml/encoding.go | 21 ++++++++++++++++++ yaml/encoding.go | 24 ++++++++++++++++++++ yaml/encoding_test.go | 19 ++++++++++++++++ 18 files changed, 348 insertions(+) create mode 100644 .drone.yml create mode 100644 .gitignore create mode 100644 .golangci.yml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 encoding.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 gob/encoding.go create mode 100644 gob/unmarshal.go create mode 100644 json/encoding.go create mode 100644 test/decode_suite.go create mode 100644 toml/encoding.go create mode 100644 toml/encoding_test.go create mode 100644 unmarshal.go create mode 100644 xml/encoding.go create mode 100644 yaml/encoding.go create mode 100644 yaml/encoding_test.go diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..8887c60 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,13 @@ +kind: pipeline +name: default + +steps: +- name: test + image: golang + commands: + - go test + +- name: golangci-lint + image: golangci/golangci-lint:v1.39 + commands: + - golangci-lint run diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f4d432a --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +# ---> Go +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..672df6e --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,31 @@ +linters-settings: + dupl: + threshold: 100 + funlen: + lines: 100 + statements: 50 + goconst: + min-len: 2 + min-occurrences: 2 + gocyclo: + min-complexity: 15 + golint: + min-confidence: 0 + govet: + check-shadowing: true + lll: + line-length: 140 + maligned: + suggest-new: true + misspell: + locale: US + +linters: + enable-all: true + +issues: + # Excluding configuration per-path, per-linter, per-text and per-source + exclude-rules: + - path: _test\.go + linters: + - gomnd diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f03940c --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +MIT License Copyright (c) 2020 4devs + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is furnished +to do so, subject to the following conditions: + +The above copyright notice and this permission notice (including the next +paragraph) shall be included in all copies or substantial portions of the +Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS +OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF +OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..aca7691 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# encoding + +[![Build Status](https://drone.gitoa.ru/api/badges/go-4devs/encoding/status.svg)](https://drone.gitoa.ru/go-4devs/encoding) +[![Go Report Card](https://goreportcard.com/badge/gitoa.ru/go-4devs/encoding)](https://goreportcard.com/report/gitoa.ru/go-4devs/encoding) +[![GoDoc](https://godoc.org/gitoa.ru/go-4devs/encoding?status.svg)](http://godoc.org/gitoa.ru/go-4devs/encoding) diff --git a/encoding.go b/encoding.go new file mode 100644 index 0000000..d99d3ad --- /dev/null +++ b/encoding.go @@ -0,0 +1,11 @@ +package encoding + +import ( + "io" +) + +// Encode writer from volume. +type Encode func(w io.Writer, v interface{}) error + +// Decode reader to volume. +type Decode func(r io.Reader, v interface{}) error diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ede6a3e --- /dev/null +++ b/go.mod @@ -0,0 +1,9 @@ +module gitoa.ru/go-4devs/encoding + +go 1.16 + +require ( + github.com/BurntSushi/toml v0.3.1 + github.com/stretchr/testify v1.7.0 + gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..f8ade2a --- /dev/null +++ b/go.sum @@ -0,0 +1,14 @@ +github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +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= diff --git a/gob/encoding.go b/gob/encoding.go new file mode 100644 index 0000000..715b60c --- /dev/null +++ b/gob/encoding.go @@ -0,0 +1,19 @@ +package gob + +import ( + "encoding/gob" + "io" + + "gitoa.ru/go-4devs/encoding" +) + +var _ encoding.Decode = Decode +var _ encoding.Encode = Encode + +func Decode(r io.Reader, v interface{}) error { + return gob.NewDecoder(r).Decode(v) +} + +func Encode(w io.Writer, v interface{}) error { + return gob.NewEncoder(w).Encode(v) +} diff --git a/gob/unmarshal.go b/gob/unmarshal.go new file mode 100644 index 0000000..b622c62 --- /dev/null +++ b/gob/unmarshal.go @@ -0,0 +1,28 @@ +package gob + +import ( + "bytes" + "encoding/gob" + + "gitoa.ru/go-4devs/encoding" +) + +var _ encoding.Unmarshal = Unmarshal +var _ encoding.Marshal = Marshal + +// Unmarshal by gob decoder. +func Unmarshal(data []byte, v interface{}) error { + buf := bytes.NewBuffer(data) + + return gob.NewDecoder(buf).Decode(v) +} + +// Marshal by gob encoder. +func Marshal(v interface{}) ([]byte, error) { + var data bytes.Buffer + if err := gob.NewEncoder(&data).Encode(v); err != nil { + return nil, err + } + + return data.Bytes(), nil +} diff --git a/json/encoding.go b/json/encoding.go new file mode 100644 index 0000000..ceb9c35 --- /dev/null +++ b/json/encoding.go @@ -0,0 +1,19 @@ +package json + +import ( + "encoding/json" + "io" + + "gitoa.ru/go-4devs/encoding" +) + +var _ encoding.Decode = Decode +var _ encoding.Encode = Encode + +func Decode(r io.Reader, v interface{}) error { + return json.NewDecoder(r).Decode(v) +} + +func Encode(w io.Writer, v interface{}) error { + return json.NewEncoder(w).Encode(v) +} diff --git a/test/decode_suite.go b/test/decode_suite.go new file mode 100644 index 0000000..7ae1ed8 --- /dev/null +++ b/test/decode_suite.go @@ -0,0 +1,51 @@ +package test + +import ( + "bytes" + "io" + "testing" + "time" + + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" + "gitoa.ru/go-4devs/encoding" +) + +type Data struct { + String string + Time time.Time + Struct SData +} +type SData struct { + ID int64 +} + +func expected() Data { + return Data{ + String: "string data", + Time: time.Date(2020, time.May, 24, 9, 15, 0, 0, time.UTC), + Struct: SData{ID: 42}, + } +} + +type DecodeSuite struct { + suite.Suite + decode encoding.Decode + data io.Reader +} + +// RunSute run test by provider. +func RunDecode(t *testing.T, decode encoding.Decode, data string) { + cs := DecodeSuite{ + decode: decode, + data: bytes.NewBufferString(data), + } + + suite.Run(t, &cs) +} +func (ds *DecodeSuite) TestDecode() { + var d Data + + require.Nil(ds.T(), ds.decode(ds.data, &d)) + require.Equal(ds.T(), expected(), d) +} diff --git a/toml/encoding.go b/toml/encoding.go new file mode 100644 index 0000000..331e096 --- /dev/null +++ b/toml/encoding.go @@ -0,0 +1,22 @@ +package toml + +import ( + "io" + + "github.com/BurntSushi/toml" + "gitoa.ru/go-4devs/encoding" +) + +var _ encoding.Decode = Decode +var _ encoding.Encode = Encode + +// Decode from reader to value. +func Decode(r io.Reader, v interface{}) error { + _, err := toml.DecodeReader(r, v) + return err +} + +// Encode from value to writer. +func Encode(w io.Writer, v interface{}) error { + return toml.NewEncoder(w).Encode(v) +} diff --git a/toml/encoding_test.go b/toml/encoding_test.go new file mode 100644 index 0000000..c7f4f81 --- /dev/null +++ b/toml/encoding_test.go @@ -0,0 +1,19 @@ +package toml_test + +import ( + "testing" + + "gitoa.ru/go-4devs/encoding/test" + "gitoa.ru/go-4devs/encoding/toml" +) + +const data = ` +string = "string data" +time = 2020-05-24T09:15:00Z +[struct] +id = 42 +` + +func TestDecode(t *testing.T) { + test.RunDecode(t, toml.Decode, data) +} diff --git a/unmarshal.go b/unmarshal.go new file mode 100644 index 0000000..5eda83c --- /dev/null +++ b/unmarshal.go @@ -0,0 +1,7 @@ +package encoding + +// Unmarshal bytes to inteface. +type Unmarshal func(data []byte, v interface{}) error + +// Marshal interface to bytes. +type Marshal func(v interface{}) ([]byte, error) diff --git a/xml/encoding.go b/xml/encoding.go new file mode 100644 index 0000000..a223127 --- /dev/null +++ b/xml/encoding.go @@ -0,0 +1,21 @@ +package json + +import ( + "encoding/xml" + "io" + + "gitoa.ru/go-4devs/encoding" +) + +var _ encoding.Decode = Decode +var _ encoding.Encode = Encode + +// Decode from xml. +func Decode(r io.Reader, v interface{}) error { + return xml.NewDecoder(r).Decode(v) +} + +// Encode to xml. +func Encode(w io.Writer, v interface{}) error { + return xml.NewEncoder(w).Encode(v) +} diff --git a/yaml/encoding.go b/yaml/encoding.go new file mode 100644 index 0000000..1f71bfb --- /dev/null +++ b/yaml/encoding.go @@ -0,0 +1,24 @@ +package yaml + +import ( + "io" + + "gitoa.ru/go-4devs/encoding" + "gopkg.in/yaml.v3" +) + +var _ encoding.Decode = Decode +var _ encoding.Encode = Encode + +// Decode from reader to value. +func Decode(r io.Reader, v interface{}) error { + return yaml.NewDecoder(r).Decode(v) +} + +// Encode from value to writer. +func Encode(w io.Writer, v interface{}) error { + enc := yaml.NewEncoder(w) + defer enc.Close() + + return enc.Encode(v) +} diff --git a/yaml/encoding_test.go b/yaml/encoding_test.go new file mode 100644 index 0000000..a5e32bd --- /dev/null +++ b/yaml/encoding_test.go @@ -0,0 +1,19 @@ +package yaml_test + +import ( + "testing" + + "gitoa.ru/go-4devs/encoding/test" + "gitoa.ru/go-4devs/encoding/yaml" +) + +const data = ` +string: "string data" +time: 2020-05-24T09:15:00Z +struct: + id: 42 +` + +func TestDecode(t *testing.T) { + test.RunDecode(t, yaml.Decode, data) +}