Skip to content

for V2: add support for comments #1966

Open
@drsybren

Description

Preflight Checklist

  • I have searched the issue tracker for an issue that matches the one I want to file, without success.

Problem Description

It would be fantastic if Viper would support adding comments to configuration files and individual fields.

Currently my application writes YAML config file the following header:

# Configuration file for ${APPLICATION NAME}.
# For an explanation of the fields, refer to {application}-example.yaml
#
# NOTE: this file will be overwritten by ${APPLICATION NAME}'s web-based configuration system.
#
# This file was written on ${TIMESTAMP} by ${APPLICATION NAME}-${VERSION}

Having Viper produce such a header (on file types where comments are supported, of course) would be fantastic. Also in this particular header you can see that there's an example file with documentation in the comments. Having such documention comments produced by Viper would be amazing, as then the file itself could be written like:

# ${APPLICATION} listens to incoming connections from workers on this address.
listen: ':8080'

Proposed Solution

An API like this could work:

v.SetDocumentation("Configuration file for ${APPLICATION NAME}....")

v.SetDefault("listen", ":8080")
v.SetFieldDocumentation("listen", ${APPLICATION} listens to incoming connections from workers on this address.")

Alternatives Considered

The obvious alternative is to simply not have support for comments, as we have now.

Additional Information

An open question would be what to do with existing comments in the file. Would these be remembered when rewriting? Or would rewriting the config file always write the set-in-the-code documentation strings? I think the latter would be the easiest to implement, and it also allows updating that documentation in the config file when the application is updated.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions