-
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 all 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,147 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package parser | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"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 { | ||
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 { | ||
_, 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 { | ||
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"` | ||
} |
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 下面所有的文件都检查一下