wt-better commented on code in PR #650:
URL: 
https://github.com/apache/incubator-seata-go/pull/650#discussion_r1464359298


##########
pkg/saga/statemachine/statelang/parser/statemachine_parser.go:
##########
@@ -52,22 +41,118 @@ func (b BaseStateParser) GetString(stateName string, 
stateMap map[string]interfa
        return valueAsString, nil
 }
 
-func (b BaseStateParser) GetSlice(stateName string, stateMap 
map[string]interface{}, key string) ([]interface{}, error) {
+func (b BaseStateParser) GetStringOrDefault(stateMap map[string]interface{}, 
key string, defaultValue string) string {
        value := stateMap[key]
+       if value == nil {
+               return defaultValue
+       }
+
+       valueAsString, ok := value.(string)
+       if !ok {
+               return defaultValue
+       }
+       return valueAsString
+}
 
+func (b BaseStateParser) GetSlice(stateName string, stateMap 
map[string]interface{}, key string) ([]interface{}, error) {
+       value := stateMap[key]
        if value == nil {
                var result []interface{}
                return result, errors.New("State [" + stateName + "] " + key + 
" not exist")
        }
 
        valueAsSlice, ok := value.([]interface{})
        if !ok {
-               var result []interface{}
-               return result, errors.New("State [" + stateName + "] " + key + 
" illegal, required slice")
+               var slice []interface{}
+               return slice, errors.New("State [" + stateName + "] " + key + " 
illegal, required []interface{}")
        }
        return valueAsSlice, nil
 }
 
+func (b BaseStateParser) GetSliceOrDefault(stateMap map[string]interface{}, 
key string, defaultValue []interface{}) []interface{} {
+       value := stateMap[key]
+
+       if value == nil {
+               return defaultValue
+       }
+
+       valueAsSlice, ok := value.([]interface{})
+       if !ok {
+               return defaultValue
+       }
+       return valueAsSlice
+}
+
+func (b BaseStateParser) GetMapOrDefault(stateMap map[string]interface{}, key 
string, defaultValue map[string]interface{}) map[string]interface{} {
+       value := stateMap[key]
+
+       if value == nil {
+               return defaultValue
+       }
+
+       valueAsMap, ok := value.(map[string]interface{})
+       if !ok {
+               return defaultValue
+       }
+       return valueAsMap
+}
+
+func (b BaseStateParser) GetBool(stateName string, stateMap 
map[string]interface{}, key string) (bool, error) {

Review Comment:
   GetBoolOrFalse rename to GetBoolOrDefault , keep unify.
   
   GetBoolOrDefault can be reduce duplicate code, other method also.



##########
pkg/saga/statemachine/statelang/parser/statemachine_parser.go:
##########
@@ -52,22 +41,118 @@ func (b BaseStateParser) GetString(stateName string, 
stateMap map[string]interfa
        return valueAsString, nil
 }
 
-func (b BaseStateParser) GetSlice(stateName string, stateMap 
map[string]interface{}, key string) ([]interface{}, error) {
+func (b BaseStateParser) GetStringOrDefault(stateMap map[string]interface{}, 
key string, defaultValue string) string {
        value := stateMap[key]
+       if value == nil {
+               return defaultValue
+       }
+
+       valueAsString, ok := value.(string)
+       if !ok {

Review Comment:
   if value convert err, example need: int , but map , throw err or better



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@seata.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@seata.apache.org
For additional commands, e-mail: dev-h...@seata.apache.org

Reply via email to