Categorygithub.com/zwgblue/yaml-encoder
modulepackage
0.0.0-20221226083717-a0bdbda0d998
Repository: https://github.com/zwgblue/yaml-encoder.git
Documentation: pkg.go.dev

# README

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"))

# Functions

NewEncoder initializes and returns an `Encoder`.
WithComments enables comments in the encoder.
WithCustomizedTag use customized tag for comment.
WithOmitEmpty toggles omitempty handling.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

Encoder implements config encoder.
Options defines encoder config.

# Type aliases

CommentsFlag comments encoding flags type.
Option gives ability to alter config encoder output settings.