github.com/zhongdalu/gf@v1.0.0/g/encoding/gparser/gparser_api_new_load.go (about)

     1  // Copyright 2017 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://gitee.com/johng/gp.
     6  
     7  package gparser
     8  
     9  import (
    10  	"github.com/zhongdalu/gf/g/encoding/gjson"
    11  )
    12  
    13  // New creates a Parser object with any variable type of <data>,
    14  // but <data> should be a map or slice for data access reason,
    15  // or it will make no sense.
    16  // The <unsafe> param specifies whether using this Parser object
    17  // in un-concurrent-safe context, which is false in default.
    18  func New(value interface{}, unsafe ...bool) *Parser {
    19  	return &Parser{gjson.New(value, unsafe...)}
    20  }
    21  
    22  // NewUnsafe creates a un-concurrent-safe Parser object.
    23  func NewUnsafe(value ...interface{}) *Parser {
    24  	if len(value) > 0 {
    25  		return &Parser{gjson.New(value[0], false)}
    26  	}
    27  	return &Parser{gjson.New(nil, false)}
    28  }
    29  
    30  // Load loads content from specified file <path>,
    31  // and creates a Parser object from its content.
    32  func Load(path string, unsafe ...bool) (*Parser, error) {
    33  	if j, e := gjson.Load(path, unsafe...); e == nil {
    34  		return &Parser{j}, nil
    35  	} else {
    36  		return nil, e
    37  	}
    38  }
    39  
    40  // LoadContent creates a Parser object from given content,
    41  // it checks the data type of <content> automatically,
    42  // supporting JSON, XML, YAML and TOML types of data.
    43  func LoadContent(data interface{}, unsafe ...bool) (*Parser, error) {
    44  	if j, e := gjson.LoadContent(data, unsafe...); e == nil {
    45  		return &Parser{j}, nil
    46  	} else {
    47  		return nil, e
    48  	}
    49  }