-
Notifications
You must be signed in to change notification settings - Fork 286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[to-reply] feature: support saga multi type config #741
base: feature/saga
Are you sure you want to change the base?
Changes from 5 commits
3b98ffd
9801d4b
2115edb
fa1d787
6be422f
f981e95
c9a8fe5
5e76a6c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package parser | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"gopkg.in/yaml.v3" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
type ConfigParser interface { | ||
FinnTew marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Parse(configContent []byte) (*StateMachineObject, error) | ||
} | ||
|
||
type StateMachineConfigParser struct{} | ||
|
||
func NewStateMachineConfigParser() *StateMachineConfigParser { | ||
return &StateMachineConfigParser{} | ||
} | ||
|
||
func (p *StateMachineConfigParser) checkConfigFile(configFilePath string) error { | ||
if _, err := os.Stat(configFilePath); err != nil { | ||
if os.IsNotExist(err) { | ||
FinnTew marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return fmt.Errorf("config file %s does not exist: %w", configFilePath, err) | ||
} | ||
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) | ||
switch fileExt { | ||
case ".json": | ||
FinnTew marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return NewJSONConfigParser(), nil | ||
case ".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 { | ||
return &JSONConfigParser{} | ||
} | ||
|
||
func (p *JSONConfigParser) Parse(configContent []byte) (*StateMachineObject, error) { | ||
if configContent == nil || len(configContent) == 0 { | ||
return nil, fmt.Errorf("empty JSON config content") | ||
} | ||
|
||
var stateMachineObject StateMachineObject | ||
if err := json.Unmarshal(configContent, &stateMachineObject); err != nil { | ||
return nil, fmt.Errorf("failed to parse JSON config content: %w", err) | ||
} | ||
|
||
return &stateMachineObject, nil | ||
} | ||
|
||
type YAMLConfigParser struct{} | ||
|
||
func NewYAMLConfigParser() *YAMLConfigParser { | ||
return &YAMLConfigParser{} | ||
} | ||
|
||
func (p *YAMLConfigParser) Parse(configContent []byte) (*StateMachineObject, error) { | ||
if configContent == nil || len(configContent) == 0 { | ||
return nil, fmt.Errorf("empty YAML config content") | ||
} | ||
|
||
var stateMachineObject StateMachineObject | ||
if err := yaml.Unmarshal(configContent, &stateMachineObject); err != nil { | ||
return nil, fmt.Errorf("failed to parse YAML config content: %w", err) | ||
} | ||
|
||
return &stateMachineObject, nil | ||
} | ||
|
||
type StateMachineObject struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a stateMachineJsonObject struct, maybe we can unify them? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the StateMachineObject here is the original StateMachineJsonObject. Because the products of the two formats are basically consistent, I moved the definition of the structure to config parser and added a tag to be compatible with the two formats. |
||
Name string `json:"Name" yaml:"Name"` | ||
Comment string `json:"Comment" yaml:"Comment"` | ||
Version string `json:"Version" yaml:"Version"` | ||
StartState string `json:"StartState" yaml:"StartState"` | ||
RecoverStrategy string `json:"RecoverStrategy" yaml:"RecoverStrategy"` | ||
Persist bool `json:"IsPersist" yaml:"IsPersist"` | ||
RetryPersistModeUpdate bool `json:"IsRetryPersistModeUpdate" yaml:"IsRetryPersistModeUpdate"` | ||
CompensatePersistModeUpdate bool `json:"IsCompensatePersistModeUpdate" yaml:"IsCompensatePersistModeUpdate"` | ||
Type string `json:"Type" yaml:"Type"` | ||
States map[string]interface{} `json:"States" yaml:"States"` | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package parser | ||
|
||
import "testing" | ||
|
||
func TestStateMachineConfigParser_Parse(t *testing.T) { | ||
parser := NewStateMachineConfigParser() | ||
|
||
tests := []struct { | ||
name string | ||
configFilePath string | ||
}{ | ||
{ | ||
name: "JSON Simple 1", | ||
configFilePath: "../../../../../testdata/saga/statelang/simple_statelang_with_choice.json", | ||
}, | ||
{ | ||
name: "JSON Simple 2", | ||
configFilePath: "../../../../../testdata/saga/statelang/simple_statemachine.json", | ||
}, | ||
{ | ||
name: "JSON Simple 3", | ||
configFilePath: "../../../../../testdata/saga/statelang/state_machine_new_designer.json", | ||
}, | ||
{ | ||
name: "YAML Simple 1", | ||
configFilePath: "../../../../../testdata/saga/statelang/simple_statelang_with_choice.yaml", | ||
}, | ||
{ | ||
name: "YAML Simple 2", | ||
configFilePath: "../../../../../testdata/saga/statelang/simple_statemachine.yaml", | ||
}, | ||
{ | ||
name: "YAML Simple 3", | ||
configFilePath: "../../../../../testdata/saga/statelang/state_machine_new_designer.yaml", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
FinnTew marked this conversation as resolved.
Show resolved
Hide resolved
|
||
t.Run(tt.name, func(t *testing.T) { | ||
_, err := parser.Parse(tt.configFilePath) | ||
if err != nil { | ||
t.Error("parse fail: " + err.Error()) | ||
} | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
package parser | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/pkg/errors" | ||
"github.com/seata/seata-go/pkg/saga/statemachine/constant" | ||
"github.com/seata/seata-go/pkg/saga/statemachine/statelang" | ||
|
@@ -22,10 +21,8 @@ func (stateMachineParser JSONStateMachineParser) GetType() string { | |
return "JSON" | ||
} | ||
|
||
func (stateMachineParser JSONStateMachineParser) Parse(content string) (statelang.StateMachine, error) { | ||
var stateMachineJsonObject StateMachineJsonObject | ||
|
||
err := json.Unmarshal([]byte(content), &stateMachineJsonObject) | ||
func (stateMachineParser JSONStateMachineParser) Parse(configFilePath string) (statelang.StateMachine, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make interface and the impl has same param |
||
stateMachineJsonObject, err := NewStateMachineConfigParser().Parse(configFilePath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -115,16 +112,3 @@ func (stateMachineParser JSONStateMachineParser) isTaskState(stateType string) b | |
} | ||
return false | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这些不需要用了吗 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. StateMachineJsonObject 改为了 statemachine_config_parser 中的 StateMachineObject,给 json 和 yaml 共用,两种格式解析后的结构是一致的,这里的 Parser 实际是和配置文件格式无关的,所以感觉放在 config parser 中更合理一些 |
||
type StateMachineJsonObject struct { | ||
Name string `json:"Name"` | ||
Comment string `json:"Comment"` | ||
Version string `json:"Version"` | ||
StartState string `json:"StartState"` | ||
RecoverStrategy string `json:"RecoverStrategy"` | ||
Persist bool `json:"IsPersist"` | ||
RetryPersistModeUpdate bool `json:"IsRetryPersistModeUpdate"` | ||
CompensatePersistModeUpdate bool `json:"IsCompensatePersistModeUpdate"` | ||
Type string `json:"Type"` | ||
States map[string]interface{} `json:"States"` | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,86 @@ | ||
package parser | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestParseChoice(t *testing.T) { | ||
filePath := "../../../../../testdata/saga/statelang/simple_statelang_with_choice.json" | ||
fileContent, err := os.ReadFile(filePath) | ||
if err != nil { | ||
t.Error("parse fail: " + err.Error()) | ||
return | ||
parser := NewJSONStateMachineParser() | ||
|
||
tests := []struct { | ||
name string | ||
configFilePath string | ||
}{ | ||
{ | ||
name: "JSON Simple: StateLang With Choice", | ||
configFilePath: "../../../../../testdata/saga/statelang/simple_statelang_with_choice.json", | ||
}, | ||
{ | ||
name: "YAML Simple: StateLang With Choice", | ||
configFilePath: "../../../../../testdata/saga/statelang/simple_statelang_with_choice.yaml", | ||
}, | ||
} | ||
_, err = NewJSONStateMachineParser().Parse(string(fileContent)) | ||
if err != nil { | ||
t.Error("parse fail: " + err.Error()) | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
_, err := parser.Parse(tt.configFilePath) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里对字段的值进行断言? |
||
if err != nil { | ||
t.Error("parse fail: " + err.Error()) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestParseServiceTaskForSimpleStateMachine(t *testing.T) { | ||
filePath := "../../../../../testdata/saga/statelang/simple_statemachine.json" | ||
fileContent, err := os.ReadFile(filePath) | ||
if err != nil { | ||
t.Error("parse fail: " + err.Error()) | ||
return | ||
parser := NewJSONStateMachineParser() | ||
|
||
tests := []struct { | ||
name string | ||
configFilePath string | ||
}{ | ||
{ | ||
name: "JSON Simple: StateMachine", | ||
configFilePath: "../../../../../testdata/saga/statelang/simple_statemachine.json", | ||
}, | ||
{ | ||
name: "YAML Simple: StateMachine", | ||
configFilePath: "../../../../../testdata/saga/statelang/simple_statemachine.yaml", | ||
}, | ||
} | ||
_, err = NewJSONStateMachineParser().Parse(string(fileContent)) | ||
if err != nil { | ||
t.Error("parse fail: " + err.Error()) | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
_, err := parser.Parse(tt.configFilePath) | ||
if err != nil { | ||
t.Error("parse fail: " + err.Error()) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestParseServiceTaskForNewDesigner(t *testing.T) { | ||
filePath := "../../../../../testdata/saga/statelang/state_machine_new_designer.json" | ||
fileContent, err := os.ReadFile(filePath) | ||
if err != nil { | ||
t.Error("parse fail: " + err.Error()) | ||
return | ||
parser := NewJSONStateMachineParser() | ||
|
||
tests := []struct { | ||
name string | ||
configFilePath string | ||
}{ | ||
{ | ||
name: "JSON Simple: StateMachine New Designer", | ||
configFilePath: "../../../../../testdata/saga/statelang/state_machine_new_designer.json", | ||
}, | ||
{ | ||
name: "YAML Simple: StateMachine New Designer", | ||
configFilePath: "../../../../../testdata/saga/statelang/state_machine_new_designer.yaml", | ||
}, | ||
} | ||
_, err = NewJSONStateMachineParser().Parse(string(fileContent)) | ||
if err != nil { | ||
t.Error("parse fail: " + err.Error()) | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
_, err := parser.Parse(tt.configFilePath) | ||
if err != nil { | ||
t.Error("parse fail: " + err.Error()) | ||
} | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -148,18 +148,23 @@ func (b BaseStateParser) GetIntOrDefault(stateName string, stateMap map[string]i | |
return defaultValue, nil | ||
} | ||
|
||
// just use float64 to convert, json reader will read all number as float64 | ||
valueAsFloat64, ok := value.(float64) | ||
if !ok { | ||
// use float64 conversion when the configuration file is json, and use int conversion when the configuration file is yaml | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is it designed this way There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because "encoding/json" package will convert all numerical values into float64 types when parsing json, but" yaml.v3" package will correspond to float64 and int types when parsing YAML, and the modification here is mainly to be compatible with the two formats, otherwise some errors will occur. |
||
valueAsFloat64, okToFloat64 := value.(float64) | ||
valueAsInt, okToInt := value.(int) | ||
if !okToFloat64 && !okToInt { | ||
return defaultValue, errors.New("State [" + stateName + "] " + key + " illegal, required int") | ||
} | ||
|
||
floatStr := strconv.FormatFloat(valueAsFloat64, 'f', -1, 64) | ||
if strings.Contains(floatStr, ".") { | ||
return defaultValue, errors.New("State [" + stateName + "] " + key + " illegal, required int") | ||
if okToFloat64 { | ||
floatStr := strconv.FormatFloat(valueAsFloat64, 'f', -1, 64) | ||
if strings.Contains(floatStr, ".") { | ||
return defaultValue, errors.New("State [" + stateName + "] " + key + " illegal, required int") | ||
} | ||
|
||
return int(valueAsFloat64), nil | ||
} | ||
|
||
return int(valueAsFloat64), nil | ||
return valueAsInt, nil | ||
} | ||
|
||
func (b BaseStateParser) GetFloat64OrDefault(stateName string, stateMap map[string]interface{}, key string, defaultValue float64) (float64, error) { | ||
|
@@ -169,11 +174,17 @@ func (b BaseStateParser) GetFloat64OrDefault(stateName string, stateMap map[stri | |
return defaultValue, nil | ||
} | ||
|
||
valueAsFloat64, ok := value.(float64) | ||
if !ok { | ||
// use float64 conversion when the configuration file is json, and use int conversion when the configuration file is yaml | ||
valueAsFloat64, okToFloat64 := value.(float64) | ||
valueAsInt, okToInt := value.(int) | ||
if !okToFloat64 && !okToInt { | ||
return defaultValue, errors.New("State [" + stateName + "] " + key + " illegal, required float64") | ||
} | ||
return valueAsFloat64, nil | ||
|
||
if okToFloat64 { | ||
return valueAsFloat64, nil | ||
} | ||
return float64(valueAsInt), nil | ||
} | ||
|
||
type StateParserFactory interface { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
要带上apache的License 头,参考别的文件copy一个,或者参考根目录的 goimports.sh 下面所有的文件都检查一下