Golang,Firebase ...将MAP转换为结构切片

时间:2018-10-03 22:11:19

标签: firebase go google-cloud-firestore admin-sdk

如何将文档映射快照从Firestore转换为结构片?在此示例中未显示该结构,但问题代码位于页面末尾。

此代码有效,但底部的“标记代码”除外。代码与FireStore数据库进行通信,并返回一个称为COMPLEX_NONACS的复杂结构。基本上,我想转换。将快照存储到json golang对象中。

package main

import (
    "context"
    "fmt"
    "log"

    "firebase.google.com/go"
    "google.golang.org/api/iterator"
    "google.golang.org/api/option"
)

func check(e error) {
    if e != nil {
        panic(e)
    }
}
func main() {
    ctx := context.Background()
    sa := option.WithCredentialsFile("./scai-qit-fb-adminsdk.json")
    app, err := firebase.NewApp(ctx, nil, sa)
    if err != nil {
        log.Fatalf("error initializing app: %v\n", err)
    }
    client, err := app.Firestore(ctx)
    if err != nil {
        log.Fatal(err)
    }
    defer client.Close()

    CompNACS := []string{}
    var SCNACS COMPLEX_NONACS // single Complex NONACS
    var MCNACS []COMPLEX_NONACS

    iter := client.Collection("COMPLEX_NONACS").Documents(ctx)
    for {
        doc, err := iter.Next()
        if err == iterator.Done {
            break
        }
        if err != nil {
            log.Fatalf("Failed to iterate: %v", err)
        }
        //This part works.  WIll return a Map of each Document
        fmt.Println("--------------------------/n")
        fmt.Println(doc.Ref.ID)
        fmt.Println("--/n")
        fmt.Println(doc.Data())

        //  PUT ANSWER HERE...
        // doc.Data() should return map[String] interface{} that is
        // actually of a struct TYPE COMPLEX_NONACS.
        // Question is there a simpler way to CAST doc.Data() as
        // a type?  Docs say this...
        // Data returns the DocumentSnapshot's fields as a map.
        // It is equivalent to
        //     var m map[string]interface{}
        //     d.DataTo(&m)
        // except that it returns nil if the document does not exist.

        //---   this is the error ---
        //  The problem cannot convert to COMPLEX_NONACS  
        SCNACS = COMPLEX_NONACS(doc.Data())
        fmt.Println(SCNACS)

        CompNACS = append(CompNACS, doc.Ref.ID)
    }
}

/ ========================== 前两个文档...
集合中所有文档的结构都相同,但是某些文档只是具有不同的字段组合。我会列出该结构,但它很大。

--------------------------/n
A01B01C01D01
--/n
map[E01PCI: DE03CABG:map[Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil>] E06CPCI: DE06CCABG:map[SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name:] DE01PCI:map[Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil>] DE06CPCI:map[Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS:] DF03CABGPCI:map[Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS:] DE05CCABG:map[SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name:] DE04PCI:map[SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC:] DE07BPRIMARY:map[Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil>] Q04PRIORCABG:No Previous CABG DF03PRIMARY:map[TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT:] E06BPCI: E05BCABG: E06CCABG: DF04PRIMARY:map[SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION:] DE05APCI:map[SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION:] E03PCI: F03CABGPCI: DE06CABG:map[Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil>] DE05CPCI:map[SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC:] E04PRIMARY: DE05BPCI:map[Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil>] DE04CABG:map[SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION:] Q03:C01 E06APRIMARY:48-Asymptomatic DE06PCI:map[Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS:] F03CABG: E05ACABG: DEADEND: DE04PRIMARY:map[Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS:] E05CPCI: Q01IschemicSymptoms:Asymptomatic (No ischemic symptoms) DE02PCI:map[SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION:] INDEX:A01B01C01D01 E05PRIMARY: F01PRIMARY: E01PRIMARY: Q03NonInvasiveTestResults:No non-invasive testing performed E03PRIMARY: DE03PRIMARY:map[SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION:] Q02AntiIschemicMedicalTherapy:No Therapy DF04PCI:map[INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE:] DF01PRIMARY:map[SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION:] F04PCI: E05CCABG: DE07CCABG:map[SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC:] E07CCABG: E07PRIMARY:49-Asymptomatic F03PRIMARY: E05CPRIMARY:23-Asymptomatic E07BPRIMARY: E07BCABG: DE06PRIMARY:map[TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT:] DE07CABG:map[INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE:] DF02PRIMARY:map[SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION:] DE05PRIMARY:map[TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT:] DE06APCI:map[Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil>] DF03CABG:map[Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil>] E06ACABG: E06BPRIMARY: E07APCI: DE06APRIMARY:map[SCOREGRAPHIC:08 SCORE:08 INDICATION:48 SCOREDEFINITIONS:48 Q01CAT:Asymptomatic TermDescription:[48. Three-vessel CAD (no left main) Abnormal LV systolic function -] Name:48-Asymptomatic] DF04CABG:map[SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name:] E06APCI: E02PCI: E05PCI: DE07ACABG:map[Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil>] E04CABG: DE05ACABG:map[INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE:] DE07APRIMARY:map[SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC:] DE06BCABG:map[SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name:] F04CABG: Q02:B01 DE07PRIMARY:map[TermDescription:[49. Left main stenosis  -] Name:49-Asymptomatic SCOREGRAPHIC:09 SCORE:09 INDICATION:49 SCOREDEFINITIONS:49 Q01CAT:Asymptomatic] DE07PCI:map[Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS:] E06PCI: DE06BPRIMARY:map[INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE:] E02PRIMARY:20-Asymptomatic DE06ACABG:map[INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE:] DE05BCABG:map[SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name:] E07CABG: DE07CPRIMARY:map[SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC:] DE05BPRIMARY:map[SCOREGRAPHIC:03 SCORE:03 INDICATION:22 SCOREDEFINITIONS:22 Q01CAT:Asymptomatic TermDescription:[22. One- or 2-vessel CAD with borderline stenosis 50% to 60% No noninvasive testing performed or equivocal test results present FFR less than or equal to 0.80* and/or IVUS with significant reduction in] Name:22-Asymptomatic] E02CABG: E04PCI: DE05APRIMARY:map[SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION:] DE05CPRIMARY:map[SCORE:01 INDICATION:23 SCOREDEFINITIONS:23 Q01CAT:Asymptomatic TermDescription:[23. One- or 2-vessel CAD with borderline stenosis 50% to 60% No noninvasive testing performed or equivocal test results present FFR or IVUS findings do not meet criteria for significant stenosis] Name:23-Asymptomatic SCOREGRAPHIC:01] DE07BCABG:map[SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name:] E01CABG: E05BPCI: E05BPRIMARY:22-Asymptomatic DE07CPCI:map[SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC:] DE02PRIMARY:map[Q01CAT:Asymptomatic TermDescription:[20. One- or 2-vessel CAD without involvement of proximal LAD No noninvasive testing performed -] Name:20-Asymptomatic SCOREGRAPHIC:03 SCORE:03 INDICATION:20 SCOREDEFINITIONS:201] DE01CABG:map[SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC:] Q01:A01 E03CABG: E06PRIMARY: DE06CPRIMARY:map[Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS:] DE06BPCI:map[Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil>] DE02CABG:map[SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name:] DF04CABGPCI:map[SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC:] F02PRIMARY: E05APCI: E07APRIMARY: F04PRIMARY: E05CABG: E06CPRIMARY: E06CABG: DE07APCI:map[TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT:] Q04:D01 DE01PRIMARY:map[Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS:] E07CPRIMARY: F04CABGPCI: E07CPCI: E05APRIMARY: DE05PCI:map[TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT:] DF03PCI:map[Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil>] F03PCI: E07ACABG: DE05CABG:map[SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC:] Title:NACS-100 DE03PCI:map[SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC:] E07BPCI: E07PCI: DE07BPCI:map[SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION:] E06BCABG:]
--------------------------/n
A01B01C01D02
--/n
map[DF03PCI:map[SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC:] F04PRIMARY: DEADEND:NR DE05CPRIMARY:map[TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT:] DE05CPCI:map[SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION:] DE07BPCI:map[SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION:] F02PRIMARY: DE07CPRIMARY:map[SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name:] DE07CABG:map[Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS:] E02PRIMARY: DE05BCABG:map[INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE:] E01PCI: Q03NonInvasiveTestResults:No non-invasive testing performed E07BPRIMARY: E06CPCI: E03PRIMARY: E06CCABG: E06APRIMARY: E07PCI: DE05BPCI:map[Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS:] E01CABG: DE05CCABG:map[Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS:] DF04CABGPCI:map[SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC:] DE07ACABG:map[Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS:] E04CABG: DE05PCI:map[SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC:] DE07APCI:map[Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil>] E05CABG: INDEX:A01B01C01D02 E04PCI: DE07CPCI:map[SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name:] E06PCI: E05CCABG: F03PRIMARY: F04CABGPCI: E05APCI: E05BPRIMARY: DE06CPCI:map[Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS:] DF03CABGPCI:map[Q01CAT: TermDescription:[] Name:69 SCOREGRAPHIC: SCORE: INDICATION:69 SCOREDEFINITIONS:] E07CPCI: DE06APCI:map[SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name:] DE05ACABG:map[SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name:] DE05APRIMARY:map[SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION:] DE06BPCI:map[TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT:] DF04CABG:map[SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name:] DE03PCI:map[SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC:] DE07CCABG:map[Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil>] DE06ACABG:map[SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name:] F03PCI: DE06PRIMARY:map[SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name:] E07CABG: E05BCABG: DE06BCABG:map[Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS:] F03CABGPCI:69 DE06BPRIMARY:map[SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC:] F04CABG: E05PRIMARY: Title:NACS-500 DE07APRIMARY:map[Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil>] E06ACABG: E03PCI: DE02CABG:map[Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS:] DE06CPRIMARY:map[SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION:] DE07BPRIMARY:map[SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name:] F01PRIMARY: DE01PCI:map[Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS:] DE06APRIMARY:map[SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name:] E07APCI: DE06CABG:map[INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE:] DE07BCABG:map[SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC:] DF02PRIMARY:map[SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name:] E04PRIMARY: DF04PRIMARY:map[Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil>] DE05PRIMARY:map[SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION:] E06CPRIMARY: Q02AntiIschemicMedicalTherapy:No Therapy E06PRIMARY: E01PRIMARY: Q04PRIORCABG:Previous CABG E05CPCI: DF01PRIMARY:map[Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil>] E07APRIMARY: E06BPRIMARY: E05CPRIMARY: DE03PRIMARY:map[TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT:] E06CABG: E03CABG: DE02PCI:map[Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS:] F03CABG: E05APRIMARY: DF04PCI:map[Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS:] DE05CABG:map[TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT:] DE05APCI:map[SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION:] E02PCI: Q01IschemicSymptoms:Asymptomatic (No ischemic symptoms) E07PRIMARY: DE04CABG:map[INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE:] E06APCI: E06BCABG: DE02PRIMARY:map[Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil>] DE06PCI:map[INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE:] DF03CABG:map[SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC:] DE07PCI:map[SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION:] Q03:C01 E05PCI: DE06CCABG:map[SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name:] DE04PCI:map[Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil>] E07BCABG: E07CPRIMARY: E02CABG: DE03CABG:map[SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name:] F04PCI: E07ACABG: DE04PRIMARY:map[SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name:] Q04:D02 E05BPCI: E07CCABG: DE05BPRIMARY:map[SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name:] DF03PRIMARY:map[Q01CAT: TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS:] DE01PRIMARY:map[SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil> Name:] E05ACABG: Q01:A01 E07BPCI: DE01CABG:map[TermDescription:<nil> Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT:] Q02:B01 DE07PRIMARY:map[Name: SCOREGRAPHIC: SCORE: INDICATION: SCOREDEFINITIONS: Q01CAT: TermDescription:<nil>] E06BPCI:]

1 个答案:

答案 0 :(得分:1)

您似乎正在从Firebase获取类型为map[string]interface{}的结果数据,并且需要将其转换为类型map[string]*Foo(其中Foo是在其他地方定义的某些结构)。

如果可以轻松地将Foo结构反序列化为某种中间格式,则可以序列化未类型化的值,然后将其反序列化为所需类型的变量。

例如,使用JSON(Go Playground):

docdata := getDocData()
for label, data := range docdata {
    // Marshal the untyped data into a suitable format.
    bs, err := json.Marshal(data)
    check(err)
    // Unmarshal the formatted data into a variable with desired type.
    var foo *Foo
    err = json.Unmarshal(bs, &foo)
    check(err)
    fmt.Printf("OK: label=%q, foo=%#v\n", label, foo)
}
// OK: label="E01PCI", foo=(*main.Foo)(nil)
// OK: label="E06CPCI", foo=(*main.Foo)(nil)
// OK: label="DE03CABG", foo=&main.Foo{Name:"Alpha", SCOREGRAPHIC:"Bravo", SCORE:3.14159, INDICATION:true, SCOREDEFINITIONS:"Echo", Q01CAT:6, TermDescription:(*string)(nil)}
// OK: label="DE06CCABG", foo=&main.Foo{Name:"Hotel", SCOREGRAPHIC:"India", SCORE:10.1, INDICATION:true, SCOREDEFINITIONS:"Lima", Q01CAT:13, TermDescription:(*string)(0x1050c3d0)}

从您的问题中尚不清楚doc.Data()中的数据是您提到的整个结构还是其他结构化数据,其中包括结构实例(我的示例假定是后者);但是,序列化为中间格式并反序列化为类型变量的一般想法应该以任何一种方式起作用。