mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
replace char <xxx> to ;add GetWithEnv/GetWithCmd fuctions for package gcfg
This commit is contained in:
@ -29,7 +29,7 @@ func EncodeToString(src []byte) string {
|
||||
return string(Encode(src))
|
||||
}
|
||||
|
||||
// EncryptFile encodes file content of <path> using BASE64 algorithms.
|
||||
// EncryptFile encodes file content of `path` using BASE64 algorithms.
|
||||
func EncodeFile(path string) ([]byte, error) {
|
||||
content, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
@ -38,7 +38,7 @@ func EncodeFile(path string) ([]byte, error) {
|
||||
return Encode(content), nil
|
||||
}
|
||||
|
||||
// MustEncodeFile encodes file content of <path> using BASE64 algorithms.
|
||||
// MustEncodeFile encodes file content of `path` using BASE64 algorithms.
|
||||
// It panics if any error occurs.
|
||||
func MustEncodeFile(path string) []byte {
|
||||
result, err := EncodeFile(path)
|
||||
@ -48,7 +48,7 @@ func MustEncodeFile(path string) []byte {
|
||||
return result
|
||||
}
|
||||
|
||||
// EncodeFileToString encodes file content of <path> to string using BASE64 algorithms.
|
||||
// EncodeFileToString encodes file content of `path` to string using BASE64 algorithms.
|
||||
func EncodeFileToString(path string) (string, error) {
|
||||
content, err := EncodeFile(path)
|
||||
if err != nil {
|
||||
@ -57,7 +57,7 @@ func EncodeFileToString(path string) (string, error) {
|
||||
return string(content), nil
|
||||
}
|
||||
|
||||
// MustEncodeFileToString encodes file content of <path> to string using BASE64 algorithms.
|
||||
// MustEncodeFileToString encodes file content of `path` to string using BASE64 algorithms.
|
||||
// It panics if any error occurs.
|
||||
func MustEncodeFileToString(path string) string {
|
||||
result, err := EncodeFileToString(path)
|
||||
|
||||
@ -13,8 +13,8 @@ import (
|
||||
"math"
|
||||
)
|
||||
|
||||
// BeEncode encodes one or multiple <values> into bytes using BigEndian.
|
||||
// It uses type asserting checking the type of each value of <values> and internally
|
||||
// BeEncode encodes one or multiple `values` into bytes using BigEndian.
|
||||
// It uses type asserting checking the type of each value of `values` and internally
|
||||
// calls corresponding converting function do the bytes converting.
|
||||
//
|
||||
// It supports common variable type asserting, and finally it uses fmt.Sprintf converting
|
||||
@ -269,7 +269,7 @@ func BeDecodeToFloat64(b []byte) float64 {
|
||||
return math.Float64frombits(binary.BigEndian.Uint64(BeFillUpSize(b, 8)))
|
||||
}
|
||||
|
||||
// BeFillUpSize fills up the bytes <b> to given length <l> using big BigEndian.
|
||||
// BeFillUpSize fills up the bytes `b` to given length `l` using big BigEndian.
|
||||
//
|
||||
// Note that it creates a new bytes slice by copying the original one to avoid changing
|
||||
// the original parameter bytes.
|
||||
|
||||
@ -13,8 +13,8 @@ import (
|
||||
"math"
|
||||
)
|
||||
|
||||
// LeEncode encodes one or multiple <values> into bytes using LittleEndian.
|
||||
// It uses type asserting checking the type of each value of <values> and internally
|
||||
// LeEncode encodes one or multiple `values` into bytes using LittleEndian.
|
||||
// It uses type asserting checking the type of each value of `values` and internally
|
||||
// calls corresponding converting function do the bytes converting.
|
||||
//
|
||||
// It supports common variable type asserting, and finally it uses fmt.Sprintf converting
|
||||
|
||||
@ -40,20 +40,20 @@ var (
|
||||
}
|
||||
)
|
||||
|
||||
// Supported returns whether charset <charset> is supported.
|
||||
// Supported returns whether charset `charset` is supported.
|
||||
func Supported(charset string) bool {
|
||||
return getEncoding(charset) != nil
|
||||
}
|
||||
|
||||
// Convert converts <src> charset encoding from <srcCharset> to <dstCharset>,
|
||||
// Convert converts `src` charset encoding from `srcCharset` to `dstCharset`,
|
||||
// and returns the converted string.
|
||||
// It returns <src> as <dst> if it fails converting.
|
||||
// It returns `src` as `dst` if it fails converting.
|
||||
func Convert(dstCharset string, srcCharset string, src string) (dst string, err error) {
|
||||
if dstCharset == srcCharset {
|
||||
return src, nil
|
||||
}
|
||||
dst = src
|
||||
// Converting <src> to UTF-8.
|
||||
// Converting `src` to UTF-8.
|
||||
if srcCharset != "UTF-8" {
|
||||
if e := getEncoding(srcCharset); e != nil {
|
||||
tmp, err := ioutil.ReadAll(
|
||||
@ -67,7 +67,7 @@ func Convert(dstCharset string, srcCharset string, src string) (dst string, err
|
||||
return dst, gerror.NewCodef(gcode.CodeInvalidParameter, "unsupported srcCharset: %s", srcCharset)
|
||||
}
|
||||
}
|
||||
// Do the converting from UTF-8 to <dstCharset>.
|
||||
// Do the converting from UTF-8 to `dstCharset`.
|
||||
if dstCharset != "UTF-8" {
|
||||
if e := getEncoding(dstCharset); e != nil {
|
||||
tmp, err := ioutil.ReadAll(
|
||||
@ -86,20 +86,20 @@ func Convert(dstCharset string, srcCharset string, src string) (dst string, err
|
||||
return dst, nil
|
||||
}
|
||||
|
||||
// ToUTF8 converts <src> charset encoding from <srcCharset> to UTF-8 ,
|
||||
// ToUTF8 converts `src` charset encoding from `srcCharset` to UTF-8 ,
|
||||
// and returns the converted string.
|
||||
func ToUTF8(srcCharset string, src string) (dst string, err error) {
|
||||
return Convert("UTF-8", srcCharset, src)
|
||||
}
|
||||
|
||||
// UTF8To converts <src> charset encoding from UTF-8 to <dstCharset>,
|
||||
// UTF8To converts `src` charset encoding from UTF-8 to `dstCharset`,
|
||||
// and returns the converted string.
|
||||
func UTF8To(dstCharset string, src string) (dst string, err error) {
|
||||
return Convert(dstCharset, "UTF-8", src)
|
||||
}
|
||||
|
||||
// getEncoding returns the encoding.Encoding interface object for <charset>.
|
||||
// It returns nil if <charset> is not supported.
|
||||
// getEncoding returns the encoding.Encoding interface object for `charset`.
|
||||
// It returns nil if `charset` is not supported.
|
||||
func getEncoding(charset string) encoding.Encoding {
|
||||
if c, ok := charsetAlias[charset]; ok {
|
||||
charset = c
|
||||
|
||||
@ -13,11 +13,11 @@ import (
|
||||
"io"
|
||||
)
|
||||
|
||||
// Gzip compresses <data> using gzip algorithm.
|
||||
// The optional parameter <level> specifies the compression level from
|
||||
// Gzip compresses `data` using gzip algorithm.
|
||||
// The optional parameter `level` specifies the compression level from
|
||||
// 1 to 9 which means from none to the best compression.
|
||||
//
|
||||
// Note that it returns error if given <level> is invalid.
|
||||
// Note that it returns error if given `level` is invalid.
|
||||
func Gzip(data []byte, level ...int) ([]byte, error) {
|
||||
var (
|
||||
writer *gzip.Writer
|
||||
@ -41,7 +41,7 @@ func Gzip(data []byte, level ...int) ([]byte, error) {
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
// GzipFile compresses the file <src> to <dst> using gzip algorithm.
|
||||
// GzipFile compresses the file `src` to `dst` using gzip algorithm.
|
||||
func GzipFile(src, dst string, level ...int) error {
|
||||
var (
|
||||
writer *gzip.Writer
|
||||
@ -75,7 +75,7 @@ func GzipFile(src, dst string, level ...int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnGzip decompresses <data> with gzip algorithm.
|
||||
// UnGzip decompresses `data` with gzip algorithm.
|
||||
func UnGzip(data []byte) ([]byte, error) {
|
||||
var buf bytes.Buffer
|
||||
reader, err := gzip.NewReader(bytes.NewReader(data))
|
||||
@ -91,7 +91,7 @@ func UnGzip(data []byte) ([]byte, error) {
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
// UnGzip decompresses file <src> to <dst> using gzip algorithm.
|
||||
// UnGzip decompresses file `src` to `dst` using gzip algorithm.
|
||||
func UnGzipFile(src, dst string) error {
|
||||
srcFile, err := gfile.Open(src)
|
||||
if err != nil {
|
||||
|
||||
@ -19,10 +19,10 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ZipPath compresses <paths> to <dest> using zip compressing algorithm.
|
||||
// The unnecessary parameter <prefix> indicates the path prefix for zip file.
|
||||
// ZipPath compresses `paths` to `dest` using zip compressing algorithm.
|
||||
// The unnecessary parameter `prefix` indicates the path prefix for zip file.
|
||||
//
|
||||
// Note that the parameter <paths> can be either a directory or a file, which
|
||||
// Note that the parameter `paths` can be either a directory or a file, which
|
||||
// supports multiple paths join with ','.
|
||||
func ZipPath(paths, dest string, prefix ...string) error {
|
||||
writer, err := os.Create(dest)
|
||||
@ -41,10 +41,10 @@ func ZipPath(paths, dest string, prefix ...string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ZipPathWriter compresses <paths> to <writer> using zip compressing algorithm.
|
||||
// The unnecessary parameter <prefix> indicates the path prefix for zip file.
|
||||
// ZipPathWriter compresses `paths` to `writer` using zip compressing algorithm.
|
||||
// The unnecessary parameter `prefix` indicates the path prefix for zip file.
|
||||
//
|
||||
// Note that the parameter <paths> can be either a directory or a file, which
|
||||
// Note that the parameter `paths` can be either a directory or a file, which
|
||||
// supports multiple paths join with ','.
|
||||
func ZipPathWriter(paths string, writer io.Writer, prefix ...string) error {
|
||||
zipWriter := zip.NewWriter(writer)
|
||||
@ -58,10 +58,10 @@ func ZipPathWriter(paths string, writer io.Writer, prefix ...string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// doZipPathWriter compresses the file of given <path> and writes the content to <zipWriter>.
|
||||
// The parameter <exclude> specifies the exclusive file path that is not compressed to <zipWriter>,
|
||||
// doZipPathWriter compresses the file of given `path` and writes the content to `zipWriter`.
|
||||
// The parameter `exclude` specifies the exclusive file path that is not compressed to `zipWriter`,
|
||||
// commonly the destination zip file path.
|
||||
// The unnecessary parameter <prefix> indicates the path prefix for zip file.
|
||||
// The unnecessary parameter `prefix` indicates the path prefix for zip file.
|
||||
func doZipPathWriter(path string, exclude string, zipWriter *zip.Writer, prefix ...string) error {
|
||||
var err error
|
||||
var files []string
|
||||
@ -108,11 +108,11 @@ func doZipPathWriter(path string, exclude string, zipWriter *zip.Writer, prefix
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnZipFile decompresses <archive> to <dest> using zip compressing algorithm.
|
||||
// The optional parameter <path> specifies the unzipped path of <archive>,
|
||||
// UnZipFile decompresses `archive` to `dest` using zip compressing algorithm.
|
||||
// The optional parameter `path` specifies the unzipped path of `archive`,
|
||||
// which can be used to specify part of the archive file to unzip.
|
||||
//
|
||||
// Note that the parameter <dest> should be a directory.
|
||||
// Note that the parameter `dest` should be a directory.
|
||||
func UnZipFile(archive, dest string, path ...string) error {
|
||||
readerCloser, err := zip.OpenReader(archive)
|
||||
if err != nil {
|
||||
@ -122,11 +122,11 @@ func UnZipFile(archive, dest string, path ...string) error {
|
||||
return unZipFileWithReader(&readerCloser.Reader, dest, path...)
|
||||
}
|
||||
|
||||
// UnZipContent decompresses <data> to <dest> using zip compressing algorithm.
|
||||
// The parameter <path> specifies the unzipped path of <archive>,
|
||||
// UnZipContent decompresses `data` to `dest` using zip compressing algorithm.
|
||||
// The parameter `path` specifies the unzipped path of `archive`,
|
||||
// which can be used to specify part of the archive file to unzip.
|
||||
//
|
||||
// Note that the parameter <dest> should be a directory.
|
||||
// Note that the parameter `dest` should be a directory.
|
||||
func UnZipContent(data []byte, dest string, path ...string) error {
|
||||
reader, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
|
||||
if err != nil {
|
||||
@ -186,8 +186,8 @@ func unZipFileWithReader(reader *zip.Reader, dest string, path ...string) error
|
||||
return nil
|
||||
}
|
||||
|
||||
// zipFile compresses the file of given <path> and writes the content to <zw>.
|
||||
// The parameter <prefix> indicates the path prefix for zip file.
|
||||
// zipFile compresses the file of given `path` and writes the content to `zw`.
|
||||
// The parameter `prefix` indicates the path prefix for zip file.
|
||||
func zipFile(path string, prefix string, zw *zip.Writer) error {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
|
||||
@ -13,7 +13,7 @@ import (
|
||||
"io"
|
||||
)
|
||||
|
||||
// Zlib compresses <data> with zlib algorithm.
|
||||
// Zlib compresses `data` with zlib algorithm.
|
||||
func Zlib(data []byte) ([]byte, error) {
|
||||
if data == nil || len(data) < 13 {
|
||||
return data, nil
|
||||
@ -30,7 +30,7 @@ func Zlib(data []byte) ([]byte, error) {
|
||||
return in.Bytes(), nil
|
||||
}
|
||||
|
||||
// UnZlib decompresses <data> with zlib algorithm.
|
||||
// UnZlib decompresses `data` with zlib algorithm.
|
||||
func UnZlib(data []byte) ([]byte, error) {
|
||||
if data == nil || len(data) < 13 {
|
||||
return data, nil
|
||||
|
||||
@ -43,7 +43,7 @@ type iInterface interface {
|
||||
Interface() interface{}
|
||||
}
|
||||
|
||||
// setValue sets <value> to <j> by <pattern>.
|
||||
// setValue sets `value` to `j` by `pattern`.
|
||||
// Note:
|
||||
// 1. If value is nil and removed is true, means deleting this value;
|
||||
// 2. It's quite complicated in hierarchical data search, node creating and data assignment;
|
||||
@ -206,7 +206,7 @@ func (j *Json) setValue(pattern string, value interface{}, removed bool) error {
|
||||
}
|
||||
}
|
||||
|
||||
// If the variable pointed to by the <pointer> is not of a reference type,
|
||||
// If the variable pointed to by the `pointer` is not of a reference type,
|
||||
// then it modifies the variable via its the parent, ie: pparent.
|
||||
default:
|
||||
if removed && value == nil {
|
||||
@ -250,7 +250,7 @@ done:
|
||||
return nil
|
||||
}
|
||||
|
||||
// convertValue converts <value> to map[string]interface{} or []interface{},
|
||||
// convertValue converts `value` to map[string]interface{} or []interface{},
|
||||
// which can be supported for hierarchical data access.
|
||||
func (j *Json) convertValue(value interface{}) interface{} {
|
||||
switch value.(type) {
|
||||
@ -283,7 +283,7 @@ func (j *Json) convertValue(value interface{}) interface{} {
|
||||
}
|
||||
}
|
||||
|
||||
// setPointerWithValue sets <key>:<value> to <pointer>, the <key> may be a map key or slice index.
|
||||
// setPointerWithValue sets `key`:`value` to `pointer`, the `key` may be a map key or slice index.
|
||||
// It returns the pointer to the new value set.
|
||||
func (j *Json) setPointerWithValue(pointer *interface{}, key string, value interface{}) *interface{} {
|
||||
switch (*pointer).(type) {
|
||||
@ -308,7 +308,7 @@ func (j *Json) setPointerWithValue(pointer *interface{}, key string, value inter
|
||||
return pointer
|
||||
}
|
||||
|
||||
// getPointerByPattern returns a pointer to the value by specified <pattern>.
|
||||
// getPointerByPattern returns a pointer to the value by specified `pattern`.
|
||||
func (j *Json) getPointerByPattern(pattern string) *interface{} {
|
||||
if j.vc {
|
||||
return j.getPointerByPatternWithViolenceCheck(pattern)
|
||||
@ -317,7 +317,7 @@ func (j *Json) getPointerByPattern(pattern string) *interface{} {
|
||||
}
|
||||
}
|
||||
|
||||
// getPointerByPatternWithViolenceCheck returns a pointer to the value of specified <pattern> with violence check.
|
||||
// getPointerByPatternWithViolenceCheck returns a pointer to the value of specified `pattern` with violence check.
|
||||
func (j *Json) getPointerByPatternWithViolenceCheck(pattern string) *interface{} {
|
||||
if !j.vc {
|
||||
return j.getPointerByPatternWithoutViolenceCheck(pattern)
|
||||
@ -367,7 +367,7 @@ func (j *Json) getPointerByPatternWithViolenceCheck(pattern string) *interface{}
|
||||
return nil
|
||||
}
|
||||
|
||||
// getPointerByPatternWithoutViolenceCheck returns a pointer to the value of specified <pattern>, with no violence check.
|
||||
// getPointerByPatternWithoutViolenceCheck returns a pointer to the value of specified `pattern`, with no violence check.
|
||||
func (j *Json) getPointerByPatternWithoutViolenceCheck(pattern string) *interface{} {
|
||||
if j.vc {
|
||||
return j.getPointerByPatternWithViolenceCheck(pattern)
|
||||
@ -401,7 +401,7 @@ func (j *Json) getPointerByPatternWithoutViolenceCheck(pattern string) *interfac
|
||||
return nil
|
||||
}
|
||||
|
||||
// checkPatternByPointer checks whether there's value by <key> in specified <pointer>.
|
||||
// checkPatternByPointer checks whether there's value by `key` in specified `pointer`.
|
||||
// It returns a pointer to the value.
|
||||
func (j *Json) checkPatternByPointer(key string, pointer *interface{}) *interface{} {
|
||||
switch (*pointer).(type) {
|
||||
|
||||
@ -27,21 +27,21 @@ func (j *Json) Var() *gvar.Var {
|
||||
return gvar.New(j.Interface())
|
||||
}
|
||||
|
||||
// IsNil checks whether the value pointed by <j> is nil.
|
||||
// IsNil checks whether the value pointed by `j` is nil.
|
||||
func (j *Json) IsNil() bool {
|
||||
j.mu.RLock()
|
||||
defer j.mu.RUnlock()
|
||||
return j.p == nil || *(j.p) == nil
|
||||
}
|
||||
|
||||
// Get retrieves and returns value by specified <pattern>.
|
||||
// It returns all values of current Json object if <pattern> is given empty or string ".".
|
||||
// It returns nil if no value found by <pattern>.
|
||||
// Get retrieves and returns value by specified `pattern`.
|
||||
// It returns all values of current Json object if `pattern` is given empty or string ".".
|
||||
// It returns nil if no value found by `pattern`.
|
||||
//
|
||||
// We can also access slice item by its index number in <pattern> like:
|
||||
// We can also access slice item by its index number in `pattern` like:
|
||||
// "list.10", "array.0.name", "array.0.1.id".
|
||||
//
|
||||
// It returns a default value specified by <def> if value for <pattern> is not found.
|
||||
// It returns a default value specified by `def` if value for `pattern` is not found.
|
||||
func (j *Json) Get(pattern string, def ...interface{}) *gvar.Var {
|
||||
j.mu.RLock()
|
||||
defer j.mu.RUnlock()
|
||||
@ -66,13 +66,13 @@ func (j *Json) Get(pattern string, def ...interface{}) *gvar.Var {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetJson gets the value by specified <pattern>,
|
||||
// GetJson gets the value by specified `pattern`,
|
||||
// and converts it to a un-concurrent-safe Json object.
|
||||
func (j *Json) GetJson(pattern string, def ...interface{}) *Json {
|
||||
return New(j.Get(pattern, def...).Val())
|
||||
}
|
||||
|
||||
// GetJsons gets the value by specified <pattern>,
|
||||
// GetJsons gets the value by specified `pattern`,
|
||||
// and converts it to a slice of un-concurrent-safe Json object.
|
||||
func (j *Json) GetJsons(pattern string, def ...interface{}) []*Json {
|
||||
array := j.Get(pattern, def...).Array()
|
||||
@ -86,7 +86,7 @@ func (j *Json) GetJsons(pattern string, def ...interface{}) []*Json {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetJsonMap gets the value by specified <pattern>,
|
||||
// GetJsonMap gets the value by specified `pattern`,
|
||||
// and converts it to a map of un-concurrent-safe Json object.
|
||||
func (j *Json) GetJsonMap(pattern string, def ...interface{}) map[string]*Json {
|
||||
m := j.Get(pattern, def...).Map()
|
||||
@ -100,25 +100,25 @@ func (j *Json) GetJsonMap(pattern string, def ...interface{}) map[string]*Json {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Set sets value with specified <pattern>.
|
||||
// Set sets value with specified `pattern`.
|
||||
// It supports hierarchical data access by char separator, which is '.' in default.
|
||||
func (j *Json) Set(pattern string, value interface{}) error {
|
||||
return j.setValue(pattern, value, false)
|
||||
}
|
||||
|
||||
// Remove deletes value with specified <pattern>.
|
||||
// Remove deletes value with specified `pattern`.
|
||||
// It supports hierarchical data access by char separator, which is '.' in default.
|
||||
func (j *Json) Remove(pattern string) error {
|
||||
return j.setValue(pattern, nil, true)
|
||||
}
|
||||
|
||||
// Contains checks whether the value by specified <pattern> exist.
|
||||
// Contains checks whether the value by specified `pattern` exist.
|
||||
func (j *Json) Contains(pattern string) bool {
|
||||
return j.Get(pattern) != nil
|
||||
}
|
||||
|
||||
// Len returns the length/size of the value by specified <pattern>.
|
||||
// The target value by <pattern> should be type of slice or map.
|
||||
// Len returns the length/size of the value by specified `pattern`.
|
||||
// The target value by `pattern` should be type of slice or map.
|
||||
// It returns -1 if the target value is not found, or its type is invalid.
|
||||
func (j *Json) Len(pattern string) int {
|
||||
p := j.getPointerByPattern(pattern)
|
||||
@ -135,8 +135,8 @@ func (j *Json) Len(pattern string) int {
|
||||
return -1
|
||||
}
|
||||
|
||||
// Append appends value to the value by specified <pattern>.
|
||||
// The target value by <pattern> should be type of slice.
|
||||
// Append appends value to the value by specified `pattern`.
|
||||
// The target value by `pattern` should be type of slice.
|
||||
func (j *Json) Append(pattern string, value interface{}) error {
|
||||
p := j.getPointerByPattern(pattern)
|
||||
if p == nil || *p == nil {
|
||||
@ -168,7 +168,7 @@ func (j *Json) Array() []interface{} {
|
||||
}
|
||||
|
||||
// Scan automatically calls Struct or Structs function according to the type of parameter
|
||||
// <pointer> to implement the converting.
|
||||
// `pointer` to implement the converting.
|
||||
func (j *Json) Scan(pointer interface{}, mapping ...map[string]string) error {
|
||||
return j.Var().Scan(pointer, mapping...)
|
||||
}
|
||||
@ -180,7 +180,7 @@ func (j *Json) Dump() {
|
||||
gutil.Dump(*j.p)
|
||||
}
|
||||
|
||||
// Export returns <j> as a string with more manually readable.
|
||||
// Export returns `j` as a string with more manually readable.
|
||||
func (j *Json) Export() string {
|
||||
j.mu.RLock()
|
||||
defer j.mu.RUnlock()
|
||||
|
||||
@ -24,22 +24,22 @@ import (
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
// New creates a Json object with any variable type of <data>, but <data> should be a map
|
||||
// New creates a Json object with any variable type of `data`, but `data` should be a map
|
||||
// or slice for data access reason, or it will make no sense.
|
||||
//
|
||||
// The parameter <safe> specifies whether using this Json object in concurrent-safe context,
|
||||
// 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, "json", safe...)
|
||||
}
|
||||
|
||||
// NewWithTag creates a Json object with any variable type of <data>, but <data> should be a map
|
||||
// NewWithTag creates a Json object with any variable type of `data`, but `data` should be a map
|
||||
// or slice for data access reason, or it will make no sense.
|
||||
//
|
||||
// The parameter <tags> specifies priority tags for struct conversion to map, multiple tags joined
|
||||
// The parameter `tags` specifies priority tags for struct conversion to map, multiple tags joined
|
||||
// with char ','.
|
||||
//
|
||||
// The parameter <safe> specifies whether using this Json object in concurrent-safe context, which
|
||||
// The parameter `safe` specifies whether using this Json object in concurrent-safe context, which
|
||||
// is false in default.
|
||||
func NewWithTag(data interface{}, tags string, safe ...bool) *Json {
|
||||
option := Options{
|
||||
@ -51,7 +51,7 @@ func NewWithTag(data interface{}, tags string, safe ...bool) *Json {
|
||||
return NewWithOptions(data, option)
|
||||
}
|
||||
|
||||
// NewWithOptions creates a Json object with any variable type of <data>, but <data> should be a map
|
||||
// NewWithOptions creates a Json object with any variable type of `data`, but `data` should be a map
|
||||
// or slice for data access reason, or it will make no sense.
|
||||
func NewWithOptions(data interface{}, options Options) *Json {
|
||||
var j *Json
|
||||
@ -104,7 +104,7 @@ func NewWithOptions(data interface{}, options Options) *Json {
|
||||
return j
|
||||
}
|
||||
|
||||
// Load loads content from specified file <path>, and creates a Json object from its content.
|
||||
// Load loads content from specified file `path`, and creates a Json object from its content.
|
||||
func Load(path string, safe ...bool) (*Json, error) {
|
||||
if p, err := gfile.Search(path); err != nil {
|
||||
return nil, err
|
||||
@ -163,7 +163,7 @@ func LoadToml(data interface{}, safe ...bool) (*Json, error) {
|
||||
return doLoadContentWithOptions("toml", gconv.Bytes(data), option)
|
||||
}
|
||||
|
||||
// LoadContent creates a Json object from given content, it checks the data type of <content>
|
||||
// LoadContent creates a Json object from given content, it checks the data type of `content`
|
||||
// automatically, supporting data content type as follows:
|
||||
// JSON, XML, INI, YAML and TOML.
|
||||
func LoadContent(data interface{}, safe ...bool) (*Json, error) {
|
||||
@ -193,7 +193,7 @@ func LoadContentType(dataType string, data interface{}, safe ...bool) (*Json, er
|
||||
return doLoadContentWithOptions(dataType, content, option)
|
||||
}
|
||||
|
||||
// IsValidDataType checks and returns whether given <dataType> a valid data type for loading.
|
||||
// IsValidDataType checks and returns whether given `dataType` a valid data type for loading.
|
||||
func IsValidDataType(dataType string) bool {
|
||||
if dataType == "" {
|
||||
return false
|
||||
@ -285,7 +285,7 @@ func doLoadContentWithOptions(dataType string, data []byte, options Options) (*J
|
||||
return NewWithOptions(result, options), nil
|
||||
}
|
||||
|
||||
// checkDataType automatically checks and returns the data type for <content>.
|
||||
// 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 {
|
||||
|
||||
@ -12,20 +12,20 @@ import (
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
// Valid checks whether <data> is a valid JSON data type.
|
||||
// The parameter <data> specifies the json format data, which can be either
|
||||
// Valid checks whether `data` is a valid JSON data type.
|
||||
// The parameter `data` specifies the json format data, which can be either
|
||||
// bytes or string type.
|
||||
func Valid(data interface{}) bool {
|
||||
return json.Valid(gconv.Bytes(data))
|
||||
}
|
||||
|
||||
// Encode encodes any golang variable <value> to JSON bytes.
|
||||
// Encode encodes any golang variable `value` to JSON bytes.
|
||||
func Encode(value interface{}) ([]byte, error) {
|
||||
return json.Marshal(value)
|
||||
}
|
||||
|
||||
// Decode decodes json format <data> to golang variable.
|
||||
// The parameter <data> can be either bytes or string type.
|
||||
// Decode decodes json format `data` to golang variable.
|
||||
// The parameter `data` can be either bytes or string type.
|
||||
func Decode(data interface{}) (interface{}, error) {
|
||||
var value interface{}
|
||||
if err := DecodeTo(gconv.Bytes(data), &value); err != nil {
|
||||
@ -35,9 +35,9 @@ func Decode(data interface{}) (interface{}, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// DecodeTo decodes json format <data> to specified golang variable <v>.
|
||||
// The parameter <data> can be either bytes or string type.
|
||||
// The parameter <v> should be a pointer type.
|
||||
// DecodeTo decodes json format `data` to specified golang variable `v`.
|
||||
// The parameter `data` can be either bytes or string type.
|
||||
// The parameter `v` should be a pointer type.
|
||||
func DecodeTo(data interface{}, v interface{}) error {
|
||||
decoder := json.NewDecoder(bytes.NewReader(gconv.Bytes(data)))
|
||||
// Do not use number, it converts float64 to json.Number type,
|
||||
@ -47,8 +47,8 @@ func DecodeTo(data interface{}, v interface{}) error {
|
||||
return decoder.Decode(v)
|
||||
}
|
||||
|
||||
// DecodeToJson codes json format <data> to a Json object.
|
||||
// The parameter <data> can be either bytes or string type.
|
||||
// DecodeToJson codes json format `data` to a Json object.
|
||||
// The parameter `data` can be either bytes or string type.
|
||||
func DecodeToJson(data interface{}, safe ...bool) (*Json, error) {
|
||||
if v, err := Decode(gconv.Bytes(data)); err != nil {
|
||||
return nil, err
|
||||
|
||||
@ -15,7 +15,7 @@ import (
|
||||
"github.com/gogf/gf/v2/text/gregex"
|
||||
)
|
||||
|
||||
// Decode parses <content> into and returns as map.
|
||||
// Decode parses `content` into and returns as map.
|
||||
func Decode(content []byte) (map[string]interface{}, error) {
|
||||
res, err := convert(content)
|
||||
if err != nil {
|
||||
@ -24,7 +24,7 @@ func Decode(content []byte) (map[string]interface{}, error) {
|
||||
return mxj.NewMapXml(res)
|
||||
}
|
||||
|
||||
// DecodeWithoutRoot parses <content> into a map, and returns the map without root level.
|
||||
// DecodeWithoutRoot parses `content` into a map, and returns the map without root level.
|
||||
func DecodeWithoutRoot(content []byte) (map[string]interface{}, error) {
|
||||
res, err := convert(content)
|
||||
if err != nil {
|
||||
@ -42,19 +42,19 @@ func DecodeWithoutRoot(content []byte) (map[string]interface{}, error) {
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// Encode encodes map <m> to a XML format content as bytes.
|
||||
// The optional parameter <rootTag> is used to specify the XML root tag.
|
||||
// Encode encodes map `m` to an XML format content as bytes.
|
||||
// The optional parameter `rootTag` is used to specify the XML root tag.
|
||||
func Encode(m map[string]interface{}, rootTag ...string) ([]byte, error) {
|
||||
return mxj.Map(m).Xml(rootTag...)
|
||||
}
|
||||
|
||||
// EncodeWithIndent encodes map <m> to an XML format content as bytes with indent.
|
||||
// The optional parameter <rootTag> is used to specify the XML root tag.
|
||||
// EncodeWithIndent encodes map `m` to an XML format content as bytes with indent.
|
||||
// The optional parameter `rootTag` is used to specify the XML root tag.
|
||||
func EncodeWithIndent(m map[string]interface{}, rootTag ...string) ([]byte, error) {
|
||||
return mxj.Map(m).XmlIndent("", "\t", rootTag...)
|
||||
}
|
||||
|
||||
// ToJson converts <content> as XML format into JSON format bytes.
|
||||
// ToJson converts `content` as XML format into JSON format bytes.
|
||||
func ToJson(content []byte) ([]byte, error) {
|
||||
res, err := convert(content)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user