yaml_loader.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package yaml_loader
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/pkg/errors"
  6. "gopkg.in/yaml.v3"
  7. "io"
  8. "os"
  9. "reflect"
  10. )
  11. type LoadMapCallback func(objectMap map[string]any) error
  12. // LoadYamlFile 加载YAML文件,一个文件中可以有多个YAML对象
  13. // 参数:
  14. // yamlFilePath: YAML文件路径
  15. // retObjects: 用来接收YAML对象的指针或者map的slice
  16. // 返回值:
  17. // 错误
  18. func LoadYamlFile(yamlFilePath string, retObjects *[]any) error {
  19. yamlFile, err := os.Open(yamlFilePath)
  20. if err != nil {
  21. return err
  22. }
  23. defer func(yamlFile *os.File) {
  24. err := yamlFile.Close()
  25. if err != nil {
  26. fmt.Println(err)
  27. }
  28. }(yamlFile)
  29. return loadYaml(yamlFile, retObjects)
  30. }
  31. // LoadYamlBytes 加载YAML文件字节,一个文件中可以有多个YAML对象
  32. // 参数:
  33. // yamlBytes: YAML文件的字节
  34. // retObjects: 用来接收YAML对象的指针或者map的slice
  35. // 返回值:
  36. // 错误
  37. func LoadYamlBytes(yamlBytes []byte, retObjects *[]any) error {
  38. return loadYaml(bytes.NewReader(yamlBytes), retObjects)
  39. }
  40. // LoadYamlMapFile 加载YAML文件,一个文件中可以有多个YAML对象,顶级不能是YAML数组
  41. // 参数:
  42. // yamlFilePath: YAML文件路径
  43. // callback: 回调函数
  44. // 返回值:
  45. // 错误
  46. func LoadYamlMapFile(yamlFilePath string, callback LoadMapCallback) error {
  47. yamlFile, err := os.Open(yamlFilePath)
  48. if err != nil {
  49. return err
  50. }
  51. defer func(yamlFile *os.File) {
  52. err := yamlFile.Close()
  53. if err != nil {
  54. fmt.Println(err)
  55. }
  56. }(yamlFile)
  57. return loadYamlMap(yamlFile, callback)
  58. }
  59. // LoadYamlMapBytes 加载YAML文件字节,一个文件中可以有多个YAML对象,顶级不能是YAML数组
  60. // 参数:
  61. // yamlBytes: YAML文件的字节
  62. // callback: 回调函数
  63. // 返回值:
  64. // 错误
  65. func LoadYamlMapBytes(yamlBytes []byte, callback LoadMapCallback) error {
  66. return loadYamlMap(bytes.NewReader(yamlBytes), callback)
  67. }
  68. func loadYaml(r io.Reader, retObjects *[]any) error {
  69. decoder := yaml.NewDecoder(r)
  70. for i := 0; i < len(*retObjects); i++ {
  71. retObject := (*retObjects)[i]
  72. retObjectType := reflect.TypeOf(retObject)
  73. if retObjectType.Kind() != reflect.Ptr && retObjectType.Kind() != reflect.Map {
  74. return errors.New("返回对象slice需要指针或者map")
  75. }
  76. err := decoder.Decode(retObject)
  77. if err != nil {
  78. if err == io.EOF {
  79. break
  80. }
  81. return err
  82. }
  83. }
  84. return nil
  85. }
  86. func loadYamlMap(r io.Reader, callback LoadMapCallback) error {
  87. decoder := yaml.NewDecoder(r)
  88. for {
  89. object := make(map[string]any)
  90. err := decoder.Decode(object)
  91. if err != nil {
  92. if err == io.EOF {
  93. break
  94. }
  95. return err
  96. }
  97. err = callback(object)
  98. if err != nil {
  99. return err
  100. }
  101. }
  102. return nil
  103. }