-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
79 lines (73 loc) · 1.66 KB
/
utils.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
72
73
74
75
76
77
78
79
package just
import (
"bytes"
"encoding/xml"
"errors"
"io"
"os"
"path"
"reflect"
"strings"
)
// Errors
var (
ErrEmptyReader = errors.New("reader is empty")
ErrUnsupportedReader = errors.New("reader is not supported")
)
func topSeekReader(reader io.Reader, nopDetect bool) (int64, error) {
if reader != nil {
if nopDetect {
if val := reflect.ValueOf(reader); val.IsValid() {
if val.Type().Name() == "nopCloser" {
if val = val.FieldByName("Reader"); val.IsValid() {
return topSeekReader(val.Interface().(io.Reader), false)
}
}
}
}
if b, ok := reader.(*bytes.Reader); ok {
return b.Seek(0, io.SeekStart)
} else if s, ok := reader.(*strings.Reader); ok {
return s.Seek(0, io.SeekStart)
} else if f, ok := reader.(*os.File); ok {
return f.Seek(0, io.SeekStart)
}
return -1, ErrUnsupportedReader
}
return -1, ErrEmptyReader
}
func joinPaths(a string, b string) string {
if len(b) < 1 {
return a
}
c := path.Join(a, b)
if b[len(b)-1] == '/' && c[len(c)-1] != '/' {
return c + "/"
}
return c
}
// H is a shortcup for map[string]interface{} (to improve code reading).
type H map[string]interface{}
// Overriding for Marshal to XML
func (h H) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
start.Name = xml.Name{
Space: "",
Local: "data",
}
if err := e.EncodeToken(start); err != nil {
return err
}
for key, value := range h {
elem := xml.StartElement{
Name: xml.Name{Space: "", Local: key},
Attr: []xml.Attr{},
}
if err := e.EncodeElement(value, elem); err != nil {
return err
}
}
if err := e.EncodeToken(xml.EndElement{Name: start.Name}); err != nil {
return err
}
return nil
}