Skip to content

Commit

Permalink
Tidy up of python autogenerated messages (PhotonVision#1594)
Browse files Browse the repository at this point in the history
This fixes mostly formatting issues so there is no longer any diff with
things after rerunning the generation script. Yes, most of this can be
fixed by running wpiformat but that doesn't exist as a pre-commit hook
atm, so I think this is nicer.

It also removes any reference to the java encode/decode within the
python message gen
  • Loading branch information
LucienMorey authored Nov 21, 2024
1 parent fa66ed8 commit 61552ad
Show file tree
Hide file tree
Showing 33 changed files with 171 additions and 185 deletions.
2 changes: 2 additions & 0 deletions .styleguide
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ modifiableFileExclude {
\.ico$
\.rknn$
gradlew
photon-lib/py/photonlibpy/generated/
photon-targeting/src/generated/
}

includeProject {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ def pack(value: "PhotonPipelineResult") -> "Packet":
ret = Packet()

# metadata is of non-intrinsic type PhotonPipelineMetadata
ret.encodeBytes(
PhotonPipelineMetadata.photonStruct.pack(value.metadata).getData()
)
ret.encodeBytes(PhotonPipelineMetadata.photonStruct.pack(value.metadata).getData())

# targets is a custom VLA!
ret.encodeList(value.targets, PhotonTrackedTarget.photonStruct)
Expand Down
12 changes: 12 additions & 0 deletions photon-serde/message_data_types.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,46 @@ bool:
cpp_type: bool
java_decode_method: decodeBoolean
java_encode_shim: encodeBoolean
python_decode_shim: decodeBoolean
python_encode_shim: encodeBoolean
int16:
len: 2
java_type: short
cpp_type: int16_t
java_decode_method: decodeShort
java_list_decode_method: decodeShortList
java_encode_shim: encodeShort
python_decode_shim: decodeShort
python_encode_shim: encodeShort
int32:
len: 4
java_type: int
cpp_type: int32_t
java_decode_method: decodeInt
java_encode_shim: encodeInt
python_decode_shim: decodeInt
python_encode_shim: encodeInt
int64:
len: 8
java_type: long
cpp_type: int64_t
java_decode_method: decodeLong
java_encode_shim: encodeLong
python_decode_shim: decodeLong
python_encode_shim: encodeLong
float32:
len: 4
java_type: float
cpp_type: float
java_decode_method: decodeFloat
java_encode_shim: encodeFloat
python_decode_shim: decodeFloat
python_encode_shim: encodeFloat
float64:
len: 8
java_type: double
cpp_type: double
java_decode_method: decodeDouble
java_encode_shim: encodeDouble
python_decode_shim: decodeDouble
python_encode_shim: encodeDouble
2 changes: 1 addition & 1 deletion photon-serde/messages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
java_encode_shim: PacketUtils.packTransform3d
cpp_type: frc::Transform3d
cpp_include: "<frc/geometry/Transform3d.h>"
python_decode_shim: packet.decodeTransform
python_decode_shim: decodeTransform
python_encode_shim: encodeTransform
java_import: edu.wpi.first.math.geometry.Transform3d
# shim since we expect fields to at least exist
Expand Down
38 changes: 19 additions & 19 deletions photon-serde/templates/Message.java.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -54,53 +54,53 @@ public class {{ name }}Serde implements PacketSerde<{{name}}> {
@Override
public void pack(Packet packet, {{ name }} value) {
{%- for field in fields -%}
{%- if field.type | is_shimmed %}
{%- if field.type | is_shimmed %}
{{ get_message_by_name(field.type).java_encode_shim }}(packet, value.{{ field.name }});
{%- elif field.optional == True %}
{%- elif field.optional == True %}
// {{ field.name }} is optional! it better not be a VLA too
packet.encodeOptional(value.{{ field.name }});
{%- elif field.vla == True and field.type | is_intrinsic %}
{%- elif field.vla == True and field.type | is_intrinsic %}
// {{ field.name }} is a intrinsic VLA!
packet.encode(value.{{ field.name }});
{%- elif field.vla == True %}
{%- elif field.vla == True %}
// {{ field.name }} is a custom VLA!
packet.encodeList(value.{{ field.name }});
{%- elif field.type | is_intrinsic %}
{%- elif field.type | is_intrinsic %}
// field {{ field.name }} is of intrinsic type {{ field.type }}
packet.encode(({{ type_map[field.type].java_type }}) value.{{ field.name }});
{%- else %}
{%- else %}
// field {{ field.name }} is of non-intrinsic type {{ field.type }}
{{ field.type }}.photonStruct.pack(packet, value.{{ field.name }});
{%- endif %}
{%- if not loop.last %}
{% endif -%}
{%- endif %}
{%- if not loop.last %}
{% endif -%}
{% endfor%}
}

@Override
public {{ name }} unpack(Packet packet) {
var ret = new {{ name }}();
{% for field in fields -%}
{%- if field.type | is_shimmed %}
{%- if field.type | is_shimmed %}
ret.{{ field.name }} = {{ get_message_by_name(field.type).java_decode_shim }}(packet);
{%- elif field.optional == True %}
{%- elif field.optional == True %}
// {{ field.name }} is optional! it better not be a VLA too
ret.{{ field.name }} = packet.decodeOptional({{ field.type }}.photonStruct);
{%- elif field.vla == True and not field.type | is_intrinsic %}
{%- elif field.vla == True and not field.type | is_intrinsic %}
// {{ field.name }} is a custom VLA!
ret.{{ field.name }} = packet.decodeList({{ field.type }}.photonStruct);
{%- elif field.vla == True and field.type | is_intrinsic %}
{%- elif field.vla == True and field.type | is_intrinsic %}
// {{ field.name }} is a custom VLA!
ret.{{ field.name }} = packet.decode{{ type_map[field.type].java_type.title() }}List();
{%- elif field.type | is_intrinsic %}
{%- elif field.type | is_intrinsic %}
// {{ field.name }} is of intrinsic type {{ field.type }}
ret.{{field.name}} = packet.{{ type_map[field.type].java_decode_method }}();
{%- else %}
{%- else %}
// {{ field.name }} is of non-intrinsic type {{ field.type }}
ret.{{field.name}} = {{ field.type }}.photonStruct.unpack(packet);
{%- endif %}
{%- if not loop.last %}
{% endif -%}
{%- endif %}
{%- if not loop.last %}
{% endif -%}
{% endfor%}

return ret;
Expand All @@ -125,4 +125,4 @@ public class {{ name }}Serde implements PacketSerde<{{name}}> {
{%- endfor%}
};
}
}
}{{'\n'}}
24 changes: 12 additions & 12 deletions photon-serde/templates/ThingSerde.cpp.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ namespace photon {
using StructType = SerdeType<{{ name }}>;

void StructType::Pack(Packet& packet, const {{ name }}& value) {
{% for field in fields -%}
packet.Pack<{{ field | get_qualified_name }}>(value.{{ field.name }});
{%- if not loop.last %}
{% endif -%}
{% endfor %}
}

{{ name }} StructType::Unpack(Packet& packet) {
return {{ name }}{ {{ name }}_PhotonStruct{
{% for field in fields -%}
packet.Pack<{{ field | get_qualified_name }}>(value.{{ field.name }});
.{{ field.name}} = packet.Unpack<{{ field | get_qualified_name }}>(),
{%- if not loop.last %}
{% endif -%}
{% endfor %}
}};
}

{{ name }} StructType::Unpack(Packet& packet) {
return {{ name }}{ {{ name }}_PhotonStruct{
{% for field in fields -%}
.{{ field.name}} = packet.Unpack<{{ field | get_qualified_name }}>(),
{%- if not loop.last %}
{% endif -%}
{% endfor %}
}};
}

} // namespace photon
} // namespace photon{{'\n'}}
2 changes: 1 addition & 1 deletion photon-serde/templates/ThingSerde.h.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ struct WPILIB_DLLEXPORT SerdeType<{{ name }}> {

static_assert(photon::PhotonStructSerializable<photon::{{ name }}>);

} // namespace photon
} // namespace photon{{'\n'}}
13 changes: 6 additions & 7 deletions photon-serde/templates/ThingSerde.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class {{ name }}Serde:
MESSAGE_FORMAT = "{{ message_fmt }}"

@staticmethod
def pack(value: '{{ name }}' ) -> 'Packet':
def pack(value: "{{ name }}") -> "Packet":
ret = Packet()
{% for field in fields -%}
{%- if field.type | is_shimmed %}
Expand All @@ -60,7 +60,7 @@ class {{ name }}Serde:
ret.encode{{ type_map[field.type].java_type.title() }}List(value.{{ field.name }})
{%- elif field.type | is_intrinsic %}
# {{ field.name }} is of intrinsic type {{ field.type }}
ret.{{ type_map[field.type].java_encode_shim }}(value.{{field.name}})
ret.{{ type_map[field.type].python_encode_shim }}(value.{{field.name}})
{%- else %}
# {{ field.name }} is of non-intrinsic type {{ field.type }}
ret.encodeBytes({{ field.type }}.photonStruct.pack(value.{{field.name}}).getData())
Expand All @@ -70,13 +70,12 @@ class {{ name }}Serde:
{% endfor%}
return ret


@staticmethod
def unpack(packet: 'Packet') -> '{{ name }}':
def unpack(packet: "Packet") -> "{{ name }}":
ret = {{ name }}()
{% for field in fields -%}
{%- if field.type | is_shimmed %}
ret.{{ field.name }} = {{ get_message_by_name(field.type).python_decode_shim }}()
ret.{{ field.name }} = packet.{{ get_message_by_name(field.type).python_decode_shim }}()
{%- elif field.optional == True %}
# {{ field.name }} is optional! it better not be a VLA too
ret.{{ field.name }} = packet.decodeOptional({{ field.type }}.photonStruct)
Expand All @@ -88,7 +87,7 @@ class {{ name }}Serde:
ret.{{ field.name }} = packet.decode{{ type_map[field.type].java_type.title() }}List()
{%- elif field.type | is_intrinsic %}
# {{ field.name }} is of intrinsic type {{ field.type }}
ret.{{field.name}} = packet.{{ type_map[field.type].java_decode_method }}()
ret.{{field.name}} = packet.{{ type_map[field.type].python_decode_shim }}()
{%- else %}
# {{ field.name }} is of non-intrinsic type {{ field.type }}
ret.{{field.name}} = {{ field.type }}.photonStruct.unpack(packet)
Expand All @@ -101,4 +100,4 @@ class {{ name }}Serde:


# Hack ourselves into the base class
{{ name }}.photonStruct = {{ name }}Serde()
{{ name }}.photonStruct = {{ name }}Serde(){{'\n'}}
2 changes: 1 addition & 1 deletion photon-serde/templates/ThingStruct.h.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ struct {{ name }}_PhotonStruct {
friend bool operator==({{ name }}_PhotonStruct const&, {{ name }}_PhotonStruct const&) = default;
};

} // namespace photon
} // namespace photon{{'\n'}}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* Auto-generated serialization/deserialization helper for MultiTargetPNPResult
*/
public class MultiTargetPNPResultSerde implements PacketSerde<MultiTargetPNPResult> {

@Override
public final String getInterfaceUUID() { return "541096947e9f3ca2d3f425ff7b04aa7b"; }
@Override
Expand Down Expand Up @@ -79,6 +80,7 @@ public PacketSerde<?>[] getNestedPhotonMessages() {
@Override
public Struct<?>[] getNestedWpilibMessages() {
return new Struct<?>[] {

};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* Auto-generated serialization/deserialization helper for PhotonPipelineMetadata
*/
public class PhotonPipelineMetadataSerde implements PacketSerde<PhotonPipelineMetadata> {

@Override
public final String getInterfaceUUID() { return "ac0a45f686457856fb30af77699ea356"; }
@Override
Expand Down Expand Up @@ -84,12 +85,14 @@ public PhotonPipelineMetadata unpack(Packet packet) {
@Override
public PacketSerde<?>[] getNestedPhotonMessages() {
return new PacketSerde<?>[] {

};
}

@Override
public Struct<?>[] getNestedWpilibMessages() {
return new Struct<?>[] {

};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* Auto-generated serialization/deserialization helper for PhotonPipelineResult
*/
public class PhotonPipelineResultSerde implements PacketSerde<PhotonPipelineResult> {

@Override
public final String getInterfaceUUID() { return "4b2ff16a964b5e2bf04be0c1454d91c4"; }
@Override
Expand Down Expand Up @@ -78,13 +79,14 @@ public PhotonPipelineResult unpack(Packet packet) {
@Override
public PacketSerde<?>[] getNestedPhotonMessages() {
return new PacketSerde<?>[] {
MultiTargetPNPResult.photonStruct,PhotonPipelineMetadata.photonStruct,PhotonTrackedTarget.photonStruct
PhotonPipelineMetadata.photonStruct,MultiTargetPNPResult.photonStruct,PhotonTrackedTarget.photonStruct
};
}

@Override
public Struct<?>[] getNestedWpilibMessages() {
return new Struct<?>[] {

};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* Auto-generated serialization/deserialization helper for PhotonTrackedTarget
*/
public class PhotonTrackedTargetSerde implements PacketSerde<PhotonTrackedTarget> {

@Override
public final String getInterfaceUUID() { return "cc6dbb5c5c1e0fa808108019b20863f1"; }
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* Auto-generated serialization/deserialization helper for PnpResult
*/
public class PnpResultSerde implements PacketSerde<PnpResult> {

@Override
public final String getInterfaceUUID() { return "ae4d655c0a3104d88df4f5db144c1e86"; }
@Override
Expand Down Expand Up @@ -86,6 +87,7 @@ public PnpResult unpack(Packet packet) {
@Override
public PacketSerde<?>[] getNestedPhotonMessages() {
return new PacketSerde<?>[] {

};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* Auto-generated serialization/deserialization helper for TargetCorner
*/
public class TargetCornerSerde implements PacketSerde<TargetCorner> {

@Override
public final String getInterfaceUUID() { return "16f6ac0dedc8eaccb951f4895d9e18b6"; }
@Override
Expand Down Expand Up @@ -72,12 +73,14 @@ public TargetCorner unpack(Packet packet) {
@Override
public PacketSerde<?>[] getNestedPhotonMessages() {
return new PacketSerde<?>[] {

};
}

@Override
public Struct<?>[] getNestedWpilibMessages() {
return new Struct<?>[] {

};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

// THIS std::FILE WAS AUTO-GENERATED BY ./photon-serde/generate_messages.py. DO
// NOT MODIFY
// THIS FILE WAS AUTO-GENERATED BY ./photon-serde/generate_messages.py. DO NOT MODIFY

#include "photon/serde/MultiTargetPNPResultSerde.h"

Expand All @@ -30,10 +29,10 @@ void StructType::Pack(Packet& packet, const MultiTargetPNPResult& value) {
}

MultiTargetPNPResult StructType::Unpack(Packet& packet) {
return MultiTargetPNPResult{MultiTargetPNPResult_PhotonStruct{
.estimatedPose = packet.Unpack<photon::PnpResult>(),
.fiducialIDsUsed = packet.Unpack<std::vector<int16_t>>(),
return MultiTargetPNPResult{ MultiTargetPNPResult_PhotonStruct{
.estimatedPose = packet.Unpack<photon::PnpResult>(),
.fiducialIDsUsed = packet.Unpack<std::vector<int16_t>>(),
}};
}

} // namespace photon
} // namespace photon
Loading

0 comments on commit 61552ad

Please sign in to comment.