text

package module
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: May 16, 2024 License: GPL-3.0 Imports: 10 Imported by: 7

README

Gophercraft/text

Go Reference Chat on discord

The Gophercraft text format is used in Gophercraft for configuration and data interchange.

You can think about it as a more lightweight JSON, but with some extra bells and whistles.

Another difference from JSON is that it cannot be used to represent arbitrary structures. Data must be serialized according to Go types.

Example document

type Document struct {
  StringField  string
  IntegerSlice []int
  Map          map[string]string
}
{
  /*
  *
  *
  * Block comments
  * 
  */
  
  // Or double-slash comments are allowed

  // Quotes are not required for words if they contain no whitespace or reserved characters
  StringField QuotesUnnecessary

  // Keys and values are both words. All words may be quoted.
  "IntegerSlice"
  {
    1
    2
    3
    "4" // Not a string, but can be quoted.
  }

  Map
  {
    "Key" value
    other_key "another value"
  }
}

Tabular documents

Encoded values can also be expressed in a tabular form (unkeyed structs).

This can be desirable when working with database files that are extremely large and contain many records, as it reduces the redudancy of repeating struct keys.

// Table header
[ StringField IntegerSlice Map ]
// Empty row
{ "" {} {} }
// Non-empty row
{ "string value" { 1 2 3 4 } { key value otherkey othervalue } }

Usage

Easy functions for dealing with a single record:

bytes, err := text.Marshal(&record)

err = text.Unmarshal(bytes, &record)

Use Encoder and Decoder to deal with many records in one stream, or make use of custom options:

encoder := text.NewEncoder(file)

// When using standard notation
encoder.Indent = "\t"

// When using tabular notation, this is what is placed between columns
encoder.Indent = " "

// Set tabular to true if you wish to encode
// table files with a much smaller size (See: Tabular documents)
encoder.Tabular = true|false

for _, record := range records {
  err = encoder.Encode(&record)
  // ...
}
decoder := text.NewDecoder(file)

for {
  err = decoder.Decode(&record)
  if errors.Is(err, io.EOF) {
    // done reading
    break
  } else if err != nil {
    // deal with error
    break
  }
  // handle decoded record
}

// close fd
file.Close()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

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

func Unmarshal

func Unmarshal(b []byte, value any) error

Types

type Decoder

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

A Decoder reads and decodes text values from an input stream.

func NewDecoder

func NewDecoder(in io.Reader) *Decoder

NewDecoder returns a new decoder that reads from r. The decoder introduces its own buffering and may read data from r beyond the text values requested.

func (*Decoder) Decode

func (decoder *Decoder) Decode(value any) (err error)

type Encoder

type Encoder struct {

	// What each indent should be. Preferably "\t" or "  "
	Indent string

	// Tabular encoding
	Tabular bool
	// contains filtered or unexported fields
}

func NewEncoder

func NewEncoder(out io.Writer) *Encoder

func (*Encoder) Encode

func (encoder *Encoder) Encode(value any) (err error)

type Word

type Word interface {
	EncodeWord() (string, error)
	DecodeWord(data string) error
}

Word describes custom data types that use one string. Useful for custom integral types

Jump to

Keyboard shortcuts

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