-
Notifications
You must be signed in to change notification settings - Fork 4
/
request.go
98 lines (90 loc) · 3.78 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
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
/*
Copyright [2019] - [2020], PERSISTENCE TECHNOLOGIES PTE. LTD. and the persistenceSDK contributors
SPDX-License-Identifier: Apache-2.0
*/
package define
import (
"encoding/json"
"github.com/asaskevich/govalidator"
"github.com/cosmos/cosmos-sdk/client/context"
sdkTypes "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/persistenceOne/persistenceSDK/constants/flags"
"github.com/persistenceOne/persistenceSDK/schema/helpers"
"github.com/persistenceOne/persistenceSDK/schema/types/base"
)
type transactionRequest struct {
BaseReq rest.BaseReq `json:"baseReq"`
FromID string `json:"fromID" valid:"required~required field fromID missing"`
ImmutableMetaTraits string `json:"immutableMetaTraits" valid:"required~required field immutableMetaTraits missing matches(^[A-Za-z]$)~invalid field immutableMetaProperties"`
ImmutableTraits string `json:"immutableTraits" valid:"required~required field immutableTraits missing matches(^[A-Za-z]$)~invalid field immutableProperties"`
MutableMetaTraits string `json:"mutableMetaTraits" valid:"required~required field mutableMetaTraits missing matches(^[A-Za-z]$)~invalid field mutableMetaProperties"`
MutableTraits string `json:"mutableTraits" valid:"required~required field mutableTraits missing matches(^[A-Za-z]$)~invalid field mutableProperties"`
}
var _ helpers.TransactionRequest = (*transactionRequest)(nil)
func (transactionRequest transactionRequest) Validate() error {
_, Error := govalidator.ValidateStruct(transactionRequest)
return Error
}
func (transactionRequest transactionRequest) FromCLI(cliCommand helpers.CLICommand, cliContext context.CLIContext) (helpers.TransactionRequest, error) {
return newTransactionRequest(
cliCommand.ReadBaseReq(cliContext),
cliCommand.ReadString(flags.FromID),
cliCommand.ReadString(flags.ImmutableMetaTraits),
cliCommand.ReadString(flags.ImmutableTraits),
cliCommand.ReadString(flags.MutableMetaTraits),
cliCommand.ReadString(flags.MutableTraits),
), nil
}
func (transactionRequest transactionRequest) FromJSON(rawMessage json.RawMessage) (helpers.TransactionRequest, error) {
if Error := json.Unmarshal(rawMessage, &transactionRequest); Error != nil {
return nil, Error
}
return transactionRequest, nil
}
func (transactionRequest transactionRequest) GetBaseReq() rest.BaseReq {
return transactionRequest.BaseReq
}
func (transactionRequest transactionRequest) MakeMsg() (sdkTypes.Msg, error) {
from, Error := sdkTypes.AccAddressFromBech32(transactionRequest.GetBaseReq().From)
if Error != nil {
return nil, Error
}
immutableMetaTraits, Error := base.ReadMetaProperties(transactionRequest.ImmutableMetaTraits)
if Error != nil {
return nil, Error
}
immutableTraits, Error := base.ReadProperties(transactionRequest.ImmutableTraits)
if Error != nil {
return nil, Error
}
mutableMetaTraits, Error := base.ReadMetaProperties(transactionRequest.MutableMetaTraits)
if Error != nil {
return nil, Error
}
mutableTraits, Error := base.ReadProperties(transactionRequest.MutableTraits)
if Error != nil {
return nil, Error
}
return newMessage(
from,
base.NewID(transactionRequest.FromID),
immutableMetaTraits,
immutableTraits,
mutableMetaTraits,
mutableTraits,
), nil
}
func requestPrototype() helpers.TransactionRequest {
return transactionRequest{}
}
func newTransactionRequest(baseReq rest.BaseReq, fromID string, immutableMetaTraits string, immutableTraits string, mutableMetaTraits string, mutableTraits string) helpers.TransactionRequest {
return transactionRequest{
BaseReq: baseReq,
FromID: fromID,
ImmutableMetaTraits: immutableMetaTraits,
ImmutableTraits: immutableTraits,
MutableMetaTraits: mutableMetaTraits,
MutableTraits: mutableTraits,
}
}