yamlfmt

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2025 License: MIT Imports: 8 Imported by: 1

README

yamlfmt

Format yaml documents with a JSONPath like syntax.

Get Started

SDK:

package main

import (
	"gopkg.in/yaml.v3"
    "github.com/Emptyless/yamlfmt"
)

func main() {
	node := new(yaml.Node)
	// or use the utility method to parse []byte: yamlfmt.LintBytes

	// optionally validate rules
	err := yamlfmt.Validate(rules)
	if err != nil {
		panic(err)
	}

	// lint document
	yamlfmt.Lint(node, []yamlfmt.Rule{yamlfmt.NewRule("$.key[*].name[0]", yamlfmt.StringOrderingFn, yamlfmt.NewSimpleOrdering("first", "second"))})
}
Opinionated formatting of OpenAPI files
$ go install github.com/Emptyless/yamlfmt/openapi-fmt@latest
$ openapi-fmt --help
opinionated formatter of openapi.yaml files

Usage:
  openapi-fmt [flags]

Flags:
      --alphabetical stringArray   path to node to sort alphabetically (e.g. '$.key')
  -f, --file string                path to openapi.yaml file
  -h, --help                       help for openapi-fmt
  -q, --quiet count                Decrease the verbosity of the output by one level, -v hides warning logs and -vv will suppress non-fatal errors
      --simple stringArray         path=keys to node to sort (e.g. path = '$.key') with comma separated list of keys
  -v, --verbose count              Increase the verbosity of the output by one level, -v shows informational logs and -vv will output debug information.

Given some openapi.yaml:

openapi-fmt --file openapi.yaml

Overwrite original specification:

openapi-fmt --file openapi.yaml > openapi.yaml

Provide additional rules:

openapi-fmt --file openapi.yaml --alphabetical '$.key' --simple '$.key[*]=first,second'

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrIllegalToken = errors.New("illegal token")

ErrIllegalToken is returned when a token is used that is not expected, e.g. two indexOpen tokens [[ sequentially

Functions

func Lint

func Lint(node *yaml.Node, rules []Rule)

Lint a yaml.Node the provided slice of Rule

func LintBytes

func LintBytes(b []byte, rules []Rule) ([]byte, error)

LintBytes is a utility method that unmarshaled the provided bytes into a yaml.Node, applies Lint on the node and returns the marshaled result

func StringOrderingFn

func StringOrderingFn(_ string, value *yaml.Node)

StringOrderingFn sorts yaml.MappingNode and yaml.SequenceNode on their yaml.Node.Value using default cmp.Compare function

func Validate

func Validate(rules []Rule) error

Validate rules that there are no parse errors

Types

type OrderFn

type OrderFn func(key string, value *yaml.Node)

OrderFn is executed on some yaml.Node with the key being a JSONPath like value

func NewSimpleOrdering

func NewSimpleOrdering(keys ...string) OrderFn

NewSimpleOrdering sorts the keys of a yaml.MappingNode in the order provided with "keys". If keys that are present in the YAML are not present in the supplied keys, the original order of that key will be preserved after all supplied keys are processed. e.g. NewSimpleOrdering("a", "b", "c") on a yaml node with keys ["c", "f", "b", "e", "a"] will result in ["a", "b", "c", "f", "e"] (notice how a, b, c are at the front in order provided and remaining keys are in their original order)

type Rule

type Rule struct {
	// Path using a JSONPath like syntax (except filtering):
	// '$' is the document root
	// '$.key' to select a key
	// '$[0]' to select some index
	// '$[*]' or '$.*' for wildcard searches
	// '$.some[*].*.name' can combine any of above rules
	Path string
	// Functions to execute if there is a Path match
	Functions []OrderFn
}

Rule combines a JSONPath like syntax to an OrderFn to apply on the node

func DefaultOpenAPIRules

func DefaultOpenAPIRules() []Rule

DefaultOpenAPIRules contains an opinionated ordering of an 'openapi.yaml' file based on the tables documented on https://swagger.io/specification/#schema-1

func NewRule

func NewRule(path string, fns ...OrderFn) Rule

NewRule constructor for Rule

func (*Rule) Run

func (r *Rule) Run(key string, value *yaml.Node)

Run Rule.Functions for given key and value

Directories

Path Synopsis
openapi-fmt module

Jump to

Keyboard shortcuts

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