github.com/hscells/guru@v0.0.0-20200207042420-2dabeb950d69/tarprotocol.go (about) 1 package guru 2 3 import ( 4 "encoding/gob" 5 "encoding/xml" 6 "io/ioutil" 7 "os" 8 "path" 9 "strings" 10 ) 11 12 // Protocol is a representation of a systematic review Protocol in XML. 13 type Protocol struct { 14 Objective string `xml:"objective"` 15 TypeOfStudy string `xml:"type_of_study"` 16 Participants string `xml:"participants"` 17 IndexTests string `xml:"index_tests"` 18 TargetConditions string `xml:"target_conditions"` 19 ReferenceStandards string `xml:"reference_standards"` 20 } 21 22 type Protocols map[string]Protocol 23 24 func ReadAndWriteProtocols(protocolsDir, protocolsBinFile string) Protocols { 25 gob.Register(Protocol{}) 26 gob.Register(Protocols{}) 27 28 // First, get a list of files in the directory. 29 files, err := ioutil.ReadDir(protocolsDir) 30 if err != nil { 31 panic(err) 32 } 33 34 protocols := make(Protocols) 35 for _, f := range files { 36 if f.IsDir() { 37 continue 38 } 39 40 if len(f.Name()) == 0 { 41 continue 42 } 43 44 p := path.Join(protocolsDir, f.Name()) 45 source, err := ioutil.ReadFile(p) 46 if err != nil { 47 panic(err) 48 } 49 50 var protocol Protocol 51 err = xml.Unmarshal(source, &protocol) 52 if err != nil { 53 panic(err) 54 } 55 56 _, topic := path.Split(p) 57 58 protocols[strings.TrimSpace(topic)] = protocol 59 } 60 f, err := os.OpenFile(protocolsBinFile, os.O_CREATE|os.O_WRONLY, 0644) 61 if err != nil { 62 panic(err) 63 } 64 defer f.Close() 65 err = gob.NewEncoder(f).Encode(protocols) 66 if err != nil { 67 panic(err) 68 } 69 return protocols 70 } 71 72 func LoadProtocols(protocolsBinFile string) Protocols { 73 gob.Register(Protocol{}) 74 gob.Register(Protocols{}) 75 76 f, err := os.OpenFile(protocolsBinFile, os.O_RDONLY, 0644) 77 if err != nil { 78 panic(err) 79 } 80 defer f.Close() 81 var protocols Protocols 82 err = gob.NewDecoder(f).Decode(&protocols) 83 if err != nil { 84 panic(err) 85 } 86 return protocols 87 }