Git Product home page Git Product logo

yaml2go's Introduction

yaml2go

Build Status

Converts YAML specs into Go type definitions

https://github.com/PrasadG193/yaml2go

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

yaml2go's People

Contributors

bajran avatar prasadg193 avatar viveksinghggits avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

yaml2go's Issues

go2yaml

Is there any way to do a go struct -> yaml conversion ? Need to generate some docs for the structs ๐Ÿค”

yaml2go shouldn't create anonymous structs

As of now, for every yaml, yaml2go creates anonymous structs for nested yaml objects
e.g for yaml

kind: test
metadata:
  name: cluster
  namespace: test-ns

yaml2go convert into inline structs as:

type Yaml2Go struct {
        Kind     string `yaml:"kind"`
        Metadata struct {
                Name      string `yaml:"name"`
                Namespace string `yaml:"namespace"`
        } `yaml:"metadata"`
}

There should be a way to get normal structure like:

type Yaml2Go struct {
        Kind     string   `yaml:"kind"`
        Metadata Metadata `yaml:"metadata"`
}
type Metadata struct {
        Name      string `yaml:"name"`
        Namespace string `yaml:"namespace"
}

Add http server

yaml2go should server REST APIs in order to integrate with UI
For now, /POST request should suffice

yaml2go produces duplicate structs

For yaml like:

key2:
  key: value
key3:
  key2:
    key: value
    key1: value

yaml2go produces

// Yaml2Go
type Yaml2Go struct {
        Key2 Key2 `yaml:"key2"`
        Key3 Key3 `yaml:"key3"`
}

// Key2
type Key2 struct {
        Key string `yaml:"key"`
}

// Key2
type Key2 struct {
        Key  string `yaml:"key"`
        Key1 string `yaml:"key1"`
}

// Key3
type Key3 struct {
        Key2 Key2 `yaml:"key2"`
}

which contains duplicate struct Key2

Invalid YAML expected type, found '-'

when the yaml value key has โ€œ-โ€ strings
The key converted from the Convert function also comes with a "-" string, and an error is reported after being formatted using the format.source function
like this:

extra_args: 
      allowed-unsafe-sysctls: "net.core.*"
      node-status-update-frequency: 20s
      eviction-soft: ""
      eviction-soft-grace-period: ""
      eviction-hard: ""
// ExtraArgs
type ExtraArgs struct {
Allowed-Unsafe-Sysctls string `yaml:"allowed-unsafe-sysctls"`
Node-Status-Update-Frequency string `yaml:"node-status-update-frequency"`
Eviction-Soft string `yaml:"eviction-soft"`
Eviction-Soft-Grace-Period string `yaml:"eviction-soft-grace-period"`
Eviction-Hard string `yaml:"eviction-hard"`
}

2020/03/05 14:55:34 14:10: expected type, found '-' (and 5 more errors)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.