Skip to content

Commit f2f0221

Browse files
committed
Move to parse
1 parent f5e9cf0 commit f2f0221

File tree

2 files changed

+165
-124
lines changed

2 files changed

+165
-124
lines changed

internal/schedule/get.go

Lines changed: 21 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ access(all) fun main(transactionID: UInt64): FlowTransactionScheduler.Transactio
121121
return nil, fmt.Errorf("failed to execute script: %w", err)
122122
}
123123

124-
txData, err := parseTransactionData(value)
124+
txData, err := ParseTransactionData(value)
125125
if err != nil {
126126
return nil, fmt.Errorf("failed to parse transaction data: %w", err)
127127
}
@@ -136,92 +136,23 @@ access(all) fun main(transactionID: UInt64): FlowTransactionScheduler.Transactio
136136
successMsg := branding.GreenStyle.Render("Transaction data retrieved successfully")
137137
logger.Info(fmt.Sprintf("%s %s", successIcon, successMsg))
138138

139-
return txData, nil
140-
}
141-
142-
// parseTransactionData parses the cadence.Value returned from the script
143-
func parseTransactionData(value cadence.Value) (*getResult, error) {
144-
// Check if result is nil (optional return)
145-
optional, ok := value.(cadence.Optional)
146-
if !ok {
147-
return nil, fmt.Errorf("expected optional value, got %T", value)
148-
}
149-
150-
if optional.Value == nil {
151-
return nil, nil // Transaction not found
152-
}
153-
154-
structValue, ok := optional.Value.(cadence.Struct)
155-
if !ok {
156-
return nil, fmt.Errorf("expected struct value, got %T", optional.Value)
157-
}
158-
159-
fields := cadence.FieldsMappedByName(structValue)
160-
161-
result := &getResult{}
162-
163-
if id, ok := fields["id"].(cadence.UInt64); ok {
164-
result.id = uint64(id)
165-
}
166-
167-
if priority, ok := fields["priority"].(cadence.Enum); ok {
168-
priorityFields := cadence.FieldsMappedByName(priority)
169-
if rawValue, ok := priorityFields["rawValue"].(cadence.UInt8); ok {
170-
result.priority = uint8(rawValue)
171-
}
172-
}
173-
174-
if effort, ok := fields["executionEffort"].(cadence.UInt64); ok {
175-
result.executionEffort = uint64(effort)
176-
}
177-
178-
if status, ok := fields["status"].(cadence.Enum); ok {
179-
statusFields := cadence.FieldsMappedByName(status)
180-
if rawValue, ok := statusFields["rawValue"].(cadence.UInt8); ok {
181-
result.status = uint8(rawValue)
182-
}
183-
}
184-
185-
if fees, ok := fields["fees"].(cadence.UFix64); ok {
186-
result.fees = fees.String()
187-
}
188-
189-
if timestamp, ok := fields["scheduledTimestamp"].(cadence.UFix64); ok {
190-
result.scheduledTimestamp = timestamp.String()
191-
}
192-
193-
if handlerType, ok := fields["handlerTypeIdentifier"].(cadence.String); ok {
194-
result.handlerTypeIdentifier = string(handlerType)
195-
}
196-
197-
if handlerAddr, ok := fields["handlerAddress"].(cadence.Address); ok {
198-
result.handlerAddress = handlerAddr.String()
199-
}
200-
201-
return result, nil
139+
return &getResult{data: txData}, nil
202140
}
203141

204142
type getResult struct {
205-
id uint64
206-
priority uint8
207-
executionEffort uint64
208-
status uint8
209-
fees string
210-
scheduledTimestamp string
211-
handlerTypeIdentifier string
212-
handlerAddress string
143+
data *TransactionData
213144
}
214145

215146
func (r *getResult) JSON() any {
216147
return map[string]any{
217-
"id": r.id,
218-
"priority": r.priority,
219-
"execution_effort": r.executionEffort,
220-
"status": r.status,
221-
"fees": r.fees,
222-
"scheduled_timestamp": r.scheduledTimestamp,
223-
"handler_type_identifier": r.handlerTypeIdentifier,
224-
"handler_address": r.handlerAddress,
148+
"id": r.data.ID,
149+
"priority": r.data.Priority,
150+
"execution_effort": r.data.ExecutionEffort,
151+
"status": r.data.Status,
152+
"fees": r.data.Fees,
153+
"scheduled_timestamp": r.data.ScheduledTimestamp,
154+
"handler_type_identifier": r.data.HandlerTypeIdentifier,
155+
"handler_address": r.data.HandlerAddress,
225156
}
226157
}
227158

@@ -230,82 +161,48 @@ func (r *getResult) String() string {
230161

231162
// ID
232163
idLabel := branding.GrayStyle.Render(" ID:")
233-
idValue := branding.PurpleStyle.Render(fmt.Sprintf("%d", r.id))
164+
idValue := branding.PurpleStyle.Render(fmt.Sprintf("%d", r.data.ID))
234165
output += fmt.Sprintf("%s %s\n", idLabel, idValue)
235166

236167
// Status
237168
statusLabel := branding.GrayStyle.Render(" Status:")
238-
statusValue := branding.GreenStyle.Render(getStatusString(r.status))
169+
statusValue := branding.GreenStyle.Render(GetStatusString(r.data.Status))
239170
output += fmt.Sprintf("%s %s\n", statusLabel, statusValue)
240171

241172
// Priority
242173
priorityLabel := branding.GrayStyle.Render(" Priority:")
243-
priorityValue := branding.PurpleStyle.Render(getPriorityString(r.priority))
174+
priorityValue := branding.PurpleStyle.Render(GetPriorityString(r.data.Priority))
244175
output += fmt.Sprintf("%s %s\n", priorityLabel, priorityValue)
245176

246177
// Execution Effort
247178
effortLabel := branding.GrayStyle.Render(" Execution Effort:")
248-
effortValue := branding.PurpleStyle.Render(fmt.Sprintf("%d", r.executionEffort))
179+
effortValue := branding.PurpleStyle.Render(fmt.Sprintf("%d", r.data.ExecutionEffort))
249180
output += fmt.Sprintf("%s %s\n", effortLabel, effortValue)
250181

251182
// Fees
252183
feesLabel := branding.GrayStyle.Render(" Fees:")
253-
feesValue := branding.PurpleStyle.Render(fmt.Sprintf("%s FLOW", r.fees))
184+
feesValue := branding.PurpleStyle.Render(fmt.Sprintf("%s FLOW", r.data.Fees))
254185
output += fmt.Sprintf("%s %s\n", feesLabel, feesValue)
255186

256187
// Scheduled Timestamp
257188
timestampLabel := branding.GrayStyle.Render(" Scheduled Timestamp:")
258-
timestampValue := branding.PurpleStyle.Render(r.scheduledTimestamp)
189+
timestampValue := branding.PurpleStyle.Render(r.data.ScheduledTimestamp)
259190
output += fmt.Sprintf("%s %s\n", timestampLabel, timestampValue)
260191

261192
// Handler Type
262193
handlerTypeLabel := branding.GrayStyle.Render(" Handler Type:")
263-
handlerTypeValue := branding.PurpleStyle.Render(r.handlerTypeIdentifier)
194+
handlerTypeValue := branding.PurpleStyle.Render(r.data.HandlerTypeIdentifier)
264195
output += fmt.Sprintf("%s %s\n", handlerTypeLabel, handlerTypeValue)
265196

266197
// Handler Address
267198
handlerAddrLabel := branding.GrayStyle.Render(" Handler Address:")
268-
handlerAddrValue := branding.PurpleStyle.Render(r.handlerAddress)
199+
handlerAddrValue := branding.PurpleStyle.Render(r.data.HandlerAddress)
269200
output += fmt.Sprintf("%s %s\n", handlerAddrLabel, handlerAddrValue)
270201

271202
return output
272203
}
273204

274205
func (r *getResult) Oneliner() string {
275-
statusStr := getStatusString(r.status)
276-
return fmt.Sprintf("Transaction %d - Status: %s", r.id, statusStr)
277-
}
278-
279-
// getStatusString converts status code to readable string
280-
func getStatusString(status uint8) string {
281-
switch status {
282-
case 0:
283-
return "Pending"
284-
case 1:
285-
return "Scheduled"
286-
case 2:
287-
return "Executing"
288-
case 3:
289-
return "Executed"
290-
case 4:
291-
return "Failed"
292-
case 5:
293-
return "Cancelled"
294-
default:
295-
return fmt.Sprintf("Unknown(%d)", status)
296-
}
297-
}
298-
299-
// getPriorityString converts priority code to readable string
300-
func getPriorityString(priority uint8) string {
301-
switch priority {
302-
case 0:
303-
return "Low"
304-
case 1:
305-
return "Medium"
306-
case 2:
307-
return "High"
308-
default:
309-
return fmt.Sprintf("Unknown(%d)", priority)
310-
}
206+
statusStr := GetStatusString(r.data.Status)
207+
return fmt.Sprintf("Transaction %d - Status: %s", r.data.ID, statusStr)
311208
}

internal/schedule/parse.go

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* Flow CLI
3+
*
4+
* Copyright Flow Foundation
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package schedule
20+
21+
import (
22+
"fmt"
23+
24+
"github.com/onflow/cadence"
25+
)
26+
27+
// TransactionData holds the parsed transaction data from the scheduler
28+
type TransactionData struct {
29+
ID uint64
30+
Priority uint8
31+
ExecutionEffort uint64
32+
Status uint8
33+
Fees string
34+
ScheduledTimestamp string
35+
HandlerTypeIdentifier string
36+
HandlerAddress string
37+
}
38+
39+
// ParseTransactionData parses the cadence.Value returned from a script into TransactionData
40+
func ParseTransactionData(value cadence.Value) (*TransactionData, error) {
41+
// Check if result is nil (optional return)
42+
optional, ok := value.(cadence.Optional)
43+
if !ok {
44+
return nil, fmt.Errorf("expected optional value, got %T", value)
45+
}
46+
47+
if optional.Value == nil {
48+
return nil, nil // Transaction not found
49+
}
50+
51+
// Cast to struct
52+
structValue, ok := optional.Value.(cadence.Struct)
53+
if !ok {
54+
return nil, fmt.Errorf("expected struct value, got %T", optional.Value)
55+
}
56+
57+
// Get fields mapped by name
58+
fields := cadence.FieldsMappedByName(structValue)
59+
60+
// Parse individual fields
61+
result := &TransactionData{}
62+
63+
// ID (UInt64)
64+
if id, ok := fields["id"].(cadence.UInt64); ok {
65+
result.ID = uint64(id)
66+
}
67+
68+
// Priority (Enum with rawValue)
69+
if priority, ok := fields["priority"].(cadence.Enum); ok {
70+
priorityFields := cadence.FieldsMappedByName(priority)
71+
if rawValue, ok := priorityFields["rawValue"].(cadence.UInt8); ok {
72+
result.Priority = uint8(rawValue)
73+
}
74+
}
75+
76+
// Execution Effort (UInt64)
77+
if effort, ok := fields["executionEffort"].(cadence.UInt64); ok {
78+
result.ExecutionEffort = uint64(effort)
79+
}
80+
81+
// Status (Enum with rawValue)
82+
if status, ok := fields["status"].(cadence.Enum); ok {
83+
statusFields := cadence.FieldsMappedByName(status)
84+
if rawValue, ok := statusFields["rawValue"].(cadence.UInt8); ok {
85+
result.Status = uint8(rawValue)
86+
}
87+
}
88+
89+
// Fees (UFix64)
90+
if fees, ok := fields["fees"].(cadence.UFix64); ok {
91+
result.Fees = fees.String()
92+
}
93+
94+
// Scheduled Timestamp (UFix64)
95+
if timestamp, ok := fields["scheduledTimestamp"].(cadence.UFix64); ok {
96+
result.ScheduledTimestamp = timestamp.String()
97+
}
98+
99+
// Handler Type Identifier (String)
100+
if handlerType, ok := fields["handlerTypeIdentifier"].(cadence.String); ok {
101+
result.HandlerTypeIdentifier = string(handlerType)
102+
}
103+
104+
// Handler Address (Address)
105+
if handlerAddr, ok := fields["handlerAddress"].(cadence.Address); ok {
106+
result.HandlerAddress = handlerAddr.String()
107+
}
108+
109+
return result, nil
110+
}
111+
112+
// GetStatusString converts status code to readable string
113+
func GetStatusString(status uint8) string {
114+
switch status {
115+
case 0:
116+
return "Pending"
117+
case 1:
118+
return "Scheduled"
119+
case 2:
120+
return "Executing"
121+
case 3:
122+
return "Executed"
123+
case 4:
124+
return "Failed"
125+
case 5:
126+
return "Cancelled"
127+
default:
128+
return fmt.Sprintf("Unknown(%d)", status)
129+
}
130+
}
131+
132+
// GetPriorityString converts priority code to readable string
133+
func GetPriorityString(priority uint8) string {
134+
switch priority {
135+
case 0:
136+
return "Low"
137+
case 1:
138+
return "Medium"
139+
case 2:
140+
return "High"
141+
default:
142+
return fmt.Sprintf("Unknown(%d)", priority)
143+
}
144+
}

0 commit comments

Comments
 (0)