replace gopkg.in/yaml.v2 with gopkg.in/yaml.v3

This commit is contained in:
John Guo
2021-10-25 19:17:56 +08:00
parent 17861fc45d
commit 4717e01708
8 changed files with 109 additions and 56 deletions

View File

@ -4,7 +4,7 @@
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// Package charset implements character-set conversion functionality.
// Package gcharset implements character-set conversion functionality.
//
// Supported Character Set:
//
@ -21,8 +21,10 @@ package gcharset
import (
"bytes"
"context"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/internal/intlog"
"io/ioutil"
"golang.org/x/text/encoding"
@ -104,8 +106,9 @@ func getEncoding(charset string) encoding.Encoding {
if c, ok := charsetAlias[charset]; ok {
charset = c
}
if e, err := ianaindex.MIB.Encoding(charset); err == nil && e != nil {
return e
enc, err := ianaindex.MIB.Encoding(charset)
if err != nil {
intlog.Error(context.TODO(), err)
}
return nil
return enc
}

View File

@ -7,7 +7,7 @@
// Package ghash provides some classic hash functions(uint32/uint64) in go.
package ghash
// BKDR Hash Function
// BKDRHash implements the classic BKDR hash algorithm for 32 bits.
func BKDRHash(str []byte) uint32 {
var seed uint32 = 131 // 31 131 1313 13131 131313 etc..
var hash uint32 = 0
@ -17,7 +17,7 @@ func BKDRHash(str []byte) uint32 {
return hash
}
// BKDR Hash Function 64
// BKDRHash64 implements the classic BKDR hash algorithm for 64 bits.
func BKDRHash64(str []byte) uint64 {
var seed uint64 = 131 // 31 131 1313 13131 131313 etc..
var hash uint64 = 0
@ -27,7 +27,7 @@ func BKDRHash64(str []byte) uint64 {
return hash
}
// SDBM Hash
// SDBMHash implements the classic SDBM hash algorithm for 32 bits.
func SDBMHash(str []byte) uint32 {
var hash uint32 = 0
for i := 0; i < len(str); i++ {
@ -37,7 +37,7 @@ func SDBMHash(str []byte) uint32 {
return hash
}
// SDBM Hash 64
// SDBMHash64 implements the classic SDBM hash algorithm for 64 bits.
func SDBMHash64(str []byte) uint64 {
var hash uint64 = 0
for i := 0; i < len(str); i++ {
@ -47,7 +47,7 @@ func SDBMHash64(str []byte) uint64 {
return hash
}
// RS Hash Function
// RSHash implements the classic RS hash algorithm for 32 bits.
func RSHash(str []byte) uint32 {
var b uint32 = 378551
var a uint32 = 63689
@ -59,7 +59,7 @@ func RSHash(str []byte) uint32 {
return hash
}
// RS Hash Function 64
// RSHash64 implements the classic RS hash algorithm for 64 bits.
func RSHash64(str []byte) uint64 {
var b uint64 = 378551
var a uint64 = 63689
@ -71,7 +71,7 @@ func RSHash64(str []byte) uint64 {
return hash
}
// JS Hash Function
// JSHash implements the classic JS hash algorithm for 32 bits.
func JSHash(str []byte) uint32 {
var hash uint32 = 1315423911
for i := 0; i < len(str); i++ {
@ -80,7 +80,7 @@ func JSHash(str []byte) uint32 {
return hash
}
// JS Hash Function 64
// JSHash64 implements the classic JS hash algorithm for 64 bits.
func JSHash64(str []byte) uint64 {
var hash uint64 = 1315423911
for i := 0; i < len(str); i++ {
@ -89,7 +89,7 @@ func JSHash64(str []byte) uint64 {
return hash
}
// P. J. Weinberger Hash Function
// PJWHash implements the classic PJW hash algorithm for 32 bits.
func PJWHash(str []byte) uint32 {
var BitsInUnignedInt uint32 = 4 * 8
var ThreeQuarters uint32 = (BitsInUnignedInt * 3) / 4
@ -106,7 +106,7 @@ func PJWHash(str []byte) uint32 {
return hash
}
// P. J. Weinberger Hash Function 64
// PJWHash64 implements the classic PJW hash algorithm for 64 bits.
func PJWHash64(str []byte) uint64 {
var BitsInUnignedInt uint64 = 4 * 8
var ThreeQuarters uint64 = (BitsInUnignedInt * 3) / 4
@ -123,7 +123,7 @@ func PJWHash64(str []byte) uint64 {
return hash
}
// ELF Hash Function
// ELFHash implements the classic ELF hash algorithm for 32 bits.
func ELFHash(str []byte) uint32 {
var hash uint32 = 0
var x uint32 = 0
@ -137,7 +137,7 @@ func ELFHash(str []byte) uint32 {
return hash
}
// ELF Hash Function 64
// ELFHash64 implements the classic ELF hash algorithm for 64 bits.
func ELFHash64(str []byte) uint64 {
var hash uint64 = 0
var x uint64 = 0
@ -151,7 +151,7 @@ func ELFHash64(str []byte) uint64 {
return hash
}
// DJB Hash Function
// DJBHash implements the classic DJB hash algorithm for 32 bits.
func DJBHash(str []byte) uint32 {
var hash uint32 = 5381
for i := 0; i < len(str); i++ {
@ -160,7 +160,7 @@ func DJBHash(str []byte) uint32 {
return hash
}
// DJB Hash Function 64.
// DJBHash64 implements the classic DJB hash algorithm for 64 bits.
func DJBHash64(str []byte) uint64 {
var hash uint64 = 5381
for i := 0; i < len(str); i++ {
@ -169,7 +169,7 @@ func DJBHash64(str []byte) uint64 {
return hash
}
// AP Hash Function
// APHash implements the classic AP hash algorithm for 32 bits.
func APHash(str []byte) uint32 {
var hash uint32 = 0
for i := 0; i < len(str); i++ {
@ -182,7 +182,7 @@ func APHash(str []byte) uint32 {
return hash
}
// AP Hash Function 64
// APHash64 implements the classic AP hash algorithm for 64 bits.
func APHash64(str []byte) uint64 {
var hash uint64 = 0
for i := 0; i < len(str); i++ {

View File

@ -1,3 +1,9 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package ghash_test
import (

View File

@ -48,10 +48,10 @@ func Example_conversionNormalFormats() {
// ======================
// YAML:
// users:
// array:
// - John
// - Ming
// count: 1
// array:
// - John
// - Ming
// count: 1
//
// ======================
// TOML:

View File

@ -246,3 +246,46 @@ enable=true
}
})
}
func Test_Load_YamlWithV3(t *testing.T) {
content := `
# CLI tool, only in development environment.
# https://goframe.org/pages/viewpage.action?pageId=3673173
gfcli:
gen:
dao:
- path : "../../pkg/oss/oss/internal"
group : "oss"
stdTime : true
descriptionTag : true
noJsonTag : true
noModelComment : true
overwriteDao : true
modelFileForDao : "model_dao.go"
tablesEx : |
bpmn_info,
dlocker,
dlocker_detail,
message_table,
monitor_data,
resource_param_info,
version_info,
version_topology_info,
work_flow,
work_flow_step_info,
work_flow_undo_step_info
- path : "../../pkg/oss/workflow/internal"
group : "workflow"
stdTime : true
descriptionTag : true
noJsonTag : true
noModelComment : true
overwriteDao : true
modelFileForDao : "model_dao.go"
`
gtest.C(t, func(t *gtest.T) {
_, err := gjson.LoadContent(content)
t.AssertNil(err)
})
}

View File

@ -8,32 +8,50 @@
package gyaml
import (
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/internal/json"
"gopkg.in/yaml.v2"
"gopkg.in/yaml.v3"
"github.com/gogf/gf/v2/util/gconv"
)
func Encode(v interface{}) ([]byte, error) {
return yaml.Marshal(v)
func Encode(value interface{}) (out []byte, err error) {
if out, err = yaml.Marshal(value); err != nil {
err = gerror.Wrap(err, `encode value to yaml failed`)
}
return
}
func Decode(v []byte) (interface{}, error) {
var result map[string]interface{}
if err := yaml.Unmarshal(v, &result); err != nil {
func Decode(value []byte) (interface{}, error) {
var (
result map[string]interface{}
err error
)
if err = yaml.Unmarshal(value, &result); err != nil {
err = gerror.Wrap(err, `decode yaml failed`)
return nil, err
}
return gconv.MapDeep(result), nil
}
func DecodeTo(v []byte, result interface{}) error {
return yaml.Unmarshal(v, result)
func DecodeTo(value []byte, result interface{}) (err error) {
err = yaml.Unmarshal(value, result)
if err != nil {
err = gerror.Wrap(err, `encode yaml to value failed`)
}
return
}
func ToJson(v []byte) ([]byte, error) {
if r, err := Decode(v); err != nil {
func ToJson(value []byte) (out []byte, err error) {
var (
result interface{}
)
if result, err = Decode(value); err != nil {
return nil, err
} else {
return json.Marshal(r)
if out, err = json.Marshal(result); err != nil {
err = gerror.Wrap(err, `convert yaml to json failed`)
}
return out, err
}
}