Skip to content

bug: Inconsistent casing with parsed structs #113

Open
@mrueg

Description

@mrueg

Describe the bug

If my struct has a field that is not all lower case and I render it in a template using sprout, the field will return as lower keys.
The only option I have is to use annotate the field using go tags, which might not always be feasible.

To Reproduce

Use minimal example provided below, there are two structs, one with the go tag and one without.

Expected behavior

It should preserve the casing of the field name.

Code Snippet

package main

import (
	"fmt"
	"os"
	"text/template"

	"github.com/go-sprout/sprout/sprigin"
)

type apple struct {
	CamelCase int
}

type pear struct {
	CamelCase int `yaml:"CamelCase"`
}

func main() {
	myA := apple{CamelCase: 5}

	myB := pear{CamelCase: 10}
	tpl := template.Must(
		template.New("example").Funcs(sprigin.FuncMap()).Parse(`{{ . | toYAML }}`),
	)
	tpl.Execute(os.Stdout, &myA)
	fmt.Println("")
	tpl.Execute(os.Stdout, &myB)
}

This returns:

camelcase: 5
CamelCase: 10

I would have expected:

CamelCase: 5
CamelCase: 10

Link to playground: https://go.dev/play/p/7F9lZ-wsmun

Version of software

v1.0.0

Additional context

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

Labels

state/triage 🚦Has not been triaged & therefore, not ready for workstate/under investigation 🚧This issue is currently under investigation, research are being conducted to identify solutions.type/bug 🔥Something isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions