github.com/gogf/gf@v1.16.9/encoding/gtoml/gtoml.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). 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://github.com/gogf/gf.
     6  
     7  // Package gtoml provides accessing and converting for TOML content.
     8  package gtoml
     9  
    10  import (
    11  	"bytes"
    12  	"github.com/gogf/gf/internal/json"
    13  
    14  	"github.com/BurntSushi/toml"
    15  )
    16  
    17  func Encode(v interface{}) ([]byte, error) {
    18  	buffer := bytes.NewBuffer(nil)
    19  	if err := toml.NewEncoder(buffer).Encode(v); err != nil {
    20  		return nil, err
    21  	}
    22  	return buffer.Bytes(), nil
    23  }
    24  
    25  func Decode(v []byte) (interface{}, error) {
    26  	var result interface{}
    27  	if err := toml.Unmarshal(v, &result); err != nil {
    28  		return nil, err
    29  	}
    30  	return result, nil
    31  }
    32  
    33  func DecodeTo(v []byte, result interface{}) error {
    34  	return toml.Unmarshal(v, result)
    35  }
    36  
    37  func ToJson(v []byte) ([]byte, error) {
    38  	if r, err := Decode(v); err != nil {
    39  		return nil, err
    40  	} else {
    41  		return json.Marshal(r)
    42  	}
    43  }