maml

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2026 License: MIT Imports: 11 Imported by: 0

README

go-maml

MAML data format implementation for Go.

  • Spec-accurate v0.1 parser
  • Zero dependencies
  • Full AST with comments and source positions
  • Struct marshaling/unmarshaling with maml tags

Installation

go get github.com/maml-dev/go-maml

Usage

Marshal / Unmarshal
package main

import (
	"fmt"

	"github.com/maml-dev/go-maml"
)

type Config struct {
	Name  string   `maml:"name"`
	Port  int      `maml:"port"`
	Debug bool     `maml:"debug,omitempty"`
	Tags  []string `maml:"tags"`
}

func main() {
	// Unmarshal
	input := []byte(`{
  name: "my-app"
  port: 8080
  tags: ["web", "api"]
}`)

	var config Config
	if err := maml.Unmarshal(input, &config); err != nil {
		panic(err)
	}
	fmt.Println(config.Name) // my-app

	// Marshal
	data, err := maml.Marshal(config)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(data))
}
Parse to Value
val, err := maml.Parse(`{name: "hello", items: [1, 2, 3]}`)
if err != nil {
    panic(err)
}

name, _ := val.Get("name")
fmt.Println(name.AsString()) // hello
AST with Comments
package main

import (
	"fmt"

	"github.com/maml-dev/go-maml/ast"
)

func main() {
	doc, err := ast.Parse(`
# Server config
{
  host: "localhost"
  port: 8080 # default port
}
`)
	if err != nil {
		panic(err)
	}

	// Comments are preserved
	fmt.Println(len(doc.LeadingComments)) // 1

	// Print back with comments
	fmt.Println(ast.Print(doc))
}

MAML Format

{
  # Comments start with hash
  name: "MAML"
  version: 1
  score: 9.5
  enabled: true
  nothing: null

  tags: [
    "minimal"
    "readable"
  ]

  nested: {
    key: "value"
  }

  poem: """
Roses are red,
Violets are blue.
"""
}

License

MIT

Documentation

Overview

Package maml implements encoding and decoding of MAML (Minimal Abstract Markup Language).

MAML is a minimal configuration format that is human-readable and machine-parsable. For the specification, see https://maml.dev/spec/v0.1

The package provides Marshal/Unmarshal functions similar to encoding/json, and an AST parser in the ast subpackage for tooling use cases.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsIdentifierKey

func IsIdentifierKey(key string) bool

IsIdentifierKey returns true if the key can be written as a bare identifier.

func Marshal

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

Marshal returns the MAML encoding of v.

func QuoteString

func QuoteString(s string) string

QuoteString quotes a string value with proper MAML escape sequences.

func Stringify

func Stringify(v Value) string

Stringify converts a Value to its MAML string representation.

func Unmarshal

func Unmarshal(data []byte, v any) error

Unmarshal parses MAML data and stores the result in the value pointed to by v.

Types

type Decoder

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

Decoder reads and decodes MAML values from an input stream.

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder returns a new decoder that reads from r.

func (*Decoder) Decode

func (d *Decoder) Decode(v any) error

Decode reads MAML from the input and stores the result in v.

type Encoder

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

Encoder writes MAML values to an output stream.

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder returns a new encoder that writes to w.

func (*Encoder) Encode

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

Encode writes the MAML encoding of v to the stream.

type KeyValue

type KeyValue struct {
	Key   string
	Value Value
}

KeyValue is a key-value pair used by OrderedMap.

type MarshalError

type MarshalError struct {
	Message string
}

MarshalError represents an error encountered during marshaling.

func (*MarshalError) Error

func (e *MarshalError) Error() string

type Marshaler

type Marshaler interface {
	MarshalMAML() ([]byte, error)
}

Marshaler is the interface implemented by types that can marshal themselves into MAML.

type OrderedMap

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

OrderedMap is an ordered map of string keys to Values, preserving insertion order.

func NewOrderedMap

func NewOrderedMap() *OrderedMap

NewOrderedMap creates a new empty OrderedMap.

func (*OrderedMap) Entries

func (m *OrderedMap) Entries() []KeyValue

Entries returns key-value pairs in insertion order.

func (*OrderedMap) Get

func (m *OrderedMap) Get(key string) (Value, bool)

Get retrieves a value by key.

func (*OrderedMap) Keys

func (m *OrderedMap) Keys() []string

Keys returns the keys in insertion order.

func (*OrderedMap) Len

func (m *OrderedMap) Len() int

Len returns the number of entries.

func (*OrderedMap) Set

func (m *OrderedMap) Set(key string, value Value)

Set adds or updates a key-value pair, preserving insertion order.

type ParseError

type ParseError struct {
	Message string
	Line    int
}

ParseError represents an error encountered during parsing.

func (*ParseError) Error

func (e *ParseError) Error() string

type UnmarshalError

type UnmarshalError struct {
	Message string
}

UnmarshalError represents an error encountered during unmarshaling.

func (*UnmarshalError) Error

func (e *UnmarshalError) Error() string

type Unmarshaler

type Unmarshaler interface {
	UnmarshalMAML([]byte) error
}

Unmarshaler is the interface implemented by types that can unmarshal a MAML value.

type Value

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

Value represents a MAML value.

func NewArray

func NewArray(arr []Value) Value

NewArray creates an array Value.

func NewBool

func NewBool(b bool) Value

NewBool creates a boolean Value.

func NewFloat

func NewFloat(f float64) Value

NewFloat creates a float Value.

func NewInt

func NewInt(n int64) Value

NewInt creates an integer Value.

func NewNull

func NewNull() Value

NewNull creates a null Value.

func NewObject

func NewObject(m *OrderedMap) Value

NewObject creates an object Value from an OrderedMap.

func NewString

func NewString(s string) Value

NewString creates a string Value.

func Parse

func Parse(source string) (Value, error)

Parse parses a MAML source string into a Value.

func (Value) AsArray

func (v Value) AsArray() []Value

AsArray returns the array value, or panics.

func (Value) AsBool

func (v Value) AsBool() bool

AsBool returns the boolean value, or panics.

func (Value) AsFloat

func (v Value) AsFloat() float64

AsFloat returns the float value, or panics.

func (Value) AsInt

func (v Value) AsInt() int64

AsInt returns the integer value, or panics.

func (Value) AsObject

func (v Value) AsObject() *OrderedMap

AsObject returns the OrderedMap value, or panics.

func (Value) AsString

func (v Value) AsString() string

AsString returns the string value, or panics.

func (Value) Get

func (v Value) Get(key string) (Value, bool)

Get retrieves a value by key from an object Value.

func (Value) IsArray

func (v Value) IsArray() bool

IsArray returns true if the value is an array.

func (Value) IsBool

func (v Value) IsBool() bool

IsBool returns true if the value is a boolean.

func (Value) IsFloat

func (v Value) IsFloat() bool

IsFloat returns true if the value is a float.

func (Value) IsInt

func (v Value) IsInt() bool

IsInt returns true if the value is an integer.

func (Value) IsNull

func (v Value) IsNull() bool

IsNull returns true if the value is null.

func (Value) IsObject

func (v Value) IsObject() bool

IsObject returns true if the value is an object.

func (Value) IsString

func (v Value) IsString() bool

IsString returns true if the value is a string.

func (Value) Raw

func (v Value) Raw() any

Raw returns the underlying Go value.

func (Value) String

func (v Value) String() string

String implements fmt.Stringer.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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