Skip to content

Allow processing pre-parsed slice of fields (via interp.Config) #248

@TLINDEN

Description

@TLINDEN

Howdy,

currently - as far as I understand the docs - if you use the go api, you have to feed the data to be processed as a string (or from whatever input it comes, but will be a string anyway), which then will be parsed according to the settings. I suspect, that in the end this input ends up in some data structure like [][]any{} or something like that.

What if I already have such a data structure? It would be cool to be able to feed just the data to interp.Config{} somehow, thereby skipping the CSV parsing and just feed the data to the AWK program?

Something like:

func main() {
	src := `{ total += @"amount" } END { print total }`
	input := [][]string{
		[]string{"name", "amount"},
		[]string{"Bob", "17.50"},
		[]string{"Jill", "20"},
		[]string{"Boba Fett", "100.00"},
	}
	prog, err := parser.ParseProgram([]byte(src), nil)
	if err != nil {
		fmt.Println(err)
		return
	}
	config := &interp.Config{
		Data:      input,
		InputMode: interp.CSVMode,
		CSVInput:  interp.CSVInputConfig{Comment: '#', Header: true},
	}
	_, err = interp.ExecProgram(prog, config)
	if err != nil {
		fmt.Println(err)
		return
	}
}

That way I could for instance parse JSON tabular data and run some AWK code on it :)

Would that be possible?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions