3 Commits

Author SHA1 Message Date
3bb09b70d7 update lint and dependency (#2)
All checks were successful
Go Action / goaction (push) Successful in 33s
Reviewed-on: #2
2025-12-23 23:25:07 +03:00
2df0314393 Merge pull request 'add safeSet from encoding/json/tables.go' (#1) from safe into master
Some checks failed
continuous-integration/drone/push Build is failing
Reviewed-on: #1
2024-01-03 21:42:28 +03:00
andrey
bb6431293d add safeSet from encoding/json/tables.go 2023-12-30 19:50:47 +03:00
15 changed files with 234 additions and 67 deletions

View File

@@ -0,0 +1,26 @@
name: Go Action
on: [push, pull_request]
jobs:
goaction:
runs-on: ubuntu-latest # Use a Gitea Actions runner label
steps:
- name: Check out repository code
uses: actions/checkout@v4 # Action to clone the repo
- name: Set up Go
uses: actions/setup-go@v5 # Action to install a specific Go version
with:
go-version: '1.25.5' # Specify your required Go version
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v8 # Use the golangci-lint action
with:
version: v2.7.2 # Specify the linter version
# Optional: additional arguments
args: --verbose
- name: Run go test
run: go test ./...

View File

@@ -1,4 +1,12 @@
linters-settings:
version: "2"
linters:
default: all
disable:
- wrapcheck
- wsl
- depguard
- revive
settings:
dupl:
threshold: 100
funlen:
@@ -9,32 +17,39 @@ linters-settings:
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
disable:
- scopelint
- maligned
- interfacer
- wrapcheck
issues:
# Excluding configuration per-path, per-linter, per-text and per-source
exclude-rules:
- path: _test\.go
linters:
- gomnd
- path: _suite\.go
linters:
- exhaustivestruct
- gomnd
exclusions:
generated: lax
presets:
- comments
- common-false-positives
- legacy
- std-error-handling
rules:
- linters:
- mnd
path: _test\.go
- linters:
- exhaustivestruct
- mnd
path: _suite\.go
paths:
- third_party$
- builtin$
- examples$
formatters:
enable:
- gci
- gofmt
- gofumpt
- goimports
- golines
exclusions:
generated: lax
paths:
- third_party$
- builtin$
- examples$

View File

@@ -1,5 +1,5 @@
# encoding
[![Build Status](https://drone.gitoa.ru/api/badges/go-4devs/encoding/status.svg)](https://drone.gitoa.ru/go-4devs/encoding)
![Build Status](https://gitoa.ru/go-4devs/encoding/actions/workflows/goaction.yml/badge.svg)
[![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)

View File

@@ -5,7 +5,7 @@ import (
)
// Encode writer from volume.
type Encode func(w io.Writer, v interface{}) error
type Encode func(w io.Writer, v any) error
// Decode reader to volume.
type Decode func(r io.Reader, v interface{}) error
type Decode func(r io.Reader, v any) error

13
go.mod
View File

@@ -1,9 +1,14 @@
module gitoa.ru/go-4devs/encoding
go 1.16
go 1.22
require (
github.com/BurntSushi/toml v0.3.1
github.com/stretchr/testify v1.7.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
github.com/BurntSushi/toml v1.6.0
github.com/stretchr/testify v1.11.1
gopkg.in/yaml.v3 v3.0.1
)
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
)

18
go.sum
View File

@@ -1,14 +1,12 @@
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/BurntSushi/toml v1.6.0 h1:dRaEfpa2VI55EwlIW72hMRHdWouJeRF7TPYhI+AUQjk=
github.com/BurntSushi/toml v1.6.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/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=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
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=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@@ -12,10 +12,10 @@ var (
_ encoding.Encode = Encode
)
func Decode(r io.Reader, v interface{}) error {
func Decode(r io.Reader, v any) error {
return gob.NewDecoder(r).Decode(v)
}
func Encode(w io.Writer, v interface{}) error {
func Encode(w io.Writer, v any) error {
return gob.NewEncoder(w).Encode(v)
}

View File

@@ -13,16 +13,18 @@ var (
)
// Unmarshal by gob decoder.
func Unmarshal(data []byte, v interface{}) error {
func Unmarshal(data []byte, v any) error {
buf := bytes.NewBuffer(data)
return gob.NewDecoder(buf).Decode(v)
}
// Marshal by gob encoder.
func Marshal(v interface{}) ([]byte, error) {
func Marshal(v any) ([]byte, error) {
var data bytes.Buffer
if err := gob.NewEncoder(&data).Encode(v); err != nil {
err := gob.NewEncoder(&data).Encode(v)
if err != nil {
return nil, err
}

View File

@@ -12,10 +12,10 @@ var (
_ encoding.Encode = Encode
)
func Decode(r io.Reader, v interface{}) error {
func Decode(r io.Reader, v any) error {
return json.NewDecoder(r).Decode(v)
}
func Encode(w io.Writer, v interface{}) error {
func Encode(w io.Writer, v any) error {
return json.NewEncoder(w).Encode(v)
}

117
json/safe_set.go Normal file
View File

@@ -0,0 +1,117 @@
package json
import "unicode/utf8"
// Copied from encoding/json/tables.go.
//
// safeSet holds the value true if the ASCII character with the given array
// position can be represented inside a JSON string without any further
// escaping.
//
// All values are true except for the ASCII control characters (0-31), the
// double quote ("), and the backslash character ("\").
//
//nolint:gochecknoglobals
var safeSet = [utf8.RuneSelf]bool{
' ': true,
'!': true,
'"': false,
'#': true,
'$': true,
'%': true,
'&': true,
'\'': true,
'(': true,
')': true,
'*': true,
'+': true,
',': true,
'-': true,
'.': true,
'/': true,
'0': true,
'1': true,
'2': true,
'3': true,
'4': true,
'5': true,
'6': true,
'7': true,
'8': true,
'9': true,
':': true,
';': true,
'<': true,
'=': true,
'>': true,
'?': true,
'@': true,
'A': true,
'B': true,
'C': true,
'D': true,
'E': true,
'F': true,
'G': true,
'H': true,
'I': true,
'J': true,
'K': true,
'L': true,
'M': true,
'N': true,
'O': true,
'P': true,
'Q': true,
'R': true,
'S': true,
'T': true,
'U': true,
'V': true,
'W': true,
'X': true,
'Y': true,
'Z': true,
'[': true,
'\\': false,
']': true,
'^': true,
'_': true,
'`': true,
'a': true,
'b': true,
'c': true,
'd': true,
'e': true,
'f': true,
'g': true,
'h': true,
'i': true,
'j': true,
'k': true,
'l': true,
'm': true,
'n': true,
'o': true,
'p': true,
'q': true,
'r': true,
's': true,
't': true,
'u': true,
'v': true,
'w': true,
'x': true,
'y': true,
'z': true,
'{': true,
'|': true,
'}': true,
'~': true,
'\u007f': true,
}
// CanSafeSet check can be represented inside a JSON string without any further escaping.
func CanSafeSet(r rune) bool {
return safeSet[r]
}

View File

@@ -6,7 +6,6 @@ import (
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"gitoa.ru/go-4devs/encoding"
)
@@ -31,25 +30,29 @@ func expected() Data {
type DecodeSuite struct {
suite.Suite
decode encoding.Decode
data io.Reader
}
// RunSute run test by provider.
// RunDecode run test by provider.
func RunDecode(t *testing.T, decode encoding.Decode, data string) {
t.Helper()
cs := DecodeSuite{
caseSuite := DecodeSuite{
decode: decode,
data: bytes.NewBufferString(data),
Suite: suite.Suite{
Assertions: nil,
},
}
suite.Run(t, &cs)
suite.Run(t, &caseSuite)
}
func (ds *DecodeSuite) TestDecode() {
var d Data
require.Nil(ds.T(), ds.decode(ds.data, &d))
require.Equal(ds.T(), expected(), d)
ds.Require().NoError(ds.decode(ds.data, &d))
ds.Require().Equal(expected(), d)
}

View File

@@ -13,13 +13,13 @@ var (
)
// Decode from reader to value.
func Decode(r io.Reader, v interface{}) error {
_, err := toml.DecodeReader(r, v)
func Decode(r io.Reader, v any) error {
_, err := toml.NewDecoder(r).Decode(v)
return err
}
// Encode from value to writer.
func Encode(w io.Writer, v interface{}) error {
func Encode(w io.Writer, v any) error {
return toml.NewEncoder(w).Encode(v)
}

View File

@@ -1,7 +1,7 @@
package encoding
// Unmarshal bytes to inteface.
type Unmarshal func(data []byte, v interface{}) error
type Unmarshal func(data []byte, v any) error
// Marshal interface to bytes.
type Marshal func(v interface{}) ([]byte, error)
type Marshal func(v any) ([]byte, error)

View File

@@ -1,4 +1,5 @@
package json
//nolint:revive
package xml
import (
"encoding/xml"
@@ -13,11 +14,11 @@ var (
)
// Decode from xml.
func Decode(r io.Reader, v interface{}) error {
func Decode(r io.Reader, v any) error {
return xml.NewDecoder(r).Decode(v)
}
// Encode to xml.
func Encode(w io.Writer, v interface{}) error {
func Encode(w io.Writer, v any) error {
return xml.NewEncoder(w).Encode(v)
}

View File

@@ -13,12 +13,12 @@ var (
)
// Decode from reader to value.
func Decode(r io.Reader, v interface{}) error {
func Decode(r io.Reader, v any) error {
return yaml.NewDecoder(r).Decode(v)
}
// Encode from value to writer.
func Encode(w io.Writer, v interface{}) error {
func Encode(w io.Writer, v any) error {
enc := yaml.NewEncoder(w)
defer enc.Close()