Giter VIP home page Giter VIP logo

Comments (2)

ilyakaznacheev avatar ilyakaznacheev commented on July 30, 2024

That's an interesting proposal, thank you.

I think the easiest thing to do would be to simply make a function that takes the formatting function as a parameter, which is where the meta-information about the structure is transmitted. But at the moment all this metainformation is implemented locally, and I don't really want to make it exportable.

On the other hand, I don't want to introduce some DSL to describe templates and make library users learn it.

Anyway, I need to think about how to do it better and I'll be glad to receive suggestions.

from cleanenv.

eloo avatar eloo commented on July 30, 2024

Hi @ilyakaznacheev
I've just check how the Usage/Description is build in envconfig.
There they use text tables which looks pretty nice and its also possible to customise it easily using the template engine.
Maybe this could be a good solution here as well.

For example the interesting code could look like this:

const (
	DefaultTableFormat = `The following environment variables can be used for configuration:
	
KEY	TYPE	DEFAULT	REQUIRED	DESCRIPTION
{{range .}}{{usage_key .}}	{{usage_type .}}	{{usage_default .}}	{{usage_required .}}	{{usage_description .}}
{{end}}`
)
...
	functions := template.FuncMap{
		"usage_key":         func(v structMeta) string { return v.envList[0] },
		"usage_description": func(v structMeta) string { return v.description },
		"usage_type":        func(v structMeta) string { return v.fieldValue.Kind().String() },
		"usage_default":     func(v structMeta) string { return derefString(v.defValue) },
		"usage_required":    func(v structMeta) string { return formatBool(v.required) },
	}
...
        tmpl, err := template.New("cleanenv").Funcs(functions).Parse(DefaultTableFormat)
	tabs := tabwriter.NewWriter(os.Stdout, 1, 0, 4, ' ', 0)
	err = tmpl.Execute(tabs, meta)

If you think this could be a solution for your library i would create a PR which brings such a functionality.

Best regards
eloo

from cleanenv.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.