Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array in yml file as context is not supported #328

Open
boatrainlsz opened this issue Feb 2, 2023 · 0 comments
Open

Array in yml file as context is not supported #328

boatrainlsz opened this issue Feb 2, 2023 · 0 comments

Comments

@boatrainlsz
Copy link

boatrainlsz commented Feb 2, 2023

I have a context.yml file contains:
ARRAY: ['elem1', 'elem2', 'elem3']
template.conf file contains:
array: {{ ARRAY }}

I did templating and expected output is:
array: ['elem1', 'elem2', 'elem3']
and actual output is:
array: <[]interface {} Value>

code snippet:

package main

import (
	"fmt"
	"github.com/flosch/pongo2/v6"
	"gopkg.in/yaml.v2"
	"io/ioutil"
	"os"
)

func main() {
	//write ARRAY: ['elem1', 'elem2', 'elem3'] to context.yml
	os.WriteFile("context.yml", []byte("ARRAY: ['elem1', 'elem2', 'elem3']"), 0644)
	//write array: {{ ARRAY }} to template.conf
	os.WriteFile("template.conf", []byte("array: {{ ARRAY }}"), 0644)
	readByte, err := readFile("template.conf")
	if err != nil {
		return
	}
	context := pongo2.Context{}
	contextFile, err := os.Open("context.yml")
	if err != nil {
		return
	}
	out := make(map[string]interface{})
	if err := yaml.NewDecoder(contextFile).Decode(out); err != nil {
		return
	}
	fmt.Println("yml decode: ", out)
	contextFile.Seek(0, 0)
	if err := yaml.NewDecoder(contextFile).Decode(&context); err != nil {
		return
	}
	temp, err := pongo2.FromBytes(readByte)
	if err != nil {
		return
	}
	output, err := temp.Execute(context)
	if err != nil {
		return
	}
	fmt.Println(output)
}
func readFile(path string) ([]byte, error) {
	file, err := os.Open(path)
	if err != nil {
		return nil, err
	}
	defer file.Close()
	byteValue, err := ioutil.ReadAll(file)
	return byteValue, nil

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant