-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
54 lines (48 loc) · 1.44 KB
/
init.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
package libinpibilan
import (
"encoding/xml"
"io"
"io/ioutil"
)
var SCHEMA = BuildCodesInpi()
// NewBilanWithReader convertir une source XML en libinpibilan.Bilan
func NewBilanWithReader(reader io.Reader) (Bilan, error) {
data, err := ioutil.ReadAll(reader)
if err != nil {
return Bilan{}, ErreurLectureSourceImpossible{wrap(err)}
}
var xmlBilans XMLBilans
err = xml.Unmarshal(data, &xmlBilans)
if err != nil {
return Bilan{}, ErreurConversionImpossible{wrap(err)}
}
bilan := xmlBilans.BuildBilan()
return bilan, nil
}
// NewBilanWithBytes convertir une source XML en libinpibilan.Bilan
func NewBilanWithBytes(data []byte) (Bilan, error) {
var xmlBilans XMLBilans
err := xml.Unmarshal(data, &xmlBilans)
if err != nil {
return Bilan{}, ErreurConversionImpossible{wrap(err)}
}
bilan := xmlBilans.BuildBilan()
return bilan, nil
}
// NewBilanRoutineWithBytesChan fournit une goroutine traitant les sources XML envoyées.
// La routine s'interrompt lorsque bytesChannel est fermé.
func NewBilanRoutineWithBytesChan(bytesChannel chan []byte, chanSize int) chan Bilan {
var bilanChan = make(chan Bilan, chanSize)
go bilanRoutine(bytesChannel, bilanChan)
return bilanChan
}
func bilanRoutine(bytesChannel chan []byte, bilanChannel chan Bilan) {
for bilanBytes := range bytesChannel {
bilan, err := NewBilanWithBytes(bilanBytes)
if err != nil {
bilan.RapportConversion = []error{err}
}
bilanChannel <- bilan
}
close(bilanChannel)
}