/
okved.go
96 lines (83 loc) · 1.5 KB
/
okved.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package torefactor
import (
"bytes"
"encoding/xml"
"io/ioutil"
)
var (
OKVEDAPI OKVEDS
)
type okvedXML struct {
Okveds OKVEDS `xml:"nsiOKVEDList>nsiOKVED"`
}
type OKVEDS []OKVED
func (o OKVEDS) Get(code string) OKVED {
for _, v := range o {
if v.Code == code {
return v
}
}
return OKVED{}
}
func (o OKVEDS) GetById(id int) (OKVED, bool) {
for _, v := range o {
if v.Id == id {
return v, true
}
}
return OKVED{}, false
}
func (o OKVEDS) Parent(code string) (OKVED, bool) {
child := o.Get(code)
if child.Text == "" {
return OKVED{}, false
}
return o.GetById(child.ParentId)
}
func (o OKVEDS) Parents() (p OKVEDS) {
for _, v := range o {
if v.ParentId == 0 {
p = append(p, v)
}
}
return
}
func (o OKVEDS) SubSections(id int) (p OKVEDS) {
for _, v := range o {
if v.SubSection != "" && v.ParentId == id {
p = append(p, v)
}
}
return
}
func (o OKVEDS) Childs(id int) (p OKVEDS) {
for _, v := range o {
if v.ParentId == id {
p = append(p, v)
}
}
return
}
type OKVED struct {
Id int `xml:"id"`
ParentId int `xml:"parentId"`
Code string `xml:"code"`
Text string `xml:"name"`
Section string `xml:"section"`
SubSection string `xml:"subsection"`
Actual bool `xml:"actual"`
}
func init() {
bts, e := ioutil.ReadFile("okved.xml")
if e != nil {
panic(e)
}
bts = bytes.Replace(bts, []byte("oos:"), []byte{}, -1)
var v okvedXML
e = xml.Unmarshal(bts, &v)
if e != nil {
panic(e)
}
OKVEDAPI = v.Okveds
}
//func main() {}