github.com/team-ide/go-dialect@v1.9.20/dialect/statement_invoker.go (about)

     1  package dialect
     2  
     3  import (
     4  	"errors"
     5  	"reflect"
     6  	"strconv"
     7  )
     8  
     9  func FormatStatements(statements []Statement, statementContext *StatementContext) (text string, err error) {
    10  
    11  	var oneText string
    12  	for _, one := range statements {
    13  		oneText, err = FormatStatement(one, statementContext)
    14  		if err != nil {
    15  			return
    16  		}
    17  		text += oneText
    18  	}
    19  	return
    20  }
    21  
    22  func FormatStatement(statement_ Statement, statementContext *StatementContext) (text string, err error) {
    23  
    24  	switch statement := statement_.(type) {
    25  	case *IgnorableStatement:
    26  		text, err = statement.Format(statementContext)
    27  		break
    28  	default:
    29  		text, err = statement.Format(statementContext)
    30  		break
    31  	}
    32  	return
    33  }
    34  
    35  func (this_ *AbstractStatement) Format(statementContext *StatementContext) (text string, err error) {
    36  	text += this_.Content
    37  
    38  	childrenText, err := FormatStatements(this_.Children, statementContext)
    39  	if err != nil {
    40  		return
    41  	}
    42  	text += childrenText
    43  	return
    44  }
    45  
    46  func (this_ *IgnorableStatement) Format(statementContext *StatementContext) (text string, err error) {
    47  
    48  	findValue, err := StatementsFindValue(this_.Children, statementContext)
    49  	if err != nil {
    50  		return
    51  	}
    52  	if !findValue {
    53  		return
    54  	}
    55  	childrenText, err := FormatStatements(this_.Children, statementContext)
    56  	if err != nil {
    57  		return
    58  	}
    59  	text += childrenText
    60  	return
    61  }
    62  
    63  func isTrue(value interface{}) bool {
    64  	res, _ := strconv.ParseBool(GetStringValue(value))
    65  	return res
    66  }
    67  
    68  func (this_ *ElseIfStatement) Format(statementContext *StatementContext) (text string, err error) {
    69  	childrenText, err := FormatStatements(this_.Children, statementContext)
    70  	if err != nil {
    71  		return
    72  	}
    73  	text += childrenText
    74  	return
    75  }
    76  
    77  func (this_ *ElseStatement) Format(statementContext *StatementContext) (text string, err error) {
    78  	childrenText, err := FormatStatements(this_.Children, statementContext)
    79  	if err != nil {
    80  		return
    81  	}
    82  	text += childrenText
    83  	return
    84  }
    85  
    86  func (this_ *IfStatement) Format(statementContext *StatementContext) (text string, err error) {
    87  	//text += this_.Content
    88  
    89  	if this_.ConditionExpression == nil {
    90  		err = errors.New("if statement expression is null")
    91  		return
    92  	}
    93  	var invoked bool
    94  	var checkOk interface{}
    95  	var oneText string
    96  	checkOk, err = this_.ConditionExpression.GetValue(statementContext)
    97  	if err != nil {
    98  		return
    99  	}
   100  	if isTrue(checkOk) {
   101  		invoked = true
   102  		for _, one := range this_.Children {
   103  			oneText, err = one.Format(statementContext)
   104  			if err != nil {
   105  				return
   106  			}
   107  			text += oneText
   108  		}
   109  	}
   110  	if !invoked {
   111  		for _, one := range this_.ElseIfs {
   112  			if one.ConditionExpression == nil {
   113  				err = errors.New("else if statement expression is null")
   114  				return
   115  			}
   116  			checkOk, err = this_.ConditionExpression.GetValue(statementContext)
   117  			if err != nil {
   118  				return
   119  			}
   120  			if !isTrue(checkOk) {
   121  				continue
   122  			}
   123  			invoked = true
   124  			oneText, err = one.Format(statementContext)
   125  			if err != nil {
   126  				return
   127  			}
   128  			text += oneText
   129  			break
   130  		}
   131  		if !invoked {
   132  			if this_.Else != nil {
   133  				oneText, err = this_.Else.Format(statementContext)
   134  				if err != nil {
   135  					return
   136  				}
   137  				text += oneText
   138  			}
   139  		}
   140  	}
   141  	return
   142  }
   143  
   144  func (this_ *ExpressionStatement) GetValue(statementContext *StatementContext) (res interface{}, err error) {
   145  	//text += this_.Content
   146  
   147  	var data interface{}
   148  	for _, one := range this_.Children {
   149  		data, err = GetStatementValue(one, statementContext)
   150  		if err != nil {
   151  			return
   152  		}
   153  	}
   154  	res = data
   155  	return
   156  }
   157  
   158  func GetStatementValue(statement_ Statement, statementContext *StatementContext) (res interface{}, err error) {
   159  
   160  	var data interface{}
   161  
   162  	switch statement := statement_.(type) {
   163  	case *ExpressionFuncStatement:
   164  		data, err = statement.GetValue(statementContext)
   165  		break
   166  	case *ExpressionIdentifierStatement:
   167  		data, err = statement.GetValue(statementContext)
   168  		break
   169  	case *ExpressionStringStatement:
   170  		data, err = statement.GetValue(statementContext)
   171  		break
   172  	case *ExpressionNumberStatement:
   173  		data, err = statement.GetValue(statementContext)
   174  		break
   175  	case *ExpressionBracketsStatement:
   176  		data, err = statement.GetValue(statementContext)
   177  		break
   178  	default:
   179  		err = errors.New("Statement type [" + reflect.TypeOf(statement).String() + "] not support")
   180  		return
   181  	}
   182  	if err != nil {
   183  		return
   184  	}
   185  	res = data
   186  	return
   187  }
   188  
   189  func StatementsFindValue(statements []Statement, statementContext *StatementContext) (findValue bool, err error) {
   190  	var data interface{}
   191  	for _, one := range statements {
   192  		switch statement := one.(type) {
   193  		//case *ExpressionFuncStatement:
   194  		//	data, err = statement.GetValue(statementContext)
   195  		//	break
   196  		case *ExpressionIdentifierStatement:
   197  			data, err = statement.GetValue(statementContext)
   198  			break
   199  		case *ExpressionStringStatement:
   200  			data, err = statement.GetValue(statementContext)
   201  			break
   202  		case *ExpressionNumberStatement:
   203  			data, err = statement.GetValue(statementContext)
   204  			break
   205  		case *ExpressionBracketsStatement:
   206  			data, err = statement.GetValue(statementContext)
   207  			break
   208  		}
   209  		if err != nil {
   210  			return
   211  		}
   212  		if data != nil && data != "" {
   213  			findValue = true
   214  			break
   215  		}
   216  		findValue, err = StatementsFindValue(*one.GetChildren(), statementContext)
   217  
   218  		if err != nil {
   219  			return
   220  		}
   221  		if findValue {
   222  			break
   223  		}
   224  	}
   225  
   226  	return
   227  }
   228  
   229  func (this_ *ExpressionIdentifierStatement) Format(statementContext *StatementContext) (text string, err error) {
   230  	value, err := this_.GetValue(statementContext)
   231  	if err != nil {
   232  		return
   233  	}
   234  	text = GetStringValue(value)
   235  	return
   236  }
   237  
   238  func (this_ *ExpressionIdentifierStatement) GetValue(statementContext *StatementContext) (res interface{}, err error) {
   239  	res, ok := statementContext.GetData(this_.Identifier)
   240  	if !ok {
   241  		err = errors.New("identifier [" + this_.Identifier + "] not define")
   242  		return
   243  	}
   244  	return
   245  }
   246  
   247  func (this_ *ExpressionStringStatement) Format(statementContext *StatementContext) (text string, err error) {
   248  	value, err := this_.GetValue(statementContext)
   249  	if err != nil {
   250  		return
   251  	}
   252  	text = GetStringValue(value)
   253  	return
   254  }
   255  
   256  func (this_ *ExpressionStringStatement) GetValue(statementContext *StatementContext) (res interface{}, err error) {
   257  	res = this_.Value
   258  	return
   259  }
   260  
   261  func (this_ *ExpressionNumberStatement) Format(statementContext *StatementContext) (text string, err error) {
   262  	value, err := this_.GetValue(statementContext)
   263  	if err != nil {
   264  		return
   265  	}
   266  	text = GetStringValue(value)
   267  	return
   268  }
   269  
   270  func (this_ *ExpressionNumberStatement) GetValue(statementContext *StatementContext) (res interface{}, err error) {
   271  	res = this_.Value
   272  	return
   273  }
   274  
   275  func (this_ *ExpressionFuncStatement) Format(statementContext *StatementContext) (text string, err error) {
   276  	value, err := this_.GetValue(statementContext)
   277  	if err != nil {
   278  		return
   279  	}
   280  	text = GetStringValue(value)
   281  	return
   282  }
   283  
   284  func (this_ *ExpressionFuncStatement) GetValue(statementContext *StatementContext) (res interface{}, err error) {
   285  	method, ok := statementContext.GetMethod(this_.Func)
   286  	if !ok {
   287  		err = errors.New("func [" + this_.Func + "] not define")
   288  		return
   289  	}
   290  	var values []interface{}
   291  	var v interface{}
   292  	for _, arg := range this_.Children {
   293  		v, err = GetStatementValue(arg, statementContext)
   294  		if err != nil {
   295  			return
   296  		}
   297  		values = append(values, v)
   298  	}
   299  	methodResults, err := method.Call(values)
   300  	if err != nil {
   301  		return
   302  	}
   303  	if len(methodResults) > 0 {
   304  		res = methodResults[0]
   305  	}
   306  	return
   307  }
   308  
   309  func (this_ *ExpressionBracketsStatement) GetValue(statementContext *StatementContext) (res interface{}, err error) {
   310  
   311  	return
   312  }