You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

statemachine_config_parser.go 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package parser
  18. import (
  19. "bytes"
  20. "fmt"
  21. "github.com/seata/seata-go/pkg/saga/statemachine"
  22. "io"
  23. "os"
  24. "github.com/knadh/koanf"
  25. "github.com/knadh/koanf/parsers/json"
  26. "github.com/knadh/koanf/parsers/yaml"
  27. "github.com/knadh/koanf/providers/rawbytes"
  28. )
  29. // ConfigParser is a general configuration parser interface, used to agree on the implementation of different types of parsers
  30. type ConfigParser interface {
  31. Parse(configContent []byte) (*statemachine.StateMachineObject, error)
  32. }
  33. type JSONConfigParser struct{}
  34. func NewJSONConfigParser() *JSONConfigParser {
  35. return &JSONConfigParser{}
  36. }
  37. func (p *JSONConfigParser) Parse(configContent []byte) (*statemachine.StateMachineObject, error) {
  38. if configContent == nil || len(configContent) == 0 {
  39. return nil, fmt.Errorf("empty JSON config content")
  40. }
  41. k := koanf.New(".")
  42. if err := k.Load(rawbytes.Provider(configContent), json.Parser()); err != nil {
  43. return nil, fmt.Errorf("failed to parse JSON config content: %w", err)
  44. }
  45. var stateMachineObject statemachine.StateMachineObject
  46. if err := k.Unmarshal("", &stateMachineObject); err != nil {
  47. return nil, fmt.Errorf("failed to unmarshal JSON config to struct: %w", err)
  48. }
  49. return &stateMachineObject, nil
  50. }
  51. type YAMLConfigParser struct{}
  52. func NewYAMLConfigParser() *YAMLConfigParser {
  53. return &YAMLConfigParser{}
  54. }
  55. func (p *YAMLConfigParser) Parse(configContent []byte) (*statemachine.StateMachineObject, error) {
  56. if configContent == nil || len(configContent) == 0 {
  57. return nil, fmt.Errorf("empty YAML config content")
  58. }
  59. k := koanf.New(".")
  60. if err := k.Load(rawbytes.Provider(configContent), yaml.Parser()); err != nil {
  61. return nil, fmt.Errorf("failed to parse YAML config content: %w", err)
  62. }
  63. var stateMachineObject statemachine.StateMachineObject
  64. if err := k.Unmarshal("", &stateMachineObject); err != nil {
  65. return nil, fmt.Errorf("failed to unmarshal YAML config to struct: %w", err)
  66. }
  67. return &stateMachineObject, nil
  68. }
  69. type StateMachineConfigParser struct{}
  70. func NewStateMachineConfigParser() *StateMachineConfigParser {
  71. return &StateMachineConfigParser{}
  72. }
  73. func (p *StateMachineConfigParser) CheckConfigFile(filePath string) error {
  74. _, err := os.Stat(filePath)
  75. if os.IsNotExist(err) {
  76. return fmt.Errorf("config file %s does not exist: %w", filePath, err)
  77. }
  78. if err != nil {
  79. return fmt.Errorf("failed to access config file %s: %w", filePath, err)
  80. }
  81. return nil
  82. }
  83. func (p *StateMachineConfigParser) ReadConfigFile(configFilePath string) ([]byte, error) {
  84. file, _ := os.Open(configFilePath)
  85. defer func(file *os.File) {
  86. _ = file.Close()
  87. }(file)
  88. var buf bytes.Buffer
  89. _, err := io.Copy(&buf, file)
  90. if err != nil {
  91. return nil, fmt.Errorf("failed to read config file %s: %w", configFilePath, err)
  92. }
  93. return buf.Bytes(), nil
  94. }
  95. func (p *StateMachineConfigParser) getParser(content []byte) (ConfigParser, error) {
  96. k := koanf.New(".")
  97. if err := k.Load(rawbytes.Provider(content), json.Parser()); err == nil {
  98. return NewJSONConfigParser(), nil
  99. }
  100. k = koanf.New(".")
  101. if err := k.Load(rawbytes.Provider(content), yaml.Parser()); err == nil {
  102. return NewYAMLConfigParser(), nil
  103. }
  104. return nil, fmt.Errorf("unsupported config file format")
  105. }
  106. func (p *StateMachineConfigParser) Parse(content []byte) (*statemachine.StateMachineObject, error) {
  107. parser, err := p.getParser(content)
  108. if err != nil {
  109. return nil, err
  110. }
  111. return parser.Parse(content)
  112. }