将数据从数据库转换为一个地图

问题是 - 我不想为每个数据库创建一个结构(如果我描述所有XL的所有结构,则代码太多,我有超过200个DB)。所以我需要可用于其中任何一个的smth


我的一个数据库的数据如下所示:


----------------------------------------------

Id  |      Path                          |Value

-----------------------------------------------

1   | SalesPlan/SalesPlanData/Year       | 2021

2   | SalesPlan/SalesPlanData/Month      | July

3   | SalesPlan/SalesPlanPerson/id       | 123

....

1700| SalesPlan/SalesPlarSpot/Spots/City | NY

我尝试了很多方法,但最终,我无法创建一个灵活的映射结构,可以在每个DB行之后正确更新下一个代码允许我存储所有最终标签,但我想在树上向上移动并更新孔结构



type ParentTag struct {

    Key   string

    Value []InnerTag

}


type InnerTag struct {

    Key   string

    Value string

}


func (s *ParentTag) Add(i InnerTag) {

    s.Value = append(s.Value, i)

    log.Printf("New X=%d", s.Value)

}


func main() {

    //xmlMap := Tag{}

    parentTagsStorage := []ParentTag{}


    house_1 := []string{"mydoc", "Country", "City", "Street", "House", "14"}

    house_2 := []string{"mydoc", "Country", "City", "Street", "House", "15"}

    street_1 := []string{"mydoc", "Country", "City", "Street", "Maddison"}

    city_1 := []string{"mydoc", "Country", "City", "NY"}


    allData := make([][]string, 0)

    allData = append(allData, house_1)

    allData = append(allData, house_2)

    allData = append(allData, street_1)

    allData = append(allData, city_1)



    for _, row := range allData {

        // the latest 2 elements present xml-tag and all previous ones are parents tags

        // <Street>

        //    <House>1</House>

        // </Street>

        innerTag := InnerTag{

            Key:   row[len(row)-2],

            Value: row[len(row)-1],

        }

        ifParentTagExist := false


        for i, pTag := range parentTagsStorage {

            if pTag.Key == row[len(row)-3] {

                pTag.Add(innerTag)

                parentTagsStorage[i] = pTag

                ifParentTagExist = true

            }

        }

}

对于任何想法,我将非常高兴


海绵宝宝撒
浏览 114回答 1
1回答

莫回无

好了,我们开始吧:package mainimport (&nbsp; &nbsp; "encoding/json"&nbsp; &nbsp; "encoding/xml"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "io"&nbsp; &nbsp; "os"&nbsp; &nbsp; "strings")type pathVal struct {&nbsp; &nbsp; path string&nbsp; &nbsp; val&nbsp; interface{}}func encode(dst io.Writer, src []pathVal) error {&nbsp; &nbsp; enc := xml.NewEncoder(dst)&nbsp; &nbsp; enc.Indent("", "\t") // for a prettier look&nbsp; &nbsp; tree := makeTree(src)&nbsp; &nbsp; err := encodeTree(enc, tree)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return err&nbsp; &nbsp; }&nbsp; &nbsp; return enc.Flush()}func encodeTree(enc *xml.Encoder, tree tree) error {&nbsp; &nbsp; for key, node := range tree {&nbsp; &nbsp; &nbsp; &nbsp; err := enc.EncodeToken(xml.StartElement{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name: xml.Name{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Local: key,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return err&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if node.SubTree != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; err = encodeTree(enc, node.SubTree)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return err&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if node.Value != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; err = encodeValue(enc, node.Value)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return err&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; err = enc.EncodeToken(xml.EndElement{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name: xml.Name{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Local: key,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return err&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return nil}func encodeValue(enc *xml.Encoder, val interface{}) error {&nbsp; &nbsp; return enc.EncodeToken(xml.CharData(fmt.Sprintf("%v", val)))}type tree map[string]*treeNodetype treeNode struct {&nbsp; &nbsp; SubTree tree&nbsp; &nbsp; Value&nbsp; &nbsp;interface{}}func makeTree(src []pathVal) tree {&nbsp; &nbsp; root := make(tree)&nbsp; &nbsp; for _, elem := range src {&nbsp; &nbsp; &nbsp; &nbsp; comps := strings.Split(elem.path, "/")&nbsp; &nbsp; &nbsp; &nbsp; comps, last := comps[:len(comps)-1], comps[len(comps)-1]&nbsp; &nbsp; &nbsp; &nbsp; subTree := root&nbsp; &nbsp; &nbsp; &nbsp; for _, comp := range comps {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; node, exists := subTree[comp]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if !exists {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newTree := make(tree)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; subTree[comp] = &treeNode{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SubTree: newTree,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; subTree = newTree&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if node.SubTree == nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; node.SubTree = make(tree)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; subTree = node.SubTree&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if node, exists := subTree[last]; exists {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; node.Value = elem.val&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; subTree[last] = &treeNode{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Value: elem.val,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return root}// "Table 1"var data1 = []pathVal{&nbsp; &nbsp; pathVal{&nbsp; &nbsp; &nbsp; &nbsp; path: "SalesPlan/SalesPlanData/Year",&nbsp; &nbsp; &nbsp; &nbsp; val:&nbsp; 2021,&nbsp; &nbsp; },&nbsp; &nbsp; pathVal{&nbsp; &nbsp; &nbsp; &nbsp; path: "SalesPlan/SalesPlanData/Month",&nbsp; &nbsp; &nbsp; &nbsp; val:&nbsp; "July",&nbsp; &nbsp; },&nbsp; &nbsp; pathVal{&nbsp; &nbsp; &nbsp; &nbsp; path: "SalesPlan/SalesPlanData/id",&nbsp; &nbsp; &nbsp; &nbsp; val:&nbsp; 123,&nbsp; &nbsp; },&nbsp; &nbsp; pathVal{&nbsp; &nbsp; &nbsp; &nbsp; path: "SalesPlan/SalesPlanSpot/Spots/City",&nbsp; &nbsp; &nbsp; &nbsp; val:&nbsp; "NY",&nbsp; &nbsp; },}// "Table 2"var data2 = []pathVal{&nbsp; &nbsp; pathVal{&nbsp; &nbsp; &nbsp; &nbsp; path: "mydoc/Country/City/Street/House",&nbsp; &nbsp; &nbsp; &nbsp; val:&nbsp; 14,&nbsp; &nbsp; },&nbsp; &nbsp; pathVal{&nbsp; &nbsp; &nbsp; &nbsp; path: "mydoc/Country/City/Street/House",&nbsp; &nbsp; &nbsp; &nbsp; val:&nbsp; 15,&nbsp; &nbsp; },&nbsp; &nbsp; pathVal{&nbsp; &nbsp; &nbsp; &nbsp; path: "mydoc/Country/City/Street",&nbsp; &nbsp; &nbsp; &nbsp; val:&nbsp; "Maddison",&nbsp; &nbsp; },&nbsp; &nbsp; pathVal{&nbsp; &nbsp; &nbsp; &nbsp; path: "mydoc/Country/City",&nbsp; &nbsp; &nbsp; &nbsp; val:&nbsp; "NY",&nbsp; &nbsp; },}func main() {&nbsp; &nbsp; out, _ := json.MarshalIndent(makeTree(data1), "", "\t")&nbsp; &nbsp; fmt.Printf("%s\n", out)&nbsp; &nbsp; fmt.Println(encode(os.Stdout, data1))&nbsp; &nbsp; out, _ = json.MarshalIndent(makeTree(data2), "", "\t")&nbsp; &nbsp; fmt.Printf("%s\n", out)&nbsp; &nbsp; fmt.Println(encode(os.Stdout, data2))}(游乐场。在示例数据(嵌入到示例中的两个“表”)上,我们得到data1data2<SalesPlan>&nbsp; &nbsp; <SalesPlanData>&nbsp; &nbsp; &nbsp; &nbsp; <id>123</id>&nbsp; &nbsp; &nbsp; &nbsp; <Year>2021</Year>&nbsp; &nbsp; &nbsp; &nbsp; <Month>July</Month>&nbsp; &nbsp; </SalesPlanData>&nbsp; &nbsp; <SalesPlanSpot>&nbsp; &nbsp; &nbsp; &nbsp; <Spots>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <City>NY</City>&nbsp; &nbsp; &nbsp; &nbsp; </Spots>&nbsp; &nbsp; </SalesPlanSpot></SalesPlan>和<mydoc>&nbsp; &nbsp; <Country>&nbsp; &nbsp; &nbsp; &nbsp; <City>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Street>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <House>15</House>Maddison&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Street>NY&nbsp; &nbsp; &nbsp; &nbsp; </City>&nbsp; &nbsp; </Country></mydoc>分别。注意那些“看起来很奇怪”的情况,“麦迪逊”放在元素里面,“NY”放在元素里面;还要注意 .我不知道如何处理这两个问题(如果它们是问题),因为你的问题没有明确说明任何关于处理它们的偏好。<Street><City><House>14</House>我会说处理多个叶元素是相当容易的 - 只需确保不覆盖 中,而是提供收集多个值(可能收集在切片中)并在 中说明这一点。如何处理一个潜在的XML元素,它应该同时包含一个“普通”值和一个嵌套元素,这实际上是一个悬而未决的问题:我所展示的是100%符合标准的XML,但最终结果可能会让人觉得不自然,因为人们通常期望在人类可读的XML文档中看到的内容。ValuemakeTreeencodeTree不过,让我把这些作为读者的练习。请注意,该解决方案仅使用包来“转储”从输入数据创建的树;它不需要算法工作。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go