-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface.go
68 lines (50 loc) · 1.64 KB
/
interface.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
package goutility
import (
"fmt"
"reflect"
)
// SprintfObject print information about an object to a string, optionally include its contents
func SprintfObject(i interface{}, contents bool) (result string) {
result = SprintfObjectInstance(i)
if contents == true {
// TODO: handle error
contents, _ := SprintfObjectContents(i)
result = StringAppendWithJoin(result, "\n", contents)
}
return
}
// SprintfObjectInstance print information about an object to a string
func SprintfObjectInstance(i interface{}) string {
return fmt.Sprintf("%s <%p>", reflect.TypeOf(i), i)
}
// SprintfObjectContents print contents of an object to a string
func SprintfObjectContents(i interface{}) (result string, error error) {
marshalResult, marshalError := MarshalToJSON(i)
error = marshalError
if marshalResult != nil && len(marshalResult) > 0 {
result = string(marshalResult)
}
return
}
// ReadObjectFromJSONFile read an object from a JSON file
func ReadObjectFromJSONFile(object interface{}, fileName string) ErrorTypeInterface {
fileContents, readFileError := ReadFile(fileName)
if readFileError != nil {
return readFileError
}
return UnmarshalFromJSON(fileContents, object)
}
// WriteObjectToJSONFile write an object to a JSON file
func WriteObjectToJSONFile(object interface{}, fileName string, pretty bool) ErrorTypeInterface {
var fileContents []byte
var marshalError ErrorTypeInterface
if pretty == true {
fileContents, marshalError = MarshalIndentToJSON(object, "", " ")
} else {
fileContents, marshalError = MarshalToJSON(object)
}
if marshalError != nil {
return marshalError
}
return WriteFile(fileName, fileContents, 0644)
}