func ReadFTPConfig(fn string) (rv FtpUser) { s := GetFile(fn) err := json.Unmarshal([]byte(s), &rv) if err != nil { fmt.Printf("Error(12029): Invalid format for FTP config file - %v\n", err) } return }
func ReadData(fn string) (data []map[string]interface{}) { s := GetFile(fn) err := json.Unmarshal([]byte(s), &data) if err != nil { // fmt.Fprintf ( xOut, "Data ->%s<-\n", s ) fmt.Fprintf(os.Stdout, "Error(12001): Invalid format - %v\n", err) } return }
func (a *Attachment) UnmarshalJSON(b []byte) error { var v struct { Num int `json:"num"` } if err := json.Unmarshal(b, &v); err != nil { return err } a.num = v.Num return nil }
func (this *EM) readEmailConfig(fn string) (rv EmailUser, err error) { data, err := ioutil.ReadFile(fn) if err != nil { e := fmt.Sprintf("Error(12023): File (%s) missing or unreadable error: %v\n", fn, err) err = errors.New(e) } else { err := json.Unmarshal(data, &rv) if err != nil { e := fmt.Sprintf("Error(12002): Invalid format - %v\n", err) err = errors.New(e) } } return }
func ReadInGlobalConfigRaw(path string) map[string]string { var jsonData map[string]string file, err := ioutil.ReadFile(path) if err != nil { fmt.Printf("Error(12006): Reading in global config %v\n", err) return jsonData } err = json.Unmarshal(file, &jsonData) if err != nil { fmt.Printf("Error(12007): Reading in global config %v\n", err) jsonData["error"] = "true" return jsonData } if s, ok := jsonData["static_dir"]; !ok || s == "" { jsonData["static_dir"] = "./static" } return jsonData }
func ReadInGlobalConfig(path string) map[string]string { var jsonData map[string]string file, err := ioutil.ReadFile(path) if err != nil { fmt.Printf("Error(12006): Reading in global config %v\n", err) return jsonData } err = json.Unmarshal(file, &jsonData) if err != nil { fmt.Printf("Error(12007): Reading in global config %v\n", err) jsonData["error"] = "true" return jsonData } if s, ok := jsonData["static_dir"]; !ok || s == "" { jsonData["static_dir"] = "./static" } if s, ok := jsonData["listen_at_ip_port"]; ok || s != "" { port := "80" addr := "127.0.0.1" tmp := strings.Split(s, ":") if len(tmp) == 2 { addr = tmp[0] port = tmp[1] } else if len(tmp) > 0 { addr = tmp[0] } jsonData["ip_addr"] = addr jsonData["ip_port"] = addr + ":" + port jsonData["port"] = ":" + port // fmt.Printf("addr ->%s<- port ->%s<- \n", addr, port) //,"ip_addr":"192.168.0.102" //,"ip_port":"192.168.0.102:8080" //,"port":":8080" } return jsonData }
func TestDecodeAttachments(t *testing.T) { var input [][]byte var v interface{} buf1 := bytes.NewBuffer(nil) buf2 := bytes.NewBuffer(nil) var attachment1 *Attachment var attachment2 *Attachment test := func() { err := decodeAttachments(v, input) So(err, ShouldBeNil) if attachment1 != nil { So(buf1.String(), ShouldEqual, "data1") } if attachment2 != nil { So(buf2.String(), ShouldEqual, "data2") } buf1.Reset() buf2.Reset() } Convey("No attachment", t, func() { input = nil v = NoAttachment{} test() }) Convey("Many attachment", t, func() { input = [][]byte{[]byte("data1")} attachment1 = &Attachment{Data: buf1} attachment1.num = 0 v = HaveAttachment{A: attachment1} test() }) Convey("Array of attachments", t, func() { input = [][]byte{[]byte("data1"), []byte("data2")} attachment1 = &Attachment{Data: buf1} attachment1.num = 0 attachment2 = &Attachment{Data: buf2} attachment2.num = 1 v = [...]interface{}{HaveAttachment{A: attachment1}, &HaveAttachment{A: attachment2}} test() }) Convey("Slice of attachments", t, func() { input = [][]byte{[]byte("data1"), []byte("data2")} attachment1 = &Attachment{Data: buf1} attachment1.num = 0 attachment2 = &Attachment{Data: buf2} attachment2.num = 1 v = []interface{}{HaveAttachment{A: attachment1}, &HaveAttachment{A: attachment2}} test() }) Convey("Map of attachments", t, func() { input = [][]byte{[]byte("data1"), []byte("data2")} attachment1 = &Attachment{Data: buf1} attachment1.num = 0 attachment2 = &Attachment{Data: buf2} attachment2.num = 1 v = map[string]interface{}{"test": HaveAttachment{A: attachment1}, "testp": &HaveAttachment{A: attachment2}} test() }) Convey("Deocde json", t, func() { b := []byte(`{"i":0,"a":{"_placeholder":true,"num":2}}`) v := &HaveAttachment{} err := json.Unmarshal(b, &v) So(err, ShouldBeNil) So(v.A.num, ShouldEqual, 2) }) }