gtoml.go 1007 B

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