Skip to content

Commit 95a26ba

Browse files
committed
feat: rlpgen -internal_methods flag
1 parent 380aa31 commit 95a26ba

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

rlp/rlpgen/gen.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ type buildContext struct {
3535
rawValueType *types.Named
3636

3737
typeToStructCache map[types.Type]*rlpstruct.Type
38+
39+
internalMethods bool
3840
}
3941

4042
func newBuildContext(packageRLP *types.Package) *buildContext {
@@ -98,6 +100,8 @@ type genContext struct {
98100
inPackage *types.Package
99101
imports map[string]struct{}
100102
tempCounter int
103+
104+
internalMethods bool
101105
}
102106

103107
func newGenContext(inPackage *types.Package) *genContext {
@@ -736,7 +740,7 @@ func generateDecoder(ctx *genContext, typ string, op op) []byte {
736740

737741
result, code := op.genDecode(ctx)
738742
var b bytes.Buffer
739-
fmt.Fprintf(&b, "func (obj *%s) DecodeRLP(dec *rlp.Stream) error {\n", typ)
743+
fmt.Fprintf(&b, "func (obj *%s) %s(dec *rlp.Stream) error {\n", typ, ctx.decoderMethod())
740744
fmt.Fprint(&b, code)
741745
fmt.Fprintf(&b, " *obj = %s\n", result)
742746
fmt.Fprintf(&b, " return nil\n")
@@ -751,7 +755,7 @@ func generateEncoder(ctx *genContext, typ string, op op) []byte {
751755
ctx.addImport(pathOfPackageRLP)
752756

753757
var b bytes.Buffer
754-
fmt.Fprintf(&b, "func (obj *%s) EncodeRLP(_w io.Writer) error {\n", typ)
758+
fmt.Fprintf(&b, "func (obj *%s) %s(_w io.Writer) error {\n", typ, ctx.encoderMethod())
755759
fmt.Fprintf(&b, " w := rlp.NewEncoderBuffer(_w)\n")
756760
fmt.Fprint(&b, op.genWrite(ctx, "obj"))
757761
fmt.Fprintf(&b, " return w.Flush()\n")
@@ -773,6 +777,7 @@ func (bctx *buildContext) generate(typ *types.Named, encoder, decoder bool) ([]b
773777
encSource []byte
774778
decSource []byte
775779
)
780+
ctx.internalMethods = bctx.internalMethods
776781
if encoder {
777782
encSource = generateEncoder(ctx, typ.Obj().Name(), op)
778783
}

rlp/rlpgen/gen.libevm.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2024 the libevm authors.
2+
//
3+
// The libevm additions to go-ethereum are free software: you can redistribute
4+
// them and/or modify them under the terms of the GNU Lesser General Public License
5+
// as published by the Free Software Foundation, either version 3 of the License,
6+
// or (at your option) any later version.
7+
//
8+
// The libevm additions are distributed in the hope that they will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
11+
// General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Lesser General Public License
14+
// along with the go-ethereum library. If not, see
15+
// <http://www.gnu.org/licenses/>.
16+
17+
package main
18+
19+
func (ctx *genContext) encoderMethod() string {
20+
if ctx.internalMethods {
21+
return "encodeRLP"
22+
}
23+
return "EncodeRLP"
24+
}
25+
26+
func (ctx *genContext) decoderMethod() string {
27+
if ctx.internalMethods {
28+
return "decodeRLP"
29+
}
30+
return "DecodeRLP"
31+
}

rlp/rlpgen/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func main() {
3636
genEncoder = flag.Bool("encoder", true, "generate EncodeRLP?")
3737
genDecoder = flag.Bool("decoder", false, "generate DecodeRLP?")
3838
typename = flag.String("type", "", "type to generate methods for")
39+
internal = flag.Bool("internal_methods", false, "generate internal (lower-case) method names")
3940
)
4041
flag.Parse()
4142

@@ -44,6 +45,7 @@ func main() {
4445
Type: *typename,
4546
GenerateEncoder: *genEncoder,
4647
GenerateDecoder: *genDecoder,
48+
InternalMethods: *internal,
4749
}
4850
code, err := cfg.process()
4951
if err != nil {
@@ -67,6 +69,8 @@ type Config struct {
6769

6870
GenerateEncoder bool
6971
GenerateDecoder bool
72+
73+
InternalMethods bool
7074
}
7175

7276
// process generates the Go code.
@@ -101,6 +105,7 @@ func (cfg *Config) process() (code []byte, err error) {
101105
}
102106
}
103107
bctx := newBuildContext(packageRLP)
108+
bctx.internalMethods = cfg.InternalMethods
104109

105110
// Find the type and generate.
106111
typ, err := lookupStructType(pkg.Scope(), cfg.Type)

0 commit comments

Comments
 (0)