-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
filter-processor: move reflection into generated code #1463
Conversation
* Don't add entries for tags in the middle. * Add type casting. * Add manual ignores for things like signatures.
"txn.votekey": true, | ||
"txn.selkey": true, | ||
"txn.sprfkey": true, | ||
// no point in filtering on state proof things? | ||
"txn.sp.c": true, | ||
"txn.sp.S.pth": true, | ||
"txn.sp.S.hsh": true, | ||
"txn.sp.S.hsh.t": true, | ||
"txn.sp.P.pth": true, | ||
"txn.sp.P.hsh": true, | ||
"txn.sp.P.hsh.t": true, | ||
"txn.sp.r": true, | ||
"txn.sp.pr": true, | ||
"txn.spmsg.b": true, | ||
"txn.spmsg.v": true, | ||
// inner transactions are handled differently | ||
"dt.itx": true, | ||
// TODO: this can be removed if the sub fields are supported | ||
"dt": true, | ||
// TODO: support map types? | ||
"dt.gd": true, | ||
"dt.ld": true, | ||
// TODO: support array types? | ||
"txn.apaa": true, | ||
"txn.apat": true, | ||
"txn.apfa": true, | ||
"txn.apbx": true, | ||
"txn.apas": true, | ||
"txn.apap": true, | ||
"txn.apsu": true, | ||
"dt.lg": true, | ||
} | ||
|
||
func noCast(t reflect.StructField) bool { | ||
switch reflect.New(t.Type).Elem().Interface().(type) { | ||
case uint64: | ||
return true | ||
case int64: | ||
return true | ||
case string: | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func simpleCast(t reflect.StructField) string { | ||
switch reflect.New(t.Type).Elem().Interface().(type) { | ||
// unsigned | ||
case uint: | ||
return "uint64" | ||
case uint8: | ||
return "uint64" | ||
case uint16: | ||
return "uint64" | ||
case uint32: | ||
return "uint64" | ||
// signed | ||
case int: | ||
return "int64" | ||
case int8: | ||
return "int64" | ||
case int16: | ||
return "int64" | ||
case int32: | ||
return "int64" // | ||
// alias | ||
// SDK types | ||
case types.MicroAlgos: | ||
// SDK microalgo does not need ".Raw" | ||
return "uint64" | ||
case types.AssetIndex: | ||
return "uint64" | ||
case types.AppIndex: | ||
return "uint64" | ||
case types.Round: | ||
return "uint64" | ||
case types.OnCompletion: | ||
return "uint64" | ||
case types.StateProofType: | ||
return "uint64" | ||
case types.TxType: | ||
return "string" | ||
// go-algorand types | ||
case basics.AssetIndex: | ||
return "uint64" | ||
case basics.AppIndex: | ||
return "uint64" | ||
case basics.Round: | ||
return "uint64" | ||
case transactions.OnCompletion: | ||
return "uint64" | ||
case protocol.StateProofType: | ||
return "uint64" | ||
case protocol.TxType: | ||
return "string" | ||
|
||
} | ||
return "" | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a lot, but it really isn't. The code is setup so that you re-run the generator and address un-handled types until everything is handled.
const templateStr = `// Code generated via go generate. DO NOT EDIT. | ||
|
||
package {{ .PackageName }} | ||
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
|
||
"github.com/algorand/go-algorand/data/transactions" | ||
) | ||
|
||
// LookupFieldByTag takes a tag and associated SignedTxnInBlock and returns the value | ||
// referenced by the tag. An error is returned if the tag does not exist | ||
func LookupFieldByTag(tag string, input *transactions.SignedTxnInBlock) (interface{}, error) { | ||
switch tag { | ||
{{ range .StructFields }} case "{{ .TagPath }}": | ||
value := {{ ReturnValue . "input" }} | ||
return &value, nil | ||
{{ end }} default: | ||
return nil, fmt.Errorf("unknown tag: %s", tag) | ||
} | ||
} | ||
` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now using the text/template
package instead of string building.
Codecov Report
@@ Coverage Diff @@
## develop #1463 +/- ##
===========================================
- Coverage 65.37% 64.82% -0.56%
===========================================
Files 83 84 +1
Lines 11414 11462 +48
===========================================
- Hits 7462 7430 -32
- Misses 3381 3464 +83
+ Partials 571 568 -3
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would maybe like more unit tests here. Or maybe we should just be marking any gen/
files as excluded since they're sort of tested by outputting the generated code itself?
@@ -0,0 +1 @@ | |||
package main |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this do anything?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. I was thinking about adding tests, I'll do that in the next PR.
Summary
These changes focus on updating the generated
SignedTxnFunc
function.LookupFieldByTag
.uint64
,int64
andstring
.txn
was a valid tag, now only leaves are allowed such astxn.fee
.String()
function can no longer be called, because it happens inside the generated code).Followup:
Test Plan
There are extensive unit tests to cover regressions.