Skip to content

Commit

Permalink
xdrgen: Prevent reordering of encoder and decoder functions
Browse files Browse the repository at this point in the history
I noticed that "xdrgen source" reorders the procedure encoder and
decoder functions every time it is run. I would prefer that the
generated code be more deterministic: it enables a reader to better
see exactly what has changed between runs of the tool.

The problem is that Python sets are not ordered. I use a Python set
to ensure that, when multiple procedures use a particular argument or
result type, the encoder/decoder for that type is emitted only once.

Sets aren't ordered, but I can use Python dictionaries for this
purpose to ensure the procedure functions are always emitted in the
same order if the .x file does not change.

Signed-off-by: Chuck Lever <[email protected]>
  • Loading branch information
chucklever committed Sep 20, 2024
1 parent fed8a17 commit 509abfc
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions tools/net/sunrpc/xdrgen/generators/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,20 @@ def emit_version_declarations(
environment: Environment, program: str, version: _RpcVersion
) -> None:
"""Emit declarations for each RPC version's procedures"""
arguments = set()
arguments = dict.fromkeys([])
for procedure in version.procedures:
if procedure.name not in excluded_apis:
arguments.add(procedure.argument.type_name)
arguments[procedure.argument.type_name] = None
if len(arguments) > 0:
print("")
template = environment.get_template("declaration/argument.j2")
for argument in arguments:
print(template.render(program=program, argument=argument))

results = set()
results = dict.fromkeys([])
for procedure in version.procedures:
if procedure.name not in excluded_apis:
results.add(procedure.result.type_name)
results[procedure.result.type_name] = None
if len(results) > 0:
print("")
template = environment.get_template("declaration/result.j2")
Expand All @@ -59,10 +59,10 @@ def emit_version_argument_decoders(
environment: Environment, program: str, version: _RpcVersion
) -> None:
"""Emit server argument decoders for each RPC version's procedures"""
arguments = set()
arguments = dict.fromkeys([])
for procedure in version.procedures:
if procedure.name not in excluded_apis:
arguments.add(procedure.argument.type_name)
arguments[procedure.argument.type_name] = None

template = environment.get_template("decoder/argument.j2")
for argument in arguments:
Expand All @@ -73,10 +73,10 @@ def emit_version_result_decoders(
environment: Environment, program: str, version: _RpcVersion
) -> None:
"""Emit client result decoders for each RPC version's procedures"""
results = set()
results = dict.fromkeys([])
for procedure in version.procedures:
if procedure.name not in excluded_apis:
results.add(procedure.result.type_name)
results[procedure.result.type_name] = None

template = environment.get_template("decoder/result.j2")
for result in results:
Expand All @@ -87,10 +87,10 @@ def emit_version_argument_encoders(
environment: Environment, program: str, version: _RpcVersion
) -> None:
"""Emit client argument encoders for each RPC version's procedures"""
arguments = set()
arguments = dict.fromkeys([])
for procedure in version.procedures:
if procedure.name not in excluded_apis:
arguments.add(procedure.argument.type_name)
arguments[procedure.argument.type_name] = None

template = environment.get_template("encoder/argument.j2")
for argument in arguments:
Expand All @@ -101,10 +101,10 @@ def emit_version_result_encoders(
environment: Environment, program: str, version: _RpcVersion
) -> None:
"""Emit server result encoders for each RPC version's procedures"""
results = set()
results = dict.fromkeys([])
for procedure in version.procedures:
if procedure.name not in excluded_apis:
results.add(procedure.result.type_name)
results[procedure.result.type_name] = None

template = environment.get_template("encoder/result.j2")
for result in results:
Expand Down

0 comments on commit 509abfc

Please sign in to comment.