Skip to content

zwgblue/yaml-encoder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

yaml-encoder

This is a yaml encoder which implements yaml.Marshaler for marshal with comments.

Inspired by The doc's encoder of talos

example

package main

import (
  "fmt"
  encoder "github.com/zwgblue/yaml-encoder"
)

type DBConfig struct {
  Username string `comment:"this is the username of database"`
  Password string `comment:"this is the password of database"`
}

func main() {
    config := DBConfig{
      Username: "root",
      Password: "xxxxxx",
    }

    encoder := encoder.NewEncoder(config, encoder.WithComments(encoder.CommentsOnHead))
    content, _ := encoder.Encode()
    fmt.Printf("%s", content)
}

output:

# this is the username of database
username: root
# this is the password of database
password: xxxxxx

If you don't like to use the comment tag, you could use the WithCustomizedTag to change:

encoder := encoder.NewEncoder(config, encoder.WithCustomizedTag("yourTagName"))

About

A yaml encoder which implements yaml.Marshaler with comments

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages