package main
import (
"fmt"
"io/ioutil"
"log"
"strings"
"gopkg.in/yaml.v2"
)
func getRoot(obj interface{}) map[interface{}]interface{} {
return obj.(map[interface{}]interface{})
}
func getSection(obj interface{}, field string) map[interface{}]interface{} {
dict := obj.(map[interface{}]interface{})[field].(map[interface{}]interface{})
return dict
}
func dump(obj map[interface{}]interface{}, indent ...string) {
for key, val := range obj {
if key == "Address" {
val = strings.ReplaceAll(val.(string), "\n", "\n\t\t ")
}
if indent != nil {
fmt.Println(indent[0], "\t", key, "=", val)
} else {
fmt.Println("\t", key, "=", val)
}
}
}
func getArray(obj interface{}) []interface{} {
dict := obj.([]interface{})
return dict
}
func dumpArray(obj interface{}) {
for _, field := range obj.([]interface{}) {
switch t := field.(type) {
case string:
fmt.Println("\t\t", t)
case map[interface{}]interface{}:
set := []interface{}{t["var"], t["colour"], t["Matrix"]}
// x := t["var"]
for _, x := range set {
if x != nil {
fmt.Println("\t\t", x)
}
}
}
}
}
func dumpList(obj interface{}) {
for _, x := range obj.([]interface{}) {
dump(x.(map[interface{}]interface{}))
println()
}
}
type Rec interface{}
func main() {
var rec Rec
sep := strings.Repeat("-", 40)
data, err := ioutil.ReadFile("test.yml")
if err != nil {
log.Fatal(err)
}
err = yaml.Unmarshal(data, &rec)
if err != nil {
log.Fatal(err)
}
root := getRoot(rec)
recordTitle := root["RecordTitle"]
entryNumber := root["Entry No"]
recordType := root["RecordType"]
lastSavedTime := root["LastSavedTime"]
previousVersion := root["Previous Version"]
Species := getRoot(root["Species"])
Minerals := getRoot(Species)["Minerals"]
location := getSection(rec, "Location")
collectionData := getSection(rec, "Collection Data")
acquisitionData := getSection(rec, "Acquisition Data")
analyticalData := getSection(rec, "Analytical Data")
accessionData := getSection(rec, "Accession Data")
imageData := getSection(rec, "Image Data")
fmt.Println(sep)
fmt.Println("RecordTitle =", recordTitle)
fmt.Println("Entry No =", entryNumber)
fmt.Println(sep)
fmt.Println("Species\n\tMinerals")
dumpArray(Minerals)
fmt.Println(sep)
fmt.Println("Location")
dump(location)
fmt.Println(sep)
fmt.Println("Collection Data")
dump(collectionData)
fmt.Println(sep)
fmt.Println("Acquisition Data")
dump(acquisitionData)
fmt.Println(sep)
fmt.Println("Analytical Data")
fmt.Println("\tCommentary =", analyticalData["Commentary"])
zeunerite := getRoot(analyticalData["Zeunerite"])
fmt.Println("\n\tZeunerite")
dump(zeunerite, "\t")
zippeite := getRoot(analyticalData["Zippeite"])
fmt.Println("\n\tZippeite")
dump(zippeite, "\t")
fmt.Println(sep)
fmt.Println("Accension Data")
dump(accessionData)
fmt.Println(sep)
fmt.Println("Image Data")
fmt.Println("\tMugshot =", imageData["Mugshot"], "\n")
images := imageData["Images"]
dumpList(images)
fmt.Println(sep)
fmt.Println("DB Info")
fmt.Println("\tRecordType =", recordType)
fmt.Println("\tLastSavedTime =", lastSavedTime)
fmt.Println("\tPrevious Version =", previousVersion)
fmt.Println(sep)
}