项目作者: PrasadG193

项目描述 :
Converts YAML specs into Go Lang type definitions
高级语言: Go
项目地址: git://github.com/PrasadG193/yaml2go.git
创建时间: 2019-07-19T11:37:31Z
项目社区:https://github.com/PrasadG193/yaml2go

开源协议:MIT License

下载


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

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

Usage

Show help

  1. yaml2go --help
  2. yaml2go converts YAML specs to Go type definitions
  3. Usage:
  4. yaml2go < /path/to/yamlspec.yaml
  5. Examples:
  6. yaml2go < test/example1.yaml
  7. yaml2go < test/example1.yaml > example1.go

Convert yaml spec to Go struct

  1. $ yaml2go < example.yaml

e.g

  1. $ cat example1.yaml
  2. kind: test
  3. metadata:
  4. name: cluster
  5. nullfield:
  6. nestedstruct:
  7. - nested:
  8. underscore_field: value
  9. field1:
  10. - 44.5
  11. - 43.6
  12. field2:
  13. - true
  14. - false
  15. nested2:
  16. - nested3:
  17. field1:
  18. - 44
  19. - 43
  20. fieldt:
  21. - true
  22. - false
  23. field3: value
  24. abc:
  25. - def:
  26. - black
  27. - white
  28. array1:
  29. - "string1"
  30. - "string2"
  31. array2:
  32. - 2
  33. - 6
  34. array3:
  35. - 3.14
  36. - 5.12
  37. is_underscore: true
  1. $ GOPATH/bin/yaml2go < example1.yaml
  2. // Yaml2Go
  3. type Yaml2Go struct {
  4. Kind string `yaml:"kind"`
  5. Metadata Metadata `yaml:"metadata"`
  6. Abc []Abc `yaml:"abc"`
  7. Array1 []string `yaml:"array1"`
  8. Array2 []int `yaml:"array2"`
  9. Array3 []float64 `yaml:"array3"`
  10. IsUnderscore bool `yaml:"is_underscore"`
  11. }
  12. // Metadata
  13. type Metadata struct {
  14. Name string `yaml:"name"`
  15. Nullfield interface{} `yaml:"nullfield"`
  16. Nestedstruct []Nestedstruct `yaml:"nestedstruct"`
  17. }
  18. // Nested3
  19. type Nested3 struct {
  20. Field1 []int `yaml:"field1"`
  21. Fieldt []bool `yaml:"fieldt"`
  22. Field3 string `yaml:"field3"`
  23. }
  24. // Abc
  25. type Abc struct {
  26. Def []string `yaml:"def"`
  27. }
  28. // Nestedstruct
  29. type Nestedstruct struct {
  30. Nested Nested `yaml:"nested"`
  31. Nested2 []Nested2 `yaml:"nested2"`
  32. }
  33. // Nested
  34. type Nested struct {
  35. UnderscoreField string `yaml:"underscore_field"`
  36. Field1 []float64 `yaml:"field1"`
  37. Field2 []bool `yaml:"field2"`
  38. }
  39. // Nested2
  40. type Nested2 struct {
  41. Nested3 Nested3 `yaml:"nested3"`
  42. }

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