Skip to content

Commit bf28e2f

Browse files
committed
Refactored parse arguments function
1 parent dd23b79 commit bf28e2f

File tree

3 files changed

+36
-85
lines changed

3 files changed

+36
-85
lines changed

engine/access/rest/websockets/data_providers/account_statuses_provider.go

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
"github.com/onflow/flow-go/engine/access/rest/common/parser"
1313
"github.com/onflow/flow-go/engine/access/rest/http/request"
14-
"github.com/onflow/flow-go/engine/access/rest/util"
1514
"github.com/onflow/flow-go/engine/access/rest/websockets/models"
1615
"github.com/onflow/flow-go/engine/access/state_stream"
1716
"github.com/onflow/flow-go/engine/access/state_stream/backend"
@@ -136,42 +135,13 @@ func parseAccountStatusesArguments(
136135
) (accountStatusesArguments, error) {
137136
var args accountStatusesArguments
138137

139-
// Check for mutual exclusivity of start_block_id and start_block_height early
140-
startBlockIDIn, hasStartBlockID := arguments["start_block_id"]
141-
startBlockHeightIn, hasStartBlockHeight := arguments["start_block_height"]
142-
143-
if hasStartBlockID && hasStartBlockHeight {
144-
return args, fmt.Errorf("can only provide either 'start_block_id' or 'start_block_height'")
145-
}
146-
147-
// Parse 'start_block_id' if provided
148-
if hasStartBlockID {
149-
result, ok := startBlockIDIn.(string)
150-
if !ok {
151-
return args, fmt.Errorf("'start_block_id' must be a string")
152-
}
153-
var startBlockID parser.ID
154-
err := startBlockID.Parse(result)
155-
if err != nil {
156-
return args, fmt.Errorf("invalid 'start_block_id': %w", err)
157-
}
158-
args.StartBlockID = startBlockID.Flow()
159-
}
160-
161-
// Parse 'start_block_height' if provided
162-
var err error
163-
if hasStartBlockHeight {
164-
result, ok := startBlockHeightIn.(string)
165-
if !ok {
166-
return args, fmt.Errorf("'start_block_height' must be a string")
167-
}
168-
args.StartBlockHeight, err = util.ToUint64(result)
169-
if err != nil {
170-
return args, fmt.Errorf("invalid 'start_block_height': %w", err)
171-
}
172-
} else {
173-
args.StartBlockHeight = request.EmptyHeight
138+
// Parse block arguments
139+
startBlockID, startBlockHeight, err := ParseStartBlock(arguments)
140+
if err != nil {
141+
return args, err
174142
}
143+
args.StartBlockID = startBlockID
144+
args.StartBlockHeight = startBlockHeight
175145

176146
// Parse 'event_types' as a JSON array
177147
var eventTypes parser.EventTypes

engine/access/rest/websockets/data_providers/blocks_provider.go

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,41 +109,52 @@ func ParseBlocksArguments(arguments models.Arguments) (blocksArguments, error) {
109109
return args, fmt.Errorf("'block_status' must be provided")
110110
}
111111

112+
// Parse block arguments
113+
startBlockID, startBlockHeight, err := ParseStartBlock(arguments)
114+
if err != nil {
115+
return args, err
116+
}
117+
args.StartBlockID = startBlockID
118+
args.StartBlockHeight = startBlockHeight
119+
120+
return args, nil
121+
}
122+
123+
func ParseStartBlock(arguments models.Arguments) (flow.Identifier, uint64, error) {
112124
startBlockIDIn, hasStartBlockID := arguments["start_block_id"]
113125
startBlockHeightIn, hasStartBlockHeight := arguments["start_block_height"]
114126

115-
// Ensure only one of start_block_id or start_block_height is provided
127+
// Check for mutual exclusivity of start_block_id and start_block_height early
116128
if hasStartBlockID && hasStartBlockHeight {
117-
return args, fmt.Errorf("can only provide either 'start_block_id' or 'start_block_height'")
129+
return flow.ZeroID, 0, fmt.Errorf("can only provide either 'start_block_id' or 'start_block_height'")
118130
}
119131

132+
// Parse 'start_block_id'
120133
if hasStartBlockID {
121134
result, ok := startBlockIDIn.(string)
122135
if !ok {
123-
return args, fmt.Errorf("'start_block_id' must be a string")
136+
return flow.ZeroID, request.EmptyHeight, fmt.Errorf("'start_block_id' must be a string")
124137
}
125138
var startBlockID parser.ID
126139
err := startBlockID.Parse(result)
127140
if err != nil {
128-
return args, err
141+
return flow.ZeroID, request.EmptyHeight, fmt.Errorf("invalid 'start_block_id': %w", err)
129142
}
130-
args.StartBlockID = startBlockID.Flow()
143+
return startBlockID.Flow(), request.EmptyHeight, nil
131144
}
132145

146+
// Parse 'start_block_height'
133147
if hasStartBlockHeight {
134148
result, ok := startBlockHeightIn.(string)
135149
if !ok {
136-
return args, fmt.Errorf("'start_block_height' must be a string")
150+
return flow.ZeroID, 0, fmt.Errorf("'start_block_height' must be a string")
137151
}
138-
var err error
139-
args.StartBlockHeight, err = util.ToUint64(result)
152+
startBlockHeight, err := util.ToUint64(result)
140153
if err != nil {
141-
return args, fmt.Errorf("invalid 'start_block_height': %w", err)
154+
return flow.ZeroID, request.EmptyHeight, fmt.Errorf("invalid 'start_block_height': %w", err)
142155
}
143-
} else {
144-
// Default value if 'start_block_height' is not provided
145-
args.StartBlockHeight = request.EmptyHeight
156+
return flow.ZeroID, startBlockHeight, nil
146157
}
147158

148-
return args, nil
159+
return flow.ZeroID, request.EmptyHeight, nil
149160
}

engine/access/rest/websockets/data_providers/events_provider.go

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
"github.com/onflow/flow-go/engine/access/rest/common/parser"
1111
"github.com/onflow/flow-go/engine/access/rest/http/request"
12-
"github.com/onflow/flow-go/engine/access/rest/util"
1312
"github.com/onflow/flow-go/engine/access/rest/websockets/models"
1413
"github.com/onflow/flow-go/engine/access/state_stream"
1514
"github.com/onflow/flow-go/engine/access/state_stream/backend"
@@ -136,42 +135,13 @@ func parseEventsArguments(
136135
) (eventsArguments, error) {
137136
var args eventsArguments
138137

139-
// Check for mutual exclusivity of start_block_id and start_block_height early
140-
startBlockIDIn, hasStartBlockID := arguments["start_block_id"]
141-
startBlockHeightIn, hasStartBlockHeight := arguments["start_block_height"]
142-
143-
if hasStartBlockID && hasStartBlockHeight {
144-
return args, fmt.Errorf("can only provide either 'start_block_id' or 'start_block_height'")
145-
}
146-
147-
// Parse 'start_block_id' if provided
148-
if hasStartBlockID {
149-
result, ok := startBlockIDIn.(string)
150-
if !ok {
151-
return args, fmt.Errorf("'start_block_id' must be a string")
152-
}
153-
var startBlockID parser.ID
154-
err := startBlockID.Parse(result)
155-
if err != nil {
156-
return args, fmt.Errorf("invalid 'start_block_id': %w", err)
157-
}
158-
args.StartBlockID = startBlockID.Flow()
159-
}
160-
161-
// Parse 'start_block_height' if provided
162-
var err error
163-
if hasStartBlockHeight {
164-
result, ok := startBlockHeightIn.(string)
165-
if !ok {
166-
return args, fmt.Errorf("'start_block_height' must be a string")
167-
}
168-
args.StartBlockHeight, err = util.ToUint64(result)
169-
if err != nil {
170-
return args, fmt.Errorf("invalid 'start_block_height': %w", err)
171-
}
172-
} else {
173-
args.StartBlockHeight = request.EmptyHeight
138+
// Parse block arguments
139+
startBlockID, startBlockHeight, err := ParseStartBlock(arguments)
140+
if err != nil {
141+
return args, err
174142
}
143+
args.StartBlockID = startBlockID
144+
args.StartBlockHeight = startBlockHeight
175145

176146
// Parse 'event_types' as a JSON array
177147
var eventTypes parser.EventTypes

0 commit comments

Comments
 (0)