-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathhelpers.go
67 lines (54 loc) · 1.59 KB
/
helpers.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
package docxplate
import (
"bytes" // #nosec G501 - allowed weak hash
"encoding/xml"
"io"
"log"
)
func readerBytes(rdr io.ReadCloser) []byte {
buf := new(bytes.Buffer)
if rdr == nil {
log.Printf("can't read bytes from empty reader")
return nil
}
if _, err := buf.ReadFrom(rdr); err != nil {
log.Printf("can't read bytes: %s", err)
return nil
}
if err := rdr.Close(); err != nil {
log.Printf("can't close reader: %s", err)
return nil
}
return buf.Bytes()
}
// Encode struct to xml code string
func structToXMLBytes(v any) []byte {
// buf, err := xml.MarshalIndent(v, "", " ")
buf, err := xml.Marshal(v)
if err != nil {
// fmt.Printf("error: %v\n", err)
return nil
}
// This is fixing `xmlns` attribute representation after marshal
buf = bytes.ReplaceAll(buf, []byte(` xmlns:_xmlns="xmlns"`), []byte(""))
buf = bytes.ReplaceAll(buf, []byte(`_xmlns:`), []byte("xmlns:"))
buf = bytes.ReplaceAll(buf, []byte(` xmlns:r="r"`), []byte(""))
buf = bytes.ReplaceAll(buf, []byte(` xmlns:o="o"`), []byte(""))
// xml decoder doesnt support <w:t so using placeholder with "w-" (<w-t)
// Or you have solution?
buf = bytes.ReplaceAll(buf, []byte("<w-"), []byte("<w:"))
buf = bytes.ReplaceAll(buf, []byte("</w-"), []byte("</w:"))
buf = bytes.ReplaceAll(buf, []byte("<v-"), []byte("<v:"))
buf = bytes.ReplaceAll(buf, []byte("</v-"), []byte("</v:"))
// buf = bytes.Replace(buf, []byte("w-item"), []byte("w-p"), -1)
return buf
}
// Is slice contains item
func inSlice(a string, slice []string) bool {
for index := range slice {
if a == slice[index] {
return true
}
}
return false
}