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