Exemplo n.º 1
0
func (w ExprOperator) Process(inp map[string]interface{}) {
	if util.IsPlus(inp) {
		vars := []string{}
		for k, v := range inp {
			_, isNumber := util.CoerceNumber(v)
			var s string
			if isNumber == nil {
				s = fmt.Sprintf("%v=%v", k, v)
			} else {
				s = fmt.Sprintf("%v=\"%v\"", k, v)
			}
			vars = append(vars, s)
		}
		varStr := strings.Join(vars, ";")
		pythonCmd := varStr + "; print " + w.expr
		cmdStr := []string{"-c", pythonCmd}
		cmd := exec.Command("python", cmdStr...)
		out, err := cmd.Output()
		if err != nil {
			fmt.Println("ERROR", pythonCmd)
			return
		}
		strOut := strings.Trim(string(out), "\n")
		numRep, isNum := util.CoerceNumber(strOut)
		if isNum == nil {
			inp[w.lsh] = numRep
		} else {
			inp[w.lsh] = strOut
		}
		w.output.Write(inp)
	}
}
Exemplo n.º 2
0
func (w FilterOperator) Process(inp map[string]interface{}) {
	if util.IsPlus(inp) {
		v, exists := inp[w.key]
		if exists && v == w.value {
			w.output.Write(inp)
		}
	}
}
Exemplo n.º 3
0
func (p Parser) Process(inp map[string]interface{}) {
	if util.IsPlus(inp) {
		matches := p.regex.FindStringSubmatch(util.ExtractRaw(inp))
		if len(matches) == 1+len(p.extractions) {
			for i, match := range matches[1:] {
				inp[p.extractions[i]] = match
			}
			p.output.Write(inp)
		}
	}
}
Exemplo n.º 4
0
func (state RenderState) Process(inp map[string]interface{}) {
	if util.IsStartRelation(inp) {
		newmap := make([]map[string]interface{}, 0)
		*state.Messages = newmap
	} else if util.IsEndRelation(inp) {
		state.Flush()
	} else if util.IsRelation(inp) {
		*state.Messages = append(*state.Messages, inp)
	} else if util.IsMeta(inp) {
		*state.Meta = inp
	} else if util.IsPlus(inp) {
		*state.Messages = append(*state.Messages, inp)
		state.Flush()
	} else {
		// :-( no type information
	}
}
Exemplo n.º 5
0
func (r Renderer) Process(inp map[string]interface{}) {
	if util.IsPlus(inp) {
		var printed = false
		charsPrinted := int64(0)
		colsWidth := render.Columns([]map[string]interface{}{inp})
		colNames := render.ColumnNames(colsWidth)
		*r.cols = colNames
		*r.colWidths = colsWidth
		for _, col := range colNames {
			v, _ := inp[col]
			vStr := fmt.Sprint(v)
			spaces := strings.Repeat(" ", colsWidth[col]-len(vStr))
			finalStr := fmt.Sprintf("[%s=%v]%s", col, vStr, spaces)
			if charsPrinted+int64(len(finalStr)) > r.width {
				availableChars := r.width - charsPrinted
				if availableChars > 3 {
					fmt.Printf(finalStr[:availableChars-3])
					fmt.Printf("...")
					charsPrinted = r.width
				}
			} else {
				charsPrinted += int64(len(finalStr))
				fmt.Printf(finalStr)
			}
		}
		if printed {
			fmt.Printf(";")
		}
		if r.showRaw {
			fmt.Printf(util.ExtractRaw(inp))
		}
		fmt.Print("\n")
	}
	if util.IsStartRelation(inp) {
		fmt.Println("======")
		for _, col := range *r.cols {
			width := (*r.colWidths)[col]
			spaces := strings.Repeat(" ", width-len(col))
			fmt.Printf("%v%s", col, spaces)
		}
		*r.rowsPrinted += 2
		fmt.Printf("\n")
	}
	if util.IsRelation(inp) {
		colsWidth := render.Columns([]map[string]interface{}{inp})
		colNames := render.ColumnNames(colsWidth)
		*r.cols = colNames
		*r.colWidths = colsWidth
		for _, col := range colNames {
			v, _ := inp[col]
			vStr := fmt.Sprint(v)
			spaces := strings.Repeat(" ", colsWidth[col]-len(vStr))
			fmt.Printf("%v%s", vStr, spaces)
		}
		*r.rowsPrinted += 1
		fmt.Printf("\n")
	}

	if util.IsEndRelation(inp) {
		if *r.rowsPrinted < r.height-1 {
			for i := *r.rowsPrinted; i < r.height; i++ {
				fmt.Printf("\n")
			}
		}
		*r.rowsPrinted = 0
	}
}
Exemplo n.º 6
0
func (ct count) Process(inp map[string]interface{}) {
	if util.IsPlus(inp) {
		*ct.ct += 1
	}
}