feature/v2.2.0 (#2154)

This commit is contained in:
John Guo
2022-09-26 22:11:13 +08:00
committed by GitHub
parent 9dc97f4b0d
commit 141ca62c6d
147 changed files with 7965 additions and 1806 deletions

View File

@ -21,15 +21,17 @@ import (
"github.com/gogf/gf/v2/util/gconv"
)
type ContentType string
const (
ContentTypeJson = `json`
ContentTypeJs = `js`
ContentTypeXml = `xml`
ContentTypeIni = `ini`
ContentTypeYaml = `yaml`
ContentTypeYml = `yml`
ContentTypeToml = `toml`
ContentTypeProperties = `properties`
ContentTypeJson ContentType = `json`
ContentTypeJs ContentType = `js`
ContentTypeXml ContentType = `xml`
ContentTypeIni ContentType = `ini`
ContentTypeYaml ContentType = `yaml`
ContentTypeYml ContentType = `yml`
ContentTypeToml ContentType = `toml`
ContentTypeProperties ContentType = `properties`
)
const (
@ -46,10 +48,10 @@ type Json struct {
// Options for Json object creating/loading.
type Options struct {
Safe bool // Mark this object is for in concurrent-safe usage. This is especially for Json object creating.
Tags string // Custom priority tags for decoding, eg: "json,yaml,MyTag". This is especially for struct parsing into Json object.
Type string // Type specifies the data content type, eg: json, xml, yaml, toml, ini.
StrNumber bool // StrNumber causes the Decoder to unmarshal a number into an interface{} as a string instead of as a float64.
Safe bool // Mark this object is for in concurrent-safe usage. This is especially for Json object creating.
Tags string // Custom priority tags for decoding, eg: "json,yaml,MyTag". This is especially for struct parsing into Json object.
Type ContentType // Type specifies the data content type, eg: json, xml, yaml, toml, ini.
StrNumber bool // StrNumber causes the Decoder to unmarshal a number into an interface{} as a string instead of as a float64.
}
// iInterfaces is used for type assert api for Interfaces().

View File

@ -32,7 +32,7 @@ import (
// The parameter `safe` specifies whether using this Json object in concurrent-safe context,
// which is false in default.
func New(data interface{}, safe ...bool) *Json {
return NewWithTag(data, ContentTypeJson, safe...)
return NewWithTag(data, string(ContentTypeJson), safe...)
}
// NewWithTag creates a Json object with any variable type of `data`, but `data` should be a map
@ -107,7 +107,7 @@ func Load(path string, safe ...bool) (*Json, error) {
path = p
}
options := Options{
Type: gfile.Ext(path),
Type: ContentType(gfile.Ext(path)),
}
if len(safe) > 0 && safe[0] {
options.Safe = true
@ -200,7 +200,7 @@ func LoadContent(data interface{}, safe ...bool) (*Json, error) {
// LoadContentType creates a Json object from given type and content,
// supporting data content type as follows:
// JSON, XML, INI, YAML and TOML.
func LoadContentType(dataType string, data interface{}, safe ...bool) (*Json, error) {
func LoadContentType(dataType ContentType, data interface{}, safe ...bool) (*Json, error) {
content := gconv.Bytes(data)
if len(content) == 0 {
return New(nil, safe...), nil
@ -220,7 +220,7 @@ func LoadContentType(dataType string, data interface{}, safe ...bool) (*Json, er
}
// IsValidDataType checks and returns whether given `dataType` a valid data type for loading.
func IsValidDataType(dataType string) bool {
func IsValidDataType(dataType ContentType) bool {
if dataType == "" {
return false
}
@ -279,7 +279,9 @@ func doLoadContentWithOptions(data []byte, options Options) (*Json, error) {
if options.Type == "" {
options.Type = checkDataType(data)
}
options.Type = gstr.TrimLeft(options.Type, ".")
options.Type = ContentType(gstr.TrimLeft(
string(options.Type), "."),
)
switch options.Type {
case ContentTypeJson, ContentTypeJs:
@ -334,7 +336,7 @@ func doLoadContentWithOptions(data []byte, options Options) (*Json, error) {
// checkDataType automatically checks and returns the data type for `content`.
// Note that it uses regular expression for loose checking, you can use LoadXXX/LoadContentType
// functions to load the content for certain content type.
func checkDataType(content []byte) string {
func checkDataType(content []byte) ContentType {
if json.Valid(content) {
return ContentTypeJson
} else if gregex.IsMatch(`^<.+>[\S\s]+<.+>\s*$`, content) {