yaml2go

package module
Version: v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2019 License: MIT Imports: 9 Imported by: 0

README

yaml2go

Build Status

Converts YAML specs into Go type definitions

Installation

Binary Installation

Pre-compiled binaries are available on the releases page. You can download the correct binary depending on your system arch, put it into $PATH and hit yaml2go help

Install From Source

Build binary using go build

$ go get -u github.com/PrasadG193/yaml2go
$ go build -o yaml2go github.com/PrasadG193/yaml2go/cmd/cli

Usage

Show help
yaml2go --help
yaml2go converts YAML specs to Go type definitions

Usage:
    yaml2go < /path/to/yamlspec.yaml

Examples:
    yaml2go < test/example1.yaml
    yaml2go < test/example1.yaml > example1.go
Convert yaml spec to Go struct
$ yaml2go < example.yaml

e.g

$ cat example1.yaml
kind: test
metadata:
  name: cluster
  nullfield:
  nestedstruct:
  - nested:
      underscore_field: value
      field1:
      - 44.5
      - 43.6
      field2:
      - true
      - false
    nested2:
      - nested3:
          field1:
          - 44
          - 43
          fieldt:
          - true
          - false
          field3: value
abc:
  - def:
    - black
    - white
array1:
  - "string1"
  - "string2"
array2:
  - 2
  - 6
array3:
  - 3.14
  - 5.12
is_underscore: true
$ GOPATH/bin/yaml2go < example1.yaml
// Yaml2Go
type Yaml2Go struct {
        Kind         string    `yaml:"kind"`
        Metadata     Metadata  `yaml:"metadata"`
        Abc          []Abc     `yaml:"abc"`
        Array1       []string  `yaml:"array1"`
        Array2       []int     `yaml:"array2"`
        Array3       []float64 `yaml:"array3"`
        IsUnderscore bool      `yaml:"is_underscore"`
}

// Metadata
type Metadata struct {
        Name         string         `yaml:"name"`
        Nullfield    interface{}    `yaml:"nullfield"`
        Nestedstruct []Nestedstruct `yaml:"nestedstruct"`
}

// Nested3
type Nested3 struct {
        Field1 []int  `yaml:"field1"`
        Fieldt []bool `yaml:"fieldt"`
        Field3 string `yaml:"field3"`
}

// Abc
type Abc struct {
        Def []string `yaml:"def"`
}

// Nestedstruct
type Nestedstruct struct {
        Nested  Nested    `yaml:"nested"`
        Nested2 []Nested2 `yaml:"nested2"`
}

// Nested
type Nested struct {
        UnderscoreField string    `yaml:"underscore_field"`
        Field1          []float64 `yaml:"field1"`
        Field2          []bool    `yaml:"field2"`
}

// Nested2
type Nested2 struct {
        Nested3 Nested3 `yaml:"nested3"`
}

Contributing

We love your input! We want to make contributing to this project as easy and transparent as possible, whether it's:

  • Reporting a bug
  • Discussing the current state of the code
  • Submitting a fix
  • Proposing new features

Credits

The Go Gopher is originally by Renee French

This artwork is borrowed from an awesome artwork collection by Egon Elbre

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HandleConvert added in v0.2.0

func HandleConvert(w http.ResponseWriter, r *http.Request)

Types

type Yaml2Go

type Yaml2Go struct {
	Visited   map[line]bool
	StructMap map[string]string
}

Yaml2Go to store converted result

func New added in v0.2.0

func New() Yaml2Go

New creates Yaml2Go object

func (*Yaml2Go) AppendResult

func (yg *Yaml2Go) AppendResult(structName string, l string)

AppendResult add lines to the result

func (*Yaml2Go) Convert

func (yg *Yaml2Go) Convert(structName string, data []byte) (string, error)

Convert transforms map[string]interface{} to go struct

func (*Yaml2Go) NewStruct added in v0.2.0

func (yg *Yaml2Go) NewStruct(structName string, parent string) string

NewStruct creates new entry in StructMap result

func (*Yaml2Go) Structify

func (yg *Yaml2Go) Structify(structName, k string, v interface{}, arrayElem bool)

Structify transforms map key values to struct fields structName : parent struct name k, v : fields in the struct

Directories

Path Synopsis
cmd
cli

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL