-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
60 lines (53 loc) · 1.6 KB
/
request.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
package main
import (
"encoding/json"
"encoding/xml"
"fmt"
)
type RequestXML struct {
XMLName xml.Name `xml:"XML"`
Text string `xml:",chardata"`
MessageType string `xml:"MessageType"`
ProcCode string `xml:"ProcCode"`
RefNum string `xml:"REFNUM"`
Stan string `xml:"STAN"`
RequestTime string `xml:"LocalTxnDtTime"`
ChanelID string `xml:"DeliveryChannelCtrlID"`
ParameterName string `xml:"PName"`
ParameterValue string `xml:"PValue"`
}
type RequestJSON struct {
Info RequestInfo `json:"RequestInfo"`
ParameterName string `json:"searchparametername"`
ParameterValue string `json:"searchparametervalue"`
}
type RequestInfo struct {
Stan string `json:"requestId"`
UserID string `json:"userId,omitempty"`
BaseNumber string `json:"basenumber"`
ChanelID string `json:"chanelId"`
RequestTime string `json:"requestTime"`
}
func RequestX2J(req []byte) ([]byte, error) {
xmlReq := &RequestXML{}
err := xml.Unmarshal(req, xmlReq)
if err != nil {
return []byte{}, fmt.Errorf("request xml unmarshal: %w", err)
}
jsonReq := &RequestJSON{
Info: RequestInfo{
Stan: xmlReq.Stan,
UserID: xmlReq.RefNum,
BaseNumber: xmlReq.ParameterValue,
ChanelID: xmlReq.ChanelID,
RequestTime: xmlReq.RequestTime,
},
ParameterName: "Baseno",
ParameterValue: xmlReq.ParameterValue,
}
result, err := json.Marshal(jsonReq)
if nil != err {
return []byte{}, fmt.Errorf("request json marshal: %w", err)
}
return result, nil
}