-
Notifications
You must be signed in to change notification settings - Fork 8
/
parse.go
123 lines (104 loc) · 2.49 KB
/
parse.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package nha
import (
"fmt"
"regexp"
"strings"
"time"
"github.com/google/uuid"
)
var regexTransfer = regexp.MustCompile(
`^` +
`(?P<type>(?i:DPJ|EPJ|OTHER|AVL-DPJ|AVL-EPJ|AVL-OTHER))-(?i:SIP)` +
`[-_]` +
`(?:` +
`(?P<journalidentifikator>[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[1-5][a-zA-Z0-9]{3}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12})` +
`|` +
`(?P<avleveringsidentifikator>[0-9.]+)[-_](?P<date>\d{6,8})` +
`)` +
`(?P<ext>(?i:\.xml)?(?i:\.tar|\.zip))?` +
`$`,
)
type TransferType int
const (
TransferTypeUnknown TransferType = iota
TransferTypeDPJ
TransferTypeEPJ
TransferTypeOther
TransferTypeAVLXML
)
func (t TransferType) String() string {
switch t {
case TransferTypeDPJ:
return "DPJ"
case TransferTypeEPJ:
return "EPJ"
case TransferTypeOther:
return "OTHER"
case TransferTypeAVLXML:
return "AVLXML"
default:
return ""
}
}
func (t TransferType) Lower() string {
return strings.ToLower(t.String())
}
// NameInfo captures attributes from NHA SIP obtained from its name.
type NameInfo struct {
// journalidentifikator (UUID) or avleveringsidentifikator (?).
Identifier string
// Type of NHA SIP.
Type TransferType
// Date, only included when using avleveringsidentifikator.
Date time.Time
// File extension, optional.
Extension string
// Original value as matched.
Value string
}
// ParseName extracts relevant information from NHA SIPs.
func ParseName(name string) (*NameInfo, error) {
res := regexTransfer.FindStringSubmatch(name)
if res == nil {
return nil, fmt.Errorf("error parsing name: no matches")
}
info := &NameInfo{
Extension: strings.TrimPrefix(res[5], "."),
Value: res[0],
}
t := strings.ToUpper(res[1])
switch {
case t == TransferTypeDPJ.String():
info.Type = TransferTypeDPJ
case t == TransferTypeEPJ.String():
info.Type = TransferTypeEPJ
case t == TransferTypeOther.String():
info.Type = TransferTypeOther
case t == TransferTypeAVLXML.String() || strings.HasPrefix(t, "AVL-"):
info.Type = TransferTypeAVLXML
}
// journalidentifikator
if res[2] != "" {
_, err := uuid.Parse(res[2])
if err != nil {
return nil, fmt.Errorf("error parsing uuid: %s", err)
}
info.Identifier = res[2]
}
// avleveringsidentifikator + date
if res[3] != "" {
info.Identifier = res[3]
}
if res[4] != "" {
const format = "20060102"
var err error
info.Date, err = time.Parse(format, res[4])
if err != nil {
return nil, fmt.Errorf("error parsing date: %s", err)
}
}
return info, nil
}
func (i NameInfo) String() string {
return i.Value
}