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

DynamoDB Generated Schemas do not follow Ktlint's conventions #1521

Closed
1 task
hugoncosta opened this issue Feb 6, 2025 · 2 comments · Fixed by #1522
Closed
1 task

DynamoDB Generated Schemas do not follow Ktlint's conventions #1521

hugoncosta opened this issue Feb 6, 2025 · 2 comments · Fixed by #1522
Assignees
Labels
feature-request A feature should be added or improved.

Comments

@hugoncosta
Copy link

Describe the bug

Hello,

I am integrating the new DDB Mapper into my codebase and Ktlint (in its default configuration) is complaining that the generated code isn't correctly configured. I understand this might be outside your scope, and we should probably just not have Ktlint look at generated code, but it would still be a good to have

Regression Issue

  • Select this option if this issue appears to be a regression.

Expected behavior

In an ideal world, the code would follow the standard Ktlint rules

Current behavior

It doesn't follow it and it fails

> Task :ktlintMainSourceSetCheck FAILED
/Volumes/workplace/NetworkValidatorDDBMapper/build/NetworkValidatorCommonLibrary/NetworkValidatorCommonLibrary-1.0/AL2_x86_64/DEV.STD.PTHREAD/build/private/gradle/generated/ksp/main/kotlin/com/amazon/networkvalidator/ddb/dynamodbmapper/generatedschemas/RuleSchema.kt:53:28 Missing trailing comma before ")"
/Volumes/workplace/NetworkValidatorDDBMapper/build/NetworkValidatorCommonLibrary/NetworkValidatorCommonLibrary-1.0/AL2_x86_64/DEV.STD.PTHREAD/build/private/gradle/generated/ksp/main/kotlin/com/amazon/networkvalidator/ddb/dynamodbmapper/generatedschemas/RuleSchema.kt:59:41 Missing trailing comma before ")"
/Volumes/workplace/NetworkValidatorDDBMapper/build/NetworkValidatorCommonLibrary/NetworkValidatorCommonLibrary-1.0/AL2_x86_64/DEV.STD.PTHREAD/build/private/gradle/generated/ksp/main/kotlin/com/amazon/networkvalidator/ddb/dynamodbmapper/generatedschemas/RuleSchema.kt:65:29 Missing trailing comma before ")"
/Volumes/workplace/NetworkValidatorDDBMapper/build/NetworkValidatorCommonLibrary/NetworkValidatorCommonLibrary-1.0/AL2_x86_64/DEV.STD.PTHREAD/build/private/gradle/generated/ksp/main/kotlin/com/amazon/networkvalidator/ddb/dynamodbmapper/generatedschemas/RuleSchema.kt:71:29 Missing trailing comma before ")"
/Volumes/workplace/NetworkValidatorDDBMapper/build/NetworkValidatorCommonLibrary/NetworkValidatorCommonLibrary-1.0/AL2_x86_64/DEV.STD.PTHREAD/build/private/gradle/generated/ksp/main/kotlin/com/amazon/networkvalidator/ddb/dynamodbmapper/generatedschemas/RuleSchema.kt:78:28 Unexpected spacing before ":"

And this is the generated code

// Code generated by dynamodb-mapper-annotation-processor. DO NOT EDIT!

package com.amazon.networkvalidator.ddb.dynamodbmapper.generatedschemas

import aws.sdk.kotlin.hll.dynamodbmapper.DynamoDbMapper
import aws.sdk.kotlin.hll.dynamodbmapper.items.AttributeDescriptor
import aws.sdk.kotlin.hll.dynamodbmapper.items.ItemConverter
import aws.sdk.kotlin.hll.dynamodbmapper.items.ItemSchema
import aws.sdk.kotlin.hll.dynamodbmapper.items.KeySpec
import aws.sdk.kotlin.hll.dynamodbmapper.items.SimpleItemConverter
import aws.sdk.kotlin.hll.dynamodbmapper.model.Table
import aws.sdk.kotlin.hll.dynamodbmapper.values.scalars.BooleanConverter
import aws.sdk.kotlin.hll.dynamodbmapper.values.scalars.EnumConverter
import aws.sdk.kotlin.hll.dynamodbmapper.values.scalars.StringConverter
import aws.smithy.kotlin.runtime.ExperimentalApi
import com.amazon.networkvalidator.ddb.Rule
import com.amazon.networkvalidator.ddb.RuleRuntime

/**
 * A DSL-style builder for instances of [Rule]
 */
@ExperimentalApi
public class RuleBuilder {
    public var ruleName: String? = null
    public var ruleRuntime: RuleRuntime? = null
    public var active: Boolean? = null
    public var ioBound: Boolean? = null

    public fun build(): Rule {
        val ruleName = requireNotNull(ruleName) { "Missing value for ruleName" }
        val ruleRuntime = requireNotNull(ruleRuntime) { "Missing value for ruleRuntime" }
        val active = requireNotNull(active) { "Missing value for active" }
        val ioBound = requireNotNull(ioBound) { "Missing value for ioBound" }

        return Rule(
            ruleName,
            ruleRuntime,
            active,
            ioBound,
        )
    }
}

@ExperimentalApi
public object RuleConverter : ItemConverter<Rule> by SimpleItemConverter(
    builderFactory = ::RuleBuilder,
    build = RuleBuilder::build,
    descriptors = arrayOf(
        AttributeDescriptor(
            "ruleName",
            Rule::ruleName,
            RuleBuilder::ruleName::set,
            StringConverter
        ),
        AttributeDescriptor(
            "ruleRuntime",
            Rule::ruleRuntime,
            RuleBuilder::ruleRuntime::set,
            EnumConverter<RuleRuntime>()
        ),
        AttributeDescriptor(
            "active",
            Rule::active,
            RuleBuilder::active::set,
            BooleanConverter
        ),
        AttributeDescriptor(
            "ioBound",
            Rule::ioBound,
            RuleBuilder::ioBound::set,
            BooleanConverter
        ),
    ),
)

@ExperimentalApi
public object RuleSchema : ItemSchema.PartitionKey<Rule, String> {
    override val converter : RuleConverter = RuleConverter
    override val partitionKey: KeySpec<String> = aws.sdk.kotlin.hll.dynamodbmapper.items.KeySpec.String("ruleName")
}

/**
 * Returns a reference to a table named [name] containing items representing [Rule]
 */
@ExperimentalApi
public fun DynamoDbMapper.getRuleTable(name: String): Table.PartitionKey<Rule, String> = getTable(name, RuleSchema)

Steps to Reproduce

This happens in any of the generated schemas, as long as you run :ktlintMainSourceSetCheck on top of it. We're using Ktlint 1.2.1 and all the default rules.

Possible Solution

Add the trailing commas and remove the unexpected spacing? :)

Context

No response

AWS SDK for Kotlin version

1.4.14-beta

Platform (JVM/JS/Native)

Jvm

Operating system and version

macOS Sequioa 15.3

@hugoncosta hugoncosta added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Feb 6, 2025
@ianbotsf
Copy link
Contributor

ianbotsf commented Feb 6, 2025

Thanks for submitting this issue, @hugoncosta! We didn't make ktlint compliance a requirement for our generated code and so I can understand why ktlint would complain if run on the generated code. We can definitely improve the style of generated code such that it does pass more style checks but we won't be able to appease every style checker, nor every variant of ktlint's rules. For that reason, we recommend users to run style-checking tools only on code they write and maintain by setting appropriate filters, includes, etc.

That said, the two specific issues highlighted above (missing trailing comma, unexpected space before type specifier) should be easy to fix. I'll work up a PR shortly.

@ianbotsf ianbotsf added feature-request A feature should be added or improved. and removed bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Feb 6, 2025
@ianbotsf ianbotsf self-assigned this Feb 6, 2025
Copy link

github-actions bot commented Feb 6, 2025

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request A feature should be added or improved.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants