Skip to content

Commit

Permalink
refactor config parser
Browse files Browse the repository at this point in the history
  • Loading branch information
FinnTew committed Jan 7, 2025
1 parent 5e76a6c commit 7929470
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 67 deletions.
117 changes: 53 additions & 64 deletions pkg/saga/statemachine/statelang/parser/statemachine_config_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,77 +24,13 @@ import (
"gopkg.in/yaml.v3"
"io"
"os"
"path/filepath"
)

// ConfigParser is a general configuration parser interface, used to agree on the implementation of different types of parsers
type ConfigParser interface {
Parse(configContent []byte) (*StateMachineObject, error)
}

type StateMachineConfigParser struct{}

func NewStateMachineConfigParser() *StateMachineConfigParser {
return &StateMachineConfigParser{}
}

func (p *StateMachineConfigParser) checkConfigFile(configFilePath string) error {
_, err := os.Stat(configFilePath)
if os.IsNotExist(err) {
return fmt.Errorf("config file %s does not exist: %w", configFilePath, err)
}
if err != nil {
return fmt.Errorf("failed to access config file %s: %w", configFilePath, err)
}
return nil
}

func (p *StateMachineConfigParser) readFile(configFilePath string) ([]byte, error) {
file, _ := os.Open(configFilePath)
defer func(file *os.File) {
_ = file.Close()
}(file)

var buf bytes.Buffer
_, err := io.Copy(&buf, file)
if err != nil {
return nil, fmt.Errorf("failed to read config file %s: %w", configFilePath, err)
}

return buf.Bytes(), nil
}

func (p *StateMachineConfigParser) getParser(configFilePath string) (ConfigParser, error) {
fileExt := filepath.Ext(configFilePath)
// check the file extension, compatible with some illegal but possible situations
switch fileExt {
case ".json", ".JSON":
return NewJSONConfigParser(), nil
case ".yaml", ".yml", ".YAML", ".YML":
return NewYAMLConfigParser(), nil
default:
return nil, fmt.Errorf("unsupported config file format: %s", fileExt)
}
}

func (p *StateMachineConfigParser) Parse(configFilePath string) (*StateMachineObject, error) {
if err := p.checkConfigFile(configFilePath); err != nil {
return nil, err
}

configContent, err := p.readFile(configFilePath)
if err != nil {
return nil, err
}

parser, err := p.getParser(configFilePath)
if err != nil {
return nil, err
}

return parser.Parse(configContent)
}

type JSONConfigParser struct{}

func NewJSONConfigParser() *JSONConfigParser {
Expand Down Expand Up @@ -133,6 +69,59 @@ func (p *YAMLConfigParser) Parse(configContent []byte) (*StateMachineObject, err
return &stateMachineObject, nil
}

type StateMachineConfigParser struct{}

func NewStateMachineConfigParser() *StateMachineConfigParser {
return &StateMachineConfigParser{}
}

func (p *StateMachineConfigParser) CheckConfigFile(filePath string) error {
_, err := os.Stat(filePath)
if os.IsNotExist(err) {
return fmt.Errorf("config file %s does not exist: %w", filePath, err)
}
if err != nil {
return fmt.Errorf("failed to access config file %s: %w", filePath, err)
}
return nil
}

func (p *StateMachineConfigParser) ReadConfigFile(configFilePath string) ([]byte, error) {
file, _ := os.Open(configFilePath)
defer func(file *os.File) {
_ = file.Close()
}(file)

var buf bytes.Buffer
_, err := io.Copy(&buf, file)
if err != nil {
return nil, fmt.Errorf("failed to read config file %s: %w", configFilePath, err)
}

return buf.Bytes(), nil
}

func (p *StateMachineConfigParser) getParser(content []byte) (ConfigParser, error) {
var obj interface{}
if err := json.Unmarshal(content, &obj); err == nil {
return NewJSONConfigParser(), nil
}
if err := yaml.Unmarshal(content, &obj); err == nil {
return NewYAMLConfigParser(), nil
}

return nil, fmt.Errorf("unsupported config file format")
}

func (p *StateMachineConfigParser) Parse(content []byte) (*StateMachineObject, error) {
parser, err := p.getParser(content)
if err != nil {
return nil, err
}

return parser.Parse(content)
}

type StateMachineObject struct {
Name string `json:"Name" yaml:"Name"`
Comment string `json:"Comment" yaml:"Comment"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ func TestStateMachineConfigParser_Parse(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
object, err := parser.Parse(tt.configFilePath)
content, err := parser.ReadConfigFile(tt.configFilePath)
if err != nil {
t.Error("parse fail: " + err.Error())
}
object, err := parser.Parse(content)
if err != nil {
t.Error("parse fail: " + err.Error())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func (stateMachineParser JSONStateMachineParser) GetType() string {
return "JSON"
}

func (stateMachineParser JSONStateMachineParser) Parse(configFilePath string) (statelang.StateMachine, error) {
stateMachineJsonObject, err := NewStateMachineConfigParser().Parse(configFilePath)
func (stateMachineParser JSONStateMachineParser) Parse(content string) (statelang.StateMachine, error) {
stateMachineJsonObject, err := NewStateMachineConfigParser().Parse([]byte(content))
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 7929470

Please sign in to comment.