yamlc

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 19, 2026 License: MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

yamlc

YAML marshaling with comment support via struct tags, built on top of gopkg.in/yaml.v3, making generated config files self-documenting and easy-readable without any post-processing.

Install

go get github.com/denwwer/yamlc

Usage

Add a comment struct tag to annotate fields. By default the comment is placed above the key (head comment). Prefix with > to place it inline (line comment).

type Config struct {
  Port     int               `yaml:"port,omitempty" comment:"Default port"`
  Log      Log               `yaml:"log" comment:"Defines the configuration for logging"`
  DevMode  bool              `yaml:"dev_mode"`
  Versions *Versions         `yaml:"versions" comment:"> Use version 10"`
  Password string            `yaml:"-" comment:"App password"`
  Keys     map[string]string `yaml:"keys" comment:"Custom keys"`
}

type Log struct {
  Level  string `yaml:"level" comment:"Log level: error | info | debug (default: info)"`
  Format string `yaml:"format" comment:"> text | json"`
}

type Versions []string
Encode to file
f, err := os.Create("config.yaml")
if err != nil {
  return err
}
defer f.Close()

enc := yamlc.NewEncoder(yaml.NewEncoder(f))
err = enc.Encode(cfg)

Comment syntax

Tag value Result
"Some text" Head comment (above the key)
"> Some text" Line comment (next to the key)

Example output

# Defines the configuration for logging
log:
  # Log level: error | info | debug (default: info)
  level: ""
  format: text # text | json
dev_mode: false
versions: # Use version 10
  - v1
  - v10
# Custom keys
keys:
  "1": k-1
  "2": k-2

Documentation

Index

Constants

View Source
const CommentTag = "comment"

CommentTag name associated with a struct field tag.

Variables

This section is empty.

Functions

func Marshal

func Marshal(v any) ([]byte, error)

Marshal converts a value to YAML-encoded data.

func ToNode

func ToNode(model any) (*yaml.Node, error)

ToNode converts a model to a *yaml.Node.

Types

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}

Encoder provides methods to encode struct into YAML format.

func NewEncoder

func NewEncoder(encoder *yaml.Encoder) *Encoder

NewEncoder creates a new Encoder instance using the provided yaml.Encoder.

func (*Encoder) Encode

func (e *Encoder) Encode(v any) error

Encode encodes the value into YAML format and writes it using the Encoder.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL