san

package module
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2018 License: Apache-2.0 Imports: 9 Imported by: 0

README

SAN-go

GoDoc Build Status GitHub release

SAN (pronounce /seɪn/, like sane) CLI and parser for Go.

Spec: https://astrocorp.net/san

Compatible with SAN version: v1.0.0

  1. Installation
  2. Library
  3. CLI

Installation

go get -u github.com/phasersec/san-go/...

Library

package main

import (
	"fmt"

	"github.com/phasersec/san-go"
)

type D struct {
	A string
}

type C struct {
	A int64 `san:"a"`
	D []D   `san:"d"`
}

type S struct {
	A string  `san:"a"`
	B []int64 `san:"b"`
	C C       `san:"c"`
}

func main() {
	str1 := `
a = "a"
b = [1, 2]
c = { a = 1, d = [ { a = "3.3" }, { a = "xxx" } ] }
`
	var s S

	err := san.Unmarshal([]byte(str1), &s)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%#v\n\n", s)

	b, err := san.Marshal(s)
	if err != nil {
		panic(err)
	}
	fmt.Print(string(b))
}
go run main.go
main.S{A:"a", B:[]int64{1, 2}, C:main.C{A:1, D:[]main.D{main.D{A:"3.3"}, main.D{A:"xxx"}}}}

a = "a"
b = [
  1,
  2,
]
c = {
  a = 1

  d = [
    {
      A = "3.3"
    },
    {
      A = "xxx"
    },
  ]
}

CLI

This repo also contains a CLI helper for the SAN format. It can be installed with the following command:

$ go get -u github.com/phasersec/san-go/...
Examples

Convert a [.toml, .json, .yml, .yaml] file to a .san

$ san convert ../config.yml # wil create ../config.san

Automatically formats a SAN file

$ san fmt config.san

Check a .san file validity

$ san validate config.san

Documentation

Overview

Package san is a SAN (https://astrocorp.net/san) parser and manipulation library.

Index

Constants

View Source
const (
	// Version is the san's parser version
	Version = "0.5.1"
)

Variables

This section is empty.

Functions

func Load added in v0.2.0

func Load(file string, v interface{}) error

Load is a sortcut for Readfile + Unmarshal use it like: err = san.Load("myfile.san", &myStruct)

func Marshal

func Marshal(data interface{}) ([]byte, error)

Marshal returns the SAN encoding of sata. Behavior is similar to the Go json encoder, except that there is no concept of a Marshaler interface or MarshalSAN function for sub-structs, and currently only definite types can be marshaled (i.e. no `interface{}`). The following struct annotations are supported:

san:"Field"      Overrides the field's name to output.
omitempty         When set, empty values and groups are not emitted.

Note that pointers are automatically assigned the "omitempty" option, as SAN explicitly does not handle null values (saying instead the label should be dropped). Tree structural types and corresponding marshal types:

*Tree                            (*)struct, (*)map[string]interface{}
[]*Tree                          (*)[](*)struct, (*)[](*)map[string]interface{}
[]interface{} (as interface{})   (*)[]primitive, (*)[]([]interface{})
interface{}                      (*)primitive

Tree primitive types and corresponding marshal types:

uint64     uint, uint8-uint64, pointers to same
int64      int, int8-uint64, pointers to same
float64    float32, float64, pointers to same
string     string, pointers to same
bool       bool, pointers to same

func Unmarshal

func Unmarshal(data []byte, v interface{}) error

Unmarshal decodes the contents of `p` in SAN format into a pointer `v`.

Types

type Decoder

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

Decoder reads and decodes SAN values from an input stream.

type Marshaler

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

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

type Options

type Options struct {
	Name      string
	Include   bool
	OmitEmpty bool
}

type Unmarshaler added in v0.4.5

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

Unmarshaler is the interface implemented by types that can unmarshal a SAN description of themselves. The input can be assumed to be a valid encoding of a SAN value. UnmarshalSAN must copy the SAN data if it wishes to retain the data after returning.

Directories

Path Synopsis
cmd
san

Jump to

Keyboard shortcuts

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