Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement initial Kotlin generator #30009

Merged
merged 1 commit into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions kotlin-detect-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ style:
- "**/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/provisioning/EnterNetworkFragment.kt"
- "**/examples/java-matter-controller/java/src/com/matter/controller/commands/common/MatterCommand.kt"
- "**/src/controller/java/src/matter/onboardingpayload/Base38.kt"
- "**/src/controller/java/generated/java/**/*"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we have kotlin as prefix instead of java?

ForbiddenComment:
excludes:
- "**/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/MultiAdminClientFragment.kt"
Expand Down Expand Up @@ -246,6 +247,9 @@ naming:
excludes:
- "**/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/clusterinteraction/HistoryCommandAdapter.kt"
- "**/src/controller/java/generated/java/**/*"
FunctionParameterNaming:
excludes:
- "**/src/controller/java/generated/java/**/*"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some doc comments about why this is required for generated code would be nice.

Here is where a separate kotlin directory would be nice too, so we do not inherit leniency from other places - we should try to make even the generated kotlin code look "nice" in as much as possible.

TopLevelPropertyNaming:
excludes:
- "**/src/controller/java/generated/java/**/*"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
{%- macro encode_value(source, encodable, depth) -%}
yufengwangca marked this conversation as resolved.
Show resolved Hide resolved
{%- if encodable.is_nullable and encodable.is_optional -%}
{{encode_value(source, encodable.without_nullable().without_optional(), depth + 1)}}?
{%- elif encodable.is_nullable -%}
{{encode_value(source, encodable.without_nullable(), depth + 1)}}?
{%- elif encodable.is_optional -%}
{{encode_value(source, encodable.without_optional(), depth + 1)}}?
{%- elif encodable.is_list -%}
ArrayList<{{encode_value(source, encodable.without_list(), depth + 1)}}>
{%- elif encodable.is_struct -%}
{%- set struct = encodable.get_underlying_struct() -%}
ChipStructs.{{source.name}}Cluster{{struct.name}}
{%- else -%}
{{encodable.boxed_java_type}}
{%- endif -%}
{%- endmacro -%}

{%- macro encode_value_without_optional(source, encodable, depth) -%}
{%- if encodable.is_nullable -%}
{{encode_value_without_optional(source, encodable.without_nullable(), depth + 1)}}?
{%- elif encodable.is_list -%}
List<{{encode_value_without_optional(source, encodable.without_list(), depth + 1)}}>
{%- elif encodable.is_struct -%}
{%- set struct = encodable.get_underlying_struct() -%}
ChipStructs.{{source.name}}Cluster{{struct.name}}
{%- else -%}
{{encodable.boxed_java_type}}
{%- endif -%}
{%- endmacro -%}

{%- macro encode_value_without_optional_nullable(source, encodable, depth) -%}
{%- if encodable.is_list -%}
ArrayList<{{encode_value_without_optional_nullable(source, encodable.without_list(), depth + 1)}}>
{%- elif encodable.is_struct -%}
{%- set struct = encodable.get_underlying_struct() -%}
ChipStructs.{{source.name}}Cluster{{struct.name}}
{%- else -%}
{{encodable.boxed_java_type}}
{%- endif -%}
{%- endmacro -%}

/*
*
* Copyright (c) 2023 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package matter.devicecontroller.cluster.clusters

import java.util.ArrayList
{% set typeLookup = idl | createLookupContext(cluster) %}
class {{cluster.name}}Cluster(private val endpointId: UShort) {
companion object {
const val CLUSTER_ID: UInt = {{cluster.code}}u
}
{% for command in cluster.commands | sort(attribute='code') -%}
{%- set callbackName = command | javaCommandCallbackName() -%}
{%- if not command.is_timed_invoke %}
fun {{command.name | lowfirst_except_acronym}}(callback: {{callbackName}}Callback
{%- if command.input_param -%}
{%- for field in (cluster.structs | named(command.input_param)).fields -%}
, {{field.name | lowfirst_except_acronym}}: {{encode_value(cluster, field | asEncodable(typeLookup), 0)}}
{%- endfor -%}
{%- endif -%}
) {
// Implementation needs to be added here
}
{%- endif %}

fun {{command.name | lowfirst_except_acronym}}(callback: {{callbackName}}Callback
{%- if command.input_param -%}
{%- for field in (cluster.structs | named(command.input_param)).fields -%}
, {{field.name | lowfirst_except_acronym}}: {{encode_value(cluster, field | asEncodable(typeLookup), 0)}}
{%- endfor -%}
{%- endif -%}
, timedInvokeTimeoutMs: Int) {
// Implementation needs to be added here
}
{% endfor %}
{%- set already_handled_command = [] -%}
{%- for command in cluster.commands | sort(attribute='code') -%}
{%- if command | isCommandNotDefaultCallback() -%}
{%- set callbackName = command | javaCommandCallbackName() -%}
{%- if callbackName not in already_handled_command %}
interface {{callbackName}}Callback {
fun onSuccess(
{%- for field in (cluster.structs | named(command.output_param)).fields -%}
{{field.name | lowfirst_except_acronym}}: {{encode_value(cluster, field | asEncodable(typeLookup), 0)}}{% if not loop.last %}, {% endif %}
{%- endfor -%}
)
fun onError(error: Exception)
}
{% if already_handled_command.append(callbackName) -%}
{%- endif -%}
{%- endif -%}
{%- endif -%}
{%- endfor %}
{%- set already_handled_attribute = [] -%}
{% for attribute in cluster.attributes | rejectattr('definition', 'is_field_global_name', typeLookup) %}
{%- set encodable = attribute.definition | asEncodable(typeLookup) -%}
{%- set interfaceName = attribute | javaAttributeCallbackName(typeLookup) -%}
{%- if interfaceName not in already_handled_attribute %}
interface {{interfaceName}} {
fun onSuccess(value: {{encode_value(cluster, encodable, 0)}})
fun onError(ex: Exception)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onError and onSubscriptionEstablished are always the same. Would it make sense to have a common base interface for them (so that a generic error handler could be written for anything) or would that break some ABI that already exists?

Same for common functions above (e.g. I see on error in other places too)

fun onSubscriptionEstablished(subscriptionId: Long)
}
{% if already_handled_attribute.append(interfaceName) -%}
{#- This block does nothing, it only exists to append to already_handled_attribute. -#}
{%- endif -%}
{%- endif -%}
{% endfor -%}
{% for attribute in cluster.attributes | sort(attribute='code') %}
fun read{{ attribute.definition.name | upfirst }}Attribute(
callback: {{ attribute | javaAttributeCallbackName(typeLookup) }}
) {
// Implementation needs to be added here
}
{% if attribute | isFabricScopedList(typeLookup) %}
fun read{{ attribute.definition.name | upfirst }}AttributeWithFabricFilter(
callback: {{ attribute | javaAttributeCallbackName(typeLookup) }},
isFabricFiltered: Boolean
) {
// Implementation needs to be added here
}

{% endif -%}
{%- if attribute.is_writable %}
{%- set encodable = attribute.definition | asEncodable(typeLookup) -%}
{%- set encodable2 = attribute.definition | asEncodable(typeLookup) -%}
{%- if not attribute.requires_timed_write %}
fun write{{ attribute.definition.name | upfirst }}Attribute(
callback: DefaultClusterCallback,
value: {{ encode_value_without_optional_nullable(cluster, encodable, 0) }}
) {
// Implementation needs to be added here
}
{% endif %}
fun write{{ attribute.definition.name | upfirst }}Attribute(
callback: DefaultClusterCallback,
value: {{ encode_value_without_optional_nullable(cluster, encodable2, 0) }},
timedWriteTimeoutMs: Int
) {
// Implementation needs to be added here
}
{% endif %}
{%- if attribute.is_subscribable %}
fun subscribe{{ attribute.definition.name | upfirst }}Attribute(
callback: {{ attribute | javaAttributeCallbackName(typeLookup) }},
minInterval: Int,
maxInterval: Int
) {
// Implementation needs to be added here
}
{% endif -%}
{%- endfor -%}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import("//build_overrides/build.gni")
import("//build_overrides/chip.gni")

matter_clusters_sources = [
{%- for cluster in clientClusters | sort(attribute='name') %}
{%- set typeLookup = idl | createLookupContext(cluster) %}
"${chip_root}/src/controller/java/generated/java/matter/devicecontroller/cluster/clusters/{{cluster.name}}Cluster.kt",
{%- endfor %}
]
Loading
Loading