Skip to content

Commit

Permalink
Reformat Structs and EventStructs template to algin with ktfmt check …
Browse files Browse the repository at this point in the history
…in CI (#30330)
  • Loading branch information
yufengwangca authored and pull[bot] committed Jan 18, 2024
1 parent 5aaee62 commit 1729796
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@

{%- macro encode_tlv(encodable, tag, name, depth) %}
{%- if encodable.is_nullable -%}
if ({{name}} != null) {
{{encode_tlv(encodable.without_nullable(), tag, name, depth + 1)}}
} else {
putNull({{tag}})
}
if ({{name}} != null) {
{{encode_tlv(encodable.without_nullable(), tag, name, depth + 1)}}
} else {
putNull({{tag}})
}
{%- elif encodable.is_optional -%}
if ({{name}}.isPresent) {
val opt{{name}} = {{name}}.get()
{{encode_tlv(encodable.without_optional(), tag, "opt" + name, depth + 1)}}
}
if ({{name}}.isPresent) {
val opt{{name}} = {{name}}.get()
{{encode_tlv(encodable.without_optional(), tag, "opt" + name, depth + 1)}}
}
{%- elif encodable.is_list -%}
startArray({{tag}})
for (item in {{name}}.iterator()) {
Expand All @@ -40,27 +40,27 @@

{%- macro decode_tlv(source, encodable, tag, depth) %}
{%- if encodable.is_nullable -%}
if (!tlvReader.isNull()) {
{{decode_tlv(source, encodable.without_nullable(), tag, depth + 1)}}
} else {
tlvReader.getNull({{tag}})
null
}
if (!tlvReader.isNull()) {
{{decode_tlv(source, encodable.without_nullable(), tag, depth + 1)}}
} else {
tlvReader.getNull({{tag}})
null
}
{%- elif encodable.is_optional -%}
if (tlvReader.isNextTag({{tag}})) {
Optional.of({{decode_tlv(source, encodable.without_optional(), tag, depth + 1)}})
} else {
Optional.empty()
}
if (tlvReader.isNextTag({{tag}})) {
Optional.of({{decode_tlv(source, encodable.without_optional(), tag, depth + 1)}})
} else {
Optional.empty()
}
{%- elif encodable.is_list -%}
{%- set encodablewithoutlist = encodable.without_list() -%}
buildList <{{encode_value(source, encodablewithoutlist, depth + 1)}}> {
tlvReader.enterArray({{tag}})
while(!tlvReader.isEndOfContainer()) {
this.add({{decode_tlv(source, encodablewithoutlist, "AnonymousTag", depth + 1)}})
{%- set encodablewithoutlist = encodable.without_list() -%}
buildList <{{encode_value(source, encodablewithoutlist, depth + 1)}}> {
tlvReader.enterArray({{tag}})
while(!tlvReader.isEndOfContainer()) {
this.add({{decode_tlv(source, encodablewithoutlist, "AnonymousTag", depth + 1)}})
}
tlvReader.exitContainer()
}
tlvReader.exitContainer()
}
{%- elif encodable.is_struct -%}
{%- set struct = encodable.get_underlying_struct() -%}
matter.devicecontroller.cluster.structs.{{source.name}}Cluster{{struct.name}}.fromTlv({{tag}}, tlvReader)
Expand Down Expand Up @@ -91,23 +91,22 @@
*/
package matter.devicecontroller.cluster.eventstructs

import java.util.Optional
import matter.devicecontroller.cluster.*
import matter.tlv.AnonymousTag
import matter.tlv.ContextSpecificTag
import matter.tlv.Tag
import matter.tlv.TlvParsingException
import matter.tlv.TlvReader
import matter.tlv.TlvWriter

import java.util.Optional

class {{cluster.name}}Cluster{{event.name}}Event (
{%- for field in event.fields -%}
{%- set encodable = field | asEncodable(typeLookup) %}
val {{field.name}}: {{encode_value(cluster, encodable, 0)}}
{%- if loop.index0 < loop.length - 1 -%}{{","}}{%- endif -%}
{%- endfor -%}) {
override fun toString(): String = buildString {
class {{cluster.name}}Cluster{{event.name}}Event(
{%- for field in event.fields -%}
{%- set encodable = field | asEncodable(typeLookup) %}
val {{field.name}}: {{encode_value(cluster, encodable, 0)}}
{%- if loop.index0 < loop.length - 1 -%}{{","}}{%- endif -%}
{%- endfor %}
) {
override fun toString(): String = buildString {
append("{{cluster.name}}Cluster{{event.name}}Event {\n")
{%- for field in event.fields %}
append("\t{{field.name}} : ${{field.name}}\n")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@

{%- macro encode_tlv(encodable, tag, name, depth) %}
{%- if encodable.is_nullable -%}
if ({{name}} != null) {
{{encode_tlv(encodable.without_nullable(), tag, name, depth + 1)}}
} else {
putNull({{tag}})
}
if ({{name}} != null) {
{{encode_tlv(encodable.without_nullable(), tag, name, depth + 1)}}
} else {
putNull({{tag}})
}
{%- elif encodable.is_optional -%}
if ({{name}}.isPresent) {
val opt{{name}} = {{name}}.get()
{{encode_tlv(encodable.without_optional(), tag, "opt" + name, depth + 1)}}
}
if ({{name}}.isPresent) {
val opt{{name}} = {{name}}.get()
{{encode_tlv(encodable.without_optional(), tag, "opt" + name, depth + 1)}}
}
{%- elif encodable.is_list -%}
startArray({{tag}})
for (item in {{name}}.iterator()) {
Expand Down Expand Up @@ -91,23 +91,22 @@
*/
package matter.devicecontroller.cluster.structs

import java.util.Optional
import matter.devicecontroller.cluster.*
import matter.tlv.AnonymousTag
import matter.tlv.ContextSpecificTag
import matter.tlv.Tag
import matter.tlv.TlvParsingException
import matter.tlv.TlvReader
import matter.tlv.TlvWriter

import java.util.Optional

class {{cluster.name}}Cluster{{struct.name}} (
{%- for field in struct.fields %}
{%- set encodable = field | asEncodable(typeLookup) %}
val {{field.name}}: {{encode_value(cluster, encodable, 0)}}
{%- if loop.index0 < loop.length - 1 -%}{{","}}{%- endif -%}
{%- endfor -%}) {
override fun toString(): String = buildString {
class {{cluster.name}}Cluster{{struct.name}}(
{%- for field in struct.fields %}
{%- set encodable = field | asEncodable(typeLookup) %}
val {{field.name}}: {{encode_value(cluster, encodable, 0)}}
{%- if loop.index0 < loop.length - 1 -%}{{","}}{%- endif -%}
{%- endfor %}
) {
override fun toString(): String = buildString {
append("{{cluster.name}}Cluster{{struct.name}} {\n")
{%- for field in struct.fields %}
append("\t{{field.name}} : ${{field.name}}\n")
Expand All @@ -132,7 +131,7 @@ class {{cluster.name}}Cluster{{struct.name}} (
private const val TAG_{{field.name | constcase}} = {{field.code}}
{%- endfor %}

fun fromTlv(tlvTag: Tag, tlvReader: TlvReader) : {{cluster.name}}Cluster{{struct.name}} {
fun fromTlv(tlvTag: Tag, tlvReader: TlvReader): {{cluster.name}}Cluster{{struct.name}} {
tlvReader.enterStructure(tlvTag)
{% for field in struct.fields %}
{%- set decodable = field | asEncodable(typeLookup) %}
Expand Down

0 comments on commit 1729796

Please sign in to comment.