Example #1
0
// ----------------------------------------------------------------------------------------------------------------------------------------
func FxReadJson(callNo int, pt *Parse2Type, Context *eval.ContextType, curTree *MtType) (err error) {
	fmt.Printf("Fx_ReadJson Called, %d\n", callNo)
	// {% read_json ID "file_name.json" %} (config allows url:// not just file"

	if callNo == 0 {

		if !curTree.NOptions(2) {
			// xyzzy } else if !curTree.IsId(0) {		// -- implement to check that [0] is an ID
		} else {
			id := curTree.SVal[0]
			path := curTree.SVal[1]
			// path = path[0 : len(path)-1]
			err = nil
			// var jsonData map[string]SQLOne
			var file []byte
			file, err = ioutil.ReadFile(path)
			if err != nil {
				fmt.Printf("Error(10014): %v, %s, Config File:%s\n", err, com.LF(), path)
				return
			}
			file = []byte(strings.Replace(string(file), "\t", " ", -1)) // file = []byte(ReplaceString(string(file), "^[ \t][ \t]*//.*$", ""))

			// Check beginning of file if "{" then MapOf, if "[" Array, else look at single value
			if strings.HasPrefix(string(file), "{") {

				jsonData := make(map[string]interface{})

				err = json.Unmarshal(file, &jsonData)
				if err != nil {
					fmt.Printf("Error(10012): %v, %s, Config File:%s\n", err, com.LF(), path)
					return
				}

				Context.SetInContext(id, eval.CtxType_MapOf, jsonData)

			} else {

				jsonData := make([]interface{}, 0, 100)

				err = json.Unmarshal(file, &jsonData)
				if err != nil {
					fmt.Printf("Error(10012): %v, %s, Config File:%s\n", err, com.LF(), path)
					return
				}

				Context.SetInContext(id, eval.CtxType_ArrayOf, jsonData)

			}

		}
	}

	return
}
Example #2
0
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
func FxSet_context(callNo int, pt *Parse2Type, Context *eval.ContextType, curTree *MtType) (err error) {
	fmt.Printf("SetContext Called, %d\n", callNo)
	if callNo == 10 {
		fmt.Printf("EvalExpr(context,0,0)=%v\n", curTree.EvalExpr(Context, 0, 0))
		if !curTree.MoreThan(2) {
		} else {
			id := curTree.SVal[0]
			val := curTree.SVal[1]
			if val == "true" || val == "TRUE" {
				Context.SetInContext(id, eval.CtxType_Bool, true) // xyzzy - type may not be correct
			} else {
				Context.SetInContext(id, eval.CtxType_Bool, false) // xyzzy - type may not be correct
			}
		}
	}
	return
}
Example #3
0
func FxCycle(callNo int, pt *Parse2Type, Context *eval.ContextType, curTree *MtType) (err error) {
	fmt.Printf("Cycle Called, %d\n", callNo)
	if callNo == 0 {
		if !curTree.MoreThan(2) {
		} else {
			x := &FxCycleDataType{CurPos: 0}
			ne := 0                                                    // Number at end we have processed.
			no := len(curTree.SVal)                                    // Number of Options
			if curTree.MoreThan(1) && curTree.SVal[no-1] == "silent" { // extract silent
				x.Silent = true
				ne = 1
			}
			if curTree.MoreThan(2+ne) && curTree.SVal[no-1-ne] == "as" { // extract as <id>
				x.As = curTree.SVal[no-ne-1]
				ne += 2
			}
			if curTree.MoreThan(2 + ne) {
				x.Opts = curTree.EvalVars(curTree.SVal[0 : no-ne]) // eval options -> values
				x.CurPos = 0                                       // establish data and position
			}
			curTree.DataVal = x
		}
	}
	if callNo == 10 {
		x := curTree.DataVal.(*FxCycleDataType)
		if x.Silent {
			curTree.HTML_Output = ""
		} else {
			curTree.HTML_Output = x.Opts[x.CurPos] // Return Value
		}
		if x.As != "" {
			Context.SetInContext(x.As, eval.CtxType_Str, x.Opts[x.CurPos]) // xyzzy - type may not be correct
		}
		x.CurPos = (x.CurPos + 1) % len(x.Opts) // Increment Postion Mod Length
		curTree.DataVal = x
	}
	return
}
Example #4
0
func FxFor(callNo int, pt *Parse2Type, Context *eval.ContextType, curTree *MtType) (err error) {
	fmt.Printf("Fx_For Called, %d\n", callNo)
	fmt.Printf("---------------------------------------------------------------------------- for tree -------------------------------------------------------------------------\n")
	if false {
		fmt.Printf("%s\n\n", com.SVarI(curTree))
	} else {
		curTree.DumpMtType(os.Stdout, 0, 0)
	}

	tmpMt := func(ss []*MtType) (rv *MtType) {
		rv = &MtType{
			NodeType: gen.Tok_Tree_List,
			List:     make([]*MtType, 0, len(ss)),
			LineNo:   ss[0].LineNo,
			ColNo:    ss[0].ColNo,
			FileName: ss[0].FileName,
		}
		for _, vv := range ss {
			rv.List = append(rv.List, vv)
		}
		return
	}

	var walkTreeEmptyOutput func(mt *MtType, pos, depth int)
	walkTreeEmptyOutput = func(mt *MtType, pos, depth int) {
		mt.HTML_Output = ""
		for ii, vv := range mt.List {
			walkTreeEmptyOutput(vv, ii, depth+1)
		}
	}

	if callNo == 11 {
		if !curTree.MoreThan(1) {
		} else {
			ifp := FindTags(curTree.List[0], gen.Tok_Tree_Empty, gen.Tok_Tree_EndFor) // find parts of for loop
			fmt.Printf("ifp=%+v\n", ifp)
			if curTree.EvalExpr(Context, 0, 0) {
				x := tmpMt(curTree.List[0].List[0:ifp[0]])
				curTree.HTML_Output = ""
				// xyzzy - check type
				for ii, vv := range curTree.XValue.([]interface{}) {
					Context.SetInContext("$index", eval.CtxType_Int, ii) // xyzzy - conversion to string not correct -- needs to push $index - on endfor pop
					Context.SetInContext("$value", eval.CtxType_Str, vv) // xyzzy - conversion to string not correct
					//Context.SetInContext("key", fmt.Sprintf("%d", ii))    // xyzzy - conversion to string not correct	 -- key should be ID, Value too.
					//Context.SetInContext("value", fmt.Sprintf("%v", vv))  // xyzzy - conversion to string not correct
					pt.x_walk(x, pt.pos, pt.depth)
					curTree.HTML_Output += pt.CollectTree(x, 0) // Need to collect HTML_Output and append it to curTree.HTML_Output
				}
				mx := len(ifp)
				// xyzzy - check type
				if len(curTree.XValue.([]interface{})) == 0 && mx > 1 && curTree.List[0].List[ifp[mx-1]].NodeType == gen.Tok_Tree_Empty {
					i := mx - 1
					x := tmpMt(curTree.List[0].List[ifp[i]+1 : ifp[i+1]])
					pt.x_walk(x, pt.pos, pt.depth)
					curTree.HTML_Output += pt.CollectTree(x, 0) // Need to collect HTML_Output and append it to curTree.HTML_Output
				}
				walkTreeEmptyOutput(curTree.List[0], 0, 0) // set children's HTML_Output to ""
			}
		}
	}
	return
}