2
\$\begingroup\$

I’ve multiple yaml files which need to be parsed and have exactly the same structure

schema: "1.0.0"
id: test
version: "1.2.3"


dependency :
 - name: ui
    type: runner
    cwd: /ui
    install:
       - name: api
         group: test
         properties:
             name: app
             url: appUrl

 - name: backend
    type: mongoDb
    path: be
    install:
       - name: db
         type: mongo
    provides:
       - name: api
         properties:
            url: url
  • The schema section is mandatory for all the yaml which the app should get always

  • The dependency tag can contain 1..n entries with mandatory fields such as ,name, type, cwd

  • The dependency tag can (or not) contain install section with name and properties which is mandatory

  • The dependency tag can (or not) contain provides section with name which is mandatory

The install can have properties and the provides also can have properties

I am using a yaml parser to parse the files but my question is how in Golang I can build struct that when I parse the doc it will automatically fill the main struct and will include the sub structs (such as dependency/ install sections )

The code which done the parse and working is the following:

package main

import (
    "fmt"
    "io/ioutil"
    "log"

    "gopkg.in/yaml.v2"
)

var data = `
schema: "1.0.0"
id: test
version: "1.2.3"


dependency :
  - name: ui
    type: runner
    cwd: /ui
    install:
       - name: api
         group: test
         properties:
             name: app
             url: appUrl

  - name: backend
    type: mongoDb
    cwd: be
    install:
       - name: db
         type: mongo
    provides:
       - name: api
         properties:
             url: url
`

type Yaml struct {
    Schema     string
    ID         string
    Version    string
    Dependency []Dependency
}

type Dependency struct {
    Name     string
    Type     string
    CWD      string
    Install  []Install
    Provides []Provide
}

type Install struct {
    Name       string
    Group      string
    Type       string
    Properties Properties
}

type Properties struct {
    Name string
    URL  string
}

type Provide struct {
    Name       string
    Properties Properties
}

func main() {
    y := Yaml{}

    err := yaml.Unmarshal([]byte(data), &y)
    if err != nil {
        log.Fatalf("error: %v", err)
    }
    fmt.Printf("%+v\n", y)

}

My question are:

  1. The install section can have group and properties so how should I contract the struct to handle this options nicely
  2. if the main is under rootProj-> cmd>main.go assume that the struct's should be extended , how should I build the project structure?

if my project is like following:

myproj
  vendor
  cmd
    main.go
  Gopkg.lock
  Gopkg.toml

Should I create another folder under the root and put the struct there ?

\$\endgroup\$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.