This commit is contained in:
13
.drone.yml
Normal file
13
.drone.yml
Normal file
@@ -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
|
||||||
17
.gitignore
vendored
Normal file
17
.gitignore
vendored
Normal file
@@ -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/
|
||||||
|
|
||||||
31
.golangci.yml
Normal file
31
.golangci.yml
Normal file
@@ -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
|
||||||
19
LICENSE
Normal file
19
LICENSE
Normal file
@@ -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.
|
||||||
5
README.md
Normal file
5
README.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
# encoding
|
||||||
|
|
||||||
|
[](https://drone.gitoa.ru/go-4devs/encoding)
|
||||||
|
[](https://goreportcard.com/report/gitoa.ru/go-4devs/encoding)
|
||||||
|
[](http://godoc.org/gitoa.ru/go-4devs/encoding)
|
||||||
11
encoding.go
Normal file
11
encoding.go
Normal file
@@ -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
|
||||||
9
go.mod
Normal file
9
go.mod
Normal file
@@ -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
|
||||||
|
)
|
||||||
14
go.sum
Normal file
14
go.sum
Normal file
@@ -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=
|
||||||
19
gob/encoding.go
Normal file
19
gob/encoding.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
28
gob/unmarshal.go
Normal file
28
gob/unmarshal.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
19
json/encoding.go
Normal file
19
json/encoding.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
51
test/decode_suite.go
Normal file
51
test/decode_suite.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
22
toml/encoding.go
Normal file
22
toml/encoding.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
19
toml/encoding_test.go
Normal file
19
toml/encoding_test.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
7
unmarshal.go
Normal file
7
unmarshal.go
Normal file
@@ -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)
|
||||||
21
xml/encoding.go
Normal file
21
xml/encoding.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
24
yaml/encoding.go
Normal file
24
yaml/encoding.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
19
yaml/encoding_test.go
Normal file
19
yaml/encoding_test.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user