简体   繁体   English

将通用 map 转换为 YAML。 语言

[英]Convert a generic map to YAML. GoLang

So, I am working on a program where you have to generate the YAML file which represents the directory tree from a given directory location( path ).因此,我正在开发一个程序,您必须在其中生成 YAML 文件,该文件表示来自给定目录位置( path )的目录树。 For example..例如..

sc
├── file.go
├── file_test.go
└── src
    ├── deploy
    │   ├── deploy.go
    │   └── deploy_test.go
    ├── pod.go
    └── pod_test.go

This should be converted into这应该转换为

sc:
  fileTest: "true"
  src:
    deploy:
      deployTest: "true"
  podTest: "true"

Note that in above only the files with _test are chosen (it is conditional not much of a problem).请注意,在上面只选择了带有_test的文件(这是有条件的,问题不大)。 My question is how do you generate a generic map[string]interface{} type of map from the directory tree which can be represented via a YAML.我的问题是如何从可以通过 YAML 表示的目录树中生成 map 的通用map[string]interface{}类型。 I have tried writing a DFS implementation to walk through the tree and generate a n-ary tree.我尝试编写一个DFS实现来遍历树并生成一个n-ary叉树。 Which has the following output.其中有以下output。 I get the root Node object for the tree.我得到了树的根节点Node

name: sc
children:
- name: file.go
  children: []
- name: file_test.go
  children: []
- name: src
  children:
  - name: deploy
    children:
    - name: deploy.go
      children: []
    - name: deploy_test.go
      children: []
  - name: pod.go
    children: []
  - name: pod_test.go
    children: []

but as you can see it was unmarshalled using the datatype which represents every node.但正如您所看到的,它是使用代表每个节点的数据类型unmarshalled组的。

//Node is the datatype which stores information regarding the file/directory
type Node struct {
    Name     string
    Path     string
    Children []*Node
}

I want a generic YAML where every key is different.我想要一个通用的 YAML ,其中每个键都不同。 Any ideas?有任何想法吗? Something like this would help.这样的事情会有所帮助。

func (node *Node) ToMap() map[string]interface{}{
 //Logic
}

DFS Code to generate the tree生成树的 DFS 代码

func IteratePath(path string) {
    rootFile, err := os.Stat(path)
    if err != nil {
        logrus.Error("Path : ", path, " doesn't exist. ", err)
    }
    rootfile := ToFile(rootFile, path)
    stack := []*tree.Node{rootfile}
    for len(stack) > 0 {
        file := stack[len(stack)-1]
        stack = stack[:len(stack)-1]
        children, _ := ioutil.ReadDir(file.Path)
        for _, chld := range children {
            child := ToFile(chld, filepath.Join(file.Path, chld.Name()))
            file.Children = append(file.Children, child)
            stack = append(stack, child)
        }
    }

func ToFile(file os.FileInfo, path string) *tree.Node {
    outFile := tree.Node{
        Name:     file.Name(),
        Children: []*tree.Node{},
        Path:     path,
    }
    return &outFile
}

You're already using recursion (with a stack variable instead of the call stack), so why not use recursion again to translate your tree into nested maps.您已经在使用递归(使用堆栈变量而不是调用堆栈),那么为什么不再次使用递归将您的树转换为嵌套映射。 Here's a method I wrote to do that starting with a node (based on your Node struct above):这是我写的一个从节点开始的方法(基于上面的Node结构):

The tree to nested map method:嵌套 map 方法的树:

type Node struct {
    Name     string
    Children []*Node
}

func (n *Node) ToMap() interface{} {
    if len(n.Children) < 1 {
        return "true"
    }

    yamlMap := make(map[string]interface{})

    for _, child := range n.Children {
        yamlMap[child.Name] = child.ToMap()
    }

    return yamlMap
}

The map generated by this function is marshal-able by gopkg.in/yaml.v2 into a YAML file of the format you want.由此 function 生成的 map 可由gopkg.in/yaml.v2为所需格式的 YAML 文件。

Here's the code running in Go Playground including marshalling to YAML这是在Go Playground中运行的代码,包括编组到 YAML

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM