diff --git a/packages/protons/src/fields/array-field.ts b/packages/protons/src/fields/array-field.ts index 1a224d8..333caac 100644 --- a/packages/protons/src/fields/array-field.ts +++ b/packages/protons/src/fields/array-field.ts @@ -42,7 +42,7 @@ export class ArrayField extends Field { } return ` - if (obj.${this.name} != null) { + if (obj.${this.name} != null && obj.${this.name}.length > 0) { for (const value of obj.${this.name}) { w.uint32(${id}) ${type.getEncoder(this, 'value')} diff --git a/packages/protons/src/fields/map-field.ts b/packages/protons/src/fields/map-field.ts index 579189b..4c00aa8 100644 --- a/packages/protons/src/fields/map-field.ts +++ b/packages/protons/src/fields/map-field.ts @@ -56,7 +56,7 @@ export class MapField extends Field { const entryType = parent.findType(this.entryType) return ` - if (${entryType.getValueTest(this, 'value')}) { + if (obj.${this.name} != null && obj.${this.name}.size > 0) { for (const [key, value] of obj.${this.name}.entries()) { w.uint32(${id}) ${entryType.getEncoder(this, '{ key, value }')} diff --git a/packages/protons/test/fixtures/bitswap.ts b/packages/protons/test/fixtures/bitswap.ts index 5990f78..ea91b90 100644 --- a/packages/protons/test/fixtures/bitswap.ts +++ b/packages/protons/test/fixtures/bitswap.ts @@ -223,7 +223,7 @@ export namespace Message { w.fork() } - if (obj.entries != null) { + if (obj.entries != null && obj.entries.length > 0) { for (const value of obj.entries) { w.uint32(10) Message.Wantlist.Entry.codec().encode(value, w) @@ -622,21 +622,21 @@ export namespace Message { Message.Wantlist.codec().encode(obj.wantlist, w) } - if (obj.blocks != null) { + if (obj.blocks != null && obj.blocks.length > 0) { for (const value of obj.blocks) { w.uint32(18) w.bytes(value) } } - if (obj.payload != null) { + if (obj.payload != null && obj.payload.length > 0) { for (const value of obj.payload) { w.uint32(26) Message.Block.codec().encode(value, w) } } - if (obj.blockPresences != null) { + if (obj.blockPresences != null && obj.blockPresences.length > 0) { for (const value of obj.blockPresences) { w.uint32(34) Message.BlockPresence.codec().encode(value, w) diff --git a/packages/protons/test/fixtures/circuit.ts b/packages/protons/test/fixtures/circuit.ts index ba017a3..b0d4a70 100644 --- a/packages/protons/test/fixtures/circuit.ts +++ b/packages/protons/test/fixtures/circuit.ts @@ -95,7 +95,7 @@ export namespace CircuitRelay { w.bytes(obj.id) } - if (obj.addrs != null) { + if (obj.addrs != null && obj.addrs.length > 0) { for (const value of obj.addrs) { w.uint32(18) w.bytes(value) diff --git a/packages/protons/test/fixtures/custom-option-jstype.ts b/packages/protons/test/fixtures/custom-option-jstype.ts index d0aebf4..9cf1988 100644 --- a/packages/protons/test/fixtures/custom-option-jstype.ts +++ b/packages/protons/test/fixtures/custom-option-jstype.ts @@ -165,14 +165,14 @@ export namespace CustomOptionNumber { w.sfixed64Number(obj.sf64) } - if (obj.i64Array != null) { + if (obj.i64Array != null && obj.i64Array.length > 0) { for (const value of obj.i64Array) { w.uint32(56) w.int64Number(value) } } - if (obj.i64Map != null) { + if (obj.i64Map != null && obj.i64Map.size > 0) { for (const [key, value] of obj.i64Map.entries()) { w.uint32(66) CustomOptionNumber.CustomOptionNumber$i64MapEntry.codec().encode({ key, value }, w) @@ -567,14 +567,14 @@ export namespace CustomOptionString { w.sfixed64String(obj.sf64) } - if (obj.i64Array != null) { + if (obj.i64Array != null && obj.i64Array.length > 0) { for (const value of obj.i64Array) { w.uint32(56) w.int64String(value) } } - if (obj.i64Map != null) { + if (obj.i64Map != null && obj.i64Map.size > 0) { for (const [key, value] of obj.i64Map.entries()) { w.uint32(66) CustomOptionString.CustomOptionString$i64MapEntry.codec().encode({ key, value }, w) diff --git a/packages/protons/test/fixtures/daemon.ts b/packages/protons/test/fixtures/daemon.ts index 7cc0c30..7d882cb 100644 --- a/packages/protons/test/fixtures/daemon.ts +++ b/packages/protons/test/fixtures/daemon.ts @@ -472,7 +472,7 @@ export namespace Response { DHTResponse.codec().encode(obj.dht, w) } - if (obj.peers != null) { + if (obj.peers != null && obj.peers.length > 0) { for (const value of obj.peers) { w.uint32(50) PeerInfo.codec().encode(value, w) @@ -783,7 +783,7 @@ export namespace IdentifyResponse { w.bytes(obj.id) } - if (obj.addrs != null) { + if (obj.addrs != null && obj.addrs.length > 0) { for (const value of obj.addrs) { w.uint32(18) w.bytes(value) @@ -915,7 +915,7 @@ export namespace ConnectRequest { w.bytes(obj.peer) } - if (obj.addrs != null) { + if (obj.addrs != null && obj.addrs.length > 0) { for (const value of obj.addrs) { w.uint32(18) w.bytes(value) @@ -1068,7 +1068,7 @@ export namespace StreamOpenRequest { w.bytes(obj.peer) } - if (obj.proto != null) { + if (obj.proto != null && obj.proto.length > 0) { for (const value of obj.proto) { w.uint32(18) w.string(value) @@ -1220,7 +1220,7 @@ export namespace StreamHandlerRequest { w.bytes(obj.addr) } - if (obj.proto != null) { + if (obj.proto != null && obj.proto.length > 0) { for (const value of obj.proto) { w.uint32(18) w.string(value) @@ -1985,7 +1985,7 @@ export namespace PeerInfo { w.bytes(obj.id) } - if (obj.addrs != null) { + if (obj.addrs != null && obj.addrs.length > 0) { for (const value of obj.addrs) { w.uint32(18) w.bytes(value) @@ -2545,7 +2545,7 @@ export namespace PSMessage { w.bytes(obj.seqno) } - if (obj.topicIDs != null) { + if (obj.topicIDs != null && obj.topicIDs.length > 0) { for (const value of obj.topicIDs) { w.uint32(34) w.string(value) @@ -2744,14 +2744,14 @@ export namespace PSResponse { w.fork() } - if (obj.topics != null) { + if (obj.topics != null && obj.topics.length > 0) { for (const value of obj.topics) { w.uint32(10) w.string(value) } } - if (obj.peerIDs != null) { + if (obj.peerIDs != null && obj.peerIDs.length > 0) { for (const value of obj.peerIDs) { w.uint32(18) w.bytes(value) @@ -2920,7 +2920,7 @@ export namespace PeerstoreRequest { w.bytes(obj.id) } - if (obj.protos != null) { + if (obj.protos != null && obj.protos.length > 0) { for (const value of obj.protos) { w.uint32(26) w.string(value) @@ -3067,7 +3067,7 @@ export namespace PeerstoreResponse { PeerInfo.codec().encode(obj.peer, w) } - if (obj.protos != null) { + if (obj.protos != null && obj.protos.length > 0) { for (const value of obj.protos) { w.uint32(18) w.string(value) diff --git a/packages/protons/test/fixtures/dht.ts b/packages/protons/test/fixtures/dht.ts index 6d34dd2..09e261c 100644 --- a/packages/protons/test/fixtures/dht.ts +++ b/packages/protons/test/fixtures/dht.ts @@ -252,7 +252,7 @@ export namespace Message { w.bytes(obj.id) } - if (obj.addrs != null) { + if (obj.addrs != null && obj.addrs.length > 0) { for (const value of obj.addrs) { w.uint32(18) w.bytes(value) @@ -412,14 +412,14 @@ export namespace Message { w.bytes(obj.record) } - if (obj.closerPeers != null) { + if (obj.closerPeers != null && obj.closerPeers.length > 0) { for (const value of obj.closerPeers) { w.uint32(66) Message.Peer.codec().encode(value, w) } } - if (obj.providerPeers != null) { + if (obj.providerPeers != null && obj.providerPeers.length > 0) { for (const value of obj.providerPeers) { w.uint32(74) Message.Peer.codec().encode(value, w) diff --git a/packages/protons/test/fixtures/maps.ts b/packages/protons/test/fixtures/maps.ts index 195d352..413a764 100644 --- a/packages/protons/test/fixtures/maps.ts +++ b/packages/protons/test/fixtures/maps.ts @@ -40,7 +40,7 @@ export namespace SubMessage { w.string(obj.foo) } - if (obj.bar != null) { + if (obj.bar != null && obj.bar.length > 0) { for (const value of obj.bar) { w.uint32(16) w.uint32(value) @@ -736,35 +736,35 @@ export namespace MapTypes { w.fork() } - if (obj.stringMap != null) { + if (obj.stringMap != null && obj.stringMap.size > 0) { for (const [key, value] of obj.stringMap.entries()) { w.uint32(10) MapTypes.MapTypes$stringMapEntry.codec().encode({ key, value }, w) } } - if (obj.intMap != null) { + if (obj.intMap != null && obj.intMap.size > 0) { for (const [key, value] of obj.intMap.entries()) { w.uint32(18) MapTypes.MapTypes$intMapEntry.codec().encode({ key, value }, w) } } - if (obj.boolMap != null) { + if (obj.boolMap != null && obj.boolMap.size > 0) { for (const [key, value] of obj.boolMap.entries()) { w.uint32(26) MapTypes.MapTypes$boolMapEntry.codec().encode({ key, value }, w) } } - if (obj.messageMap != null) { + if (obj.messageMap != null && obj.messageMap.size > 0) { for (const [key, value] of obj.messageMap.entries()) { w.uint32(34) MapTypes.MapTypes$messageMapEntry.codec().encode({ key, value }, w) } } - if (obj.enumMap != null) { + if (obj.enumMap != null && obj.enumMap.size > 0) { for (const [key, value] of obj.enumMap.entries()) { w.uint32(42) MapTypes.MapTypes$enumMapEntry.codec().encode({ key, value }, w) diff --git a/packages/protons/test/fixtures/peer.ts b/packages/protons/test/fixtures/peer.ts index 6aad7b5..aa01497 100644 --- a/packages/protons/test/fixtures/peer.ts +++ b/packages/protons/test/fixtures/peer.ts @@ -21,21 +21,21 @@ export namespace Peer { w.fork() } - if (obj.addresses != null) { + if (obj.addresses != null && obj.addresses.length > 0) { for (const value of obj.addresses) { w.uint32(10) Address.codec().encode(value, w) } } - if (obj.protocols != null) { + if (obj.protocols != null && obj.protocols.length > 0) { for (const value of obj.protocols) { w.uint32(18) w.string(value) } } - if (obj.metadata != null) { + if (obj.metadata != null && obj.metadata.length > 0) { for (const value of obj.metadata) { w.uint32(26) Metadata.codec().encode(value, w) diff --git a/packages/protons/test/fixtures/protons-options.ts b/packages/protons/test/fixtures/protons-options.ts index fdc1b5a..7a5e028 100644 --- a/packages/protons/test/fixtures/protons-options.ts +++ b/packages/protons/test/fixtures/protons-options.ts @@ -16,7 +16,7 @@ export namespace MessageWithSizeLimitedRepeatedField { w.fork() } - if (obj.repeatedField != null) { + if (obj.repeatedField != null && obj.repeatedField.length > 0) { for (const value of obj.repeatedField) { w.uint32(10) w.string(value) @@ -244,7 +244,7 @@ export namespace MessageWithSizeLimitedMap { w.fork() } - if (obj.mapField != null) { + if (obj.mapField != null && obj.mapField.size > 0) { for (const [key, value] of obj.mapField.entries()) { w.uint32(10) MessageWithSizeLimitedMap.MessageWithSizeLimitedMap$mapFieldEntry.codec().encode({ key, value }, w) diff --git a/packages/protons/test/fixtures/repeated.ts b/packages/protons/test/fixtures/repeated.ts index 428dc82..36a3d0a 100644 --- a/packages/protons/test/fixtures/repeated.ts +++ b/packages/protons/test/fixtures/repeated.ts @@ -17,7 +17,7 @@ export namespace SubSubMessage { w.fork() } - if (obj.foo != null) { + if (obj.foo != null && obj.foo.length > 0) { for (const value of obj.foo) { w.uint32(10) w.string(value) @@ -149,7 +149,7 @@ export namespace SubMessage { w.fork() } - if (obj.foo != null) { + if (obj.foo != null && obj.foo.length > 0) { for (const value of obj.foo) { w.uint32(10) w.string(value) @@ -166,7 +166,7 @@ export namespace SubMessage { SubSubMessage.codec().encode(obj.message, w) } - if (obj.messages != null) { + if (obj.messages != null && obj.messages.length > 0) { for (const value of obj.messages) { w.uint32(34) SubSubMessage.codec().encode(value, w) @@ -360,21 +360,21 @@ export namespace RepeatedTypes { w.fork() } - if (obj.number != null) { + if (obj.number != null && obj.number.length > 0) { for (const value of obj.number) { w.uint32(8) w.uint32(value) } } - if (obj.limitedNumber != null) { + if (obj.limitedNumber != null && obj.limitedNumber.length > 0) { for (const value of obj.limitedNumber) { w.uint32(16) w.uint32(value) } } - if (obj.messages != null) { + if (obj.messages != null && obj.messages.length > 0) { for (const value of obj.messages) { w.uint32(26) SubMessage.codec().encode(value, w) diff --git a/packages/protons/test/fixtures/streaming.ts b/packages/protons/test/fixtures/streaming.ts index db2acd9..2eb285f 100644 --- a/packages/protons/test/fixtures/streaming.ts +++ b/packages/protons/test/fixtures/streaming.ts @@ -28,7 +28,7 @@ export namespace MessageWithArrayField { w.uint32(obj.field2) } - if (obj.arr != null) { + if (obj.arr != null && obj.arr.length > 0) { for (const value of obj.arr) { w.uint32(26) w.string(value) @@ -494,7 +494,7 @@ export namespace MessageWithRepeatedMessage { w.bool(obj.field1) } - if (obj.nestedMessages != null) { + if (obj.nestedMessages != null && obj.nestedMessages.length > 0) { for (const value of obj.nestedMessages) { w.uint32(18) NestedMessage.codec().encode(value, w) @@ -743,7 +743,7 @@ export namespace MessageWithMapMessage { w.bool(obj.field1) } - if (obj.nestedMessages != null) { + if (obj.nestedMessages != null && obj.nestedMessages.size > 0) { for (const [key, value] of obj.nestedMessages.entries()) { w.uint32(18) MessageWithMapMessage.MessageWithMapMessage$nestedMessagesEntry.codec().encode({ key, value }, w) @@ -991,7 +991,7 @@ export namespace MessageWithPrimitiveMap { w.bool(obj.field1) } - if (obj.nestedStrings != null) { + if (obj.nestedStrings != null && obj.nestedStrings.size > 0) { for (const [key, value] of obj.nestedStrings.entries()) { w.uint32(18) MessageWithPrimitiveMap.MessageWithPrimitiveMap$nestedStringsEntry.codec().encode({ key, value }, w) diff --git a/packages/protons/test/fixtures/test.ts b/packages/protons/test/fixtures/test.ts index cc8f7c4..320b2cf 100644 --- a/packages/protons/test/fixtures/test.ts +++ b/packages/protons/test/fixtures/test.ts @@ -203,7 +203,7 @@ export namespace AllTheTypes { SubMessage.codec().encode(obj.field13, w) } - if (obj.field14 != null) { + if (obj.field14 != null && obj.field14.length > 0) { for (const value of obj.field14) { w.uint32(114) w.string(value)