-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparserfrom.go
71 lines (59 loc) · 1.42 KB
/
parserfrom.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package parseandgo
import (
"io"
"io/ioutil"
"net/http"
"os"
)
type ParserFrom interface {
parseFrom() []byte
}
type ParserFromURL struct {
address string
}
func newParserFromURL(address string) ParserFromURL {
return ParserFromURL{address: address}
}
func (parserFromURL ParserFromURL) parseFrom() []byte {
response, err := http.Get(parserFromURL.address)
if err != nil {
panic(couldNotGetConfigurationFromURL(err))
}
body, readAllBodyError := ioutil.ReadAll(response.Body)
defer func(Body io.ReadCloser) {
bodyCloseError := Body.Close()
logError(bodyCloseError)
}(response.Body)
logError(readAllBodyError)
return body
}
type ParserFromFilePath struct {
address string
}
func newParserFromFilePath(address string) ParserFromFilePath {
return ParserFromFilePath{address: address}
}
func (parserFromFilePath ParserFromFilePath) parseFrom() []byte {
file, fileOpenError := os.Open(parserFromFilePath.address)
defer func(file *os.File) {
err := file.Close()
logError(err)
}(file)
logError(fileOpenError)
body, fileReadError := ioutil.ReadAll(file)
logError(fileReadError)
return body
}
func newParserFrom(address string, addressType AddressType) ParserFrom {
switch addressType {
case URL:
return newParserFromURL(address)
case FILEPATH:
return newParserFromFilePath(address)
default:
panic(noSuchParserDefined())
}
}
func parseFrom(parserFrom ParserFrom) []byte {
return parserFrom.parseFrom()
}