예제 #1
0
func DoRunCalcFunc(fname string, unit *CalcUnit, value *simplejson.Json, dataMap map[string]interface{}) (interface{}, error) {
	unit.Oper, _ = value.Get(FItem_Function).String()
	reInitCalcUnit(unit)
	genSubCmdUnit(value, unit)
	//fmt.Printf("function unit:%v\n", unit)

	//traverse the sub command
	for _, subCmd := range unit.SubCmd {
		subUnit := NewCalcUnit(subCmd)

		subVal, _ := DoRunCalcUnit(fname, subUnit, dataMap)
		unit.SubResult = append(unit.SubResult, subVal)
	}

	// calc the result
	var cmdPara interface{}
	switch unit.Oper {
	case ucmd.FNAME_GDEF:
		cmdPara = unit.Default

		//cmdPara0, _ := cmdPara.(int64)
		//cmdPara1, _ := cmdPara.(float64)
		//fmt.Printf("cmdPara=%v, cmdParaf=%v\n", cmdPara0, cmdPara1)
	default:
		cmdPara = genCalcPara(unit.Oper, unit.SubResult)
		//fmt.Println("cmdPara=", cmdPara)
	}
	//fmt.Printf("Oper=%v, SubResult=%v, Digital=%v, cmdPara=%v\n", unit.Oper, unit.SubResult, unit.Digital, cmdPara)
	result := ucmd.Run(unit.Oper, cmdPara, unit.Digital)
	//fmt.Println("result=", result)
	return result, nil
}
예제 #2
0
func genSubCmdUnit(v *simplejson.Json, data *CalcUnit) {
	// generate the items & digital
	switch data.Oper {
	case ucmd.FNAME_DIV:
		item, _ := v.Get(FItem_Dividend).String()
		data.SubCmd = append(data.SubCmd, item)

		item, _ = v.Get(FItem_Divisor).String()
		data.SubCmd = append(data.SubCmd, item)
	case ucmd.FNAME_HL:
		item, _ := v.Get(FItem_H).String()
		data.SubCmd = append(data.SubCmd, item)

		item, _ = v.Get(FItem_L).String()
		data.SubCmd = append(data.SubCmd, item)

		item, _ = v.Get(FItem_N).String()
		data.SubCmd = append(data.SubCmd, item)
	case ucmd.FNAME_GDEF:
		var err error
		if data.Default, err = v.Get(FItem_Default).String(); err != nil {
			if data.Default, err = v.Get(FItem_Default).Float64(); err != nil {
				data.Default, _ = v.Get(FItem_Default).Int64()
			}
		}
		//fmt.Println("default value:", data.Default)
	default:
		// normal sub command
		data.SubCmd, _ = v.Get(FItem_Items).StringArray()
	}
	data.Digital = v.Get(FItem_Digit).MustFloat64()
}