ast_expr_builder.go 1.1 KB
package astexpr

import (
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
	"strconv"
	"strings"
)

func NewNumberExprAST(val string) NumberExprAST {
	num, _ := strconv.ParseFloat(val, 64)
	return NumberExprAST{
		ExprType: TypeNumberExprAST,
		Val:      num,
		Str:      val,
	}
}

func NewValueExprAST(val string) ValueExprAST {
	return ValueExprAST{
		ExprType: TypeValueExprAST,
		Val:      val,
		Str:      val,
	}
}

func NewFieldExprAST(val string) FieldExprAST {
	words := strings.Split(val, ".")
	table := words[0]
	filed := words[0]
	if len(words) > 0 {
		filed = words[1]
	}
	return FieldExprAST{
		ExprType: TypeFieldExprAST,
		Str:      val,
		Field: &domain.TableField{
			FieldName:    filed,
			FieldSqlName: filed,
			TableName:    table,
		},
	}
}

func NewBinaryExprAST(op string, lhs ExprAST, rhs ExprAST) BinaryExprAST {
	return BinaryExprAST{
		ExprType: TypeBinaryExprAST,
		Op:       op,
		Lhs:      lhs,
		Rhs:      rhs,
	}
}

func NewFunCallerExprAST(name string, args ...ExprAST) FunCallerExprAST {
	return FunCallerExprAST{
		ExprType: TypeFunCallerExprAST,
		Name:     name,
		Args:     args,
	}
}