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

THRIFT-5571: add metadata map building to kotlin generator #2584

Merged
merged 7 commits into from
May 6, 2022

Conversation

jimexist
Copy link
Member

@jimexist jimexist commented Apr 21, 2022

add metadata map building to kotlin generator

  • Did you create an Apache Jira ticket? (not required for trivial changes)
  • If a ticket exists: Does your pull request title follow the pattern "THRIFT-NNNN: describe my issue"?
  • Did you squash your changes to a single commit? (not required, but preferred)
  • Did you do your best to avoid breaking changes? If one was needed, did you label the Jira ticket with "Breaking-Change"?
  • If your change does not involve any code, include [skip ci] anywhere in the commit message to free up build resources.

code generation example:

Bonk.kt
/**
 * Autogenerated by Thrift Compiler (0.17.0)
 *
 * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
 *  @generated
 */
@file:Suppress("ClassName", "PropertyName", "RedundantUnitReturnType", "NestedLambdaShadowedImplicitParameter", "RemoveRedundantQualifierName")
package thrift.test

data class Bonk(
  var message: kotlin.String? = null,
  var type: kotlin.Int? = null
) : org.apache.thrift.TBase<Bonk, Bonk._Fields> {
  enum class _Fields(private val thriftFieldId: kotlin.Short, private val fieldName: kotlin.String) : org.apache.thrift.TFieldIdEnum {
    MESSAGE(1, "message"),
    TYPE(2, "type");

    override fun getThriftFieldId() = thriftFieldId

    override fun getFieldName() = fieldName

    companion object {
      @kotlin.jvm.JvmStatic
      fun findByValue(value: kotlin.Int): _Fields? {
        return when (value) {
          1 -> MESSAGE
          2 -> TYPE
          else -> null
        }
      }

      @kotlin.jvm.JvmStatic
      fun findByName(name: kotlin.String): _Fields? {
        return when (name) {
          "message" -> MESSAGE
          "type" -> TYPE
          else -> null
        }
      }
    }
  }

  companion object {
    private val STRUCT_DESC: org.apache.thrift.protocol.TStruct = org.apache.thrift.protocol.TStruct("Bonk")
    private val MESSAGE_FIELD_DESC: org.apache.thrift.protocol.TField = org.apache.thrift.protocol.TField("message", org.apache.thrift.protocol.TType.STRING, 1)
    private val MESSAGE_FIELD_META_DATA: org.apache.thrift.meta_data.FieldMetaData = org.apache.thrift.meta_data.FieldMetaData(
      "message",
      org.apache.thrift.TFieldRequirementType.DEFAULT,
      org.apache.thrift.meta_data.FieldValueMetaData(org.apache.thrift.protocol.TType.STRING))
    private val TYPE_FIELD_DESC: org.apache.thrift.protocol.TField = org.apache.thrift.protocol.TField("type", org.apache.thrift.protocol.TType.I32, 2)
    private val TYPE_FIELD_META_DATA: org.apache.thrift.meta_data.FieldMetaData = org.apache.thrift.meta_data.FieldMetaData(
      "type",
      org.apache.thrift.TFieldRequirementType.DEFAULT,
      org.apache.thrift.meta_data.FieldValueMetaData(org.apache.thrift.protocol.TType.I32))
    private val metadata: Map<_Fields, org.apache.thrift.meta_data.FieldMetaData> = mapOf(
      _Fields.MESSAGE to MESSAGE_FIELD_META_DATA,
      _Fields.TYPE to TYPE_FIELD_META_DATA,
    )
    init {
      org.apache.thrift.meta_data.FieldMetaData.addStructMetaDataMap(Bonk::class.java, metadata)
    }
  }

  private object BonkStandardScheme : org.apache.thrift.scheme.StandardScheme<Bonk>() {
    override fun read(iproto: org.apache.thrift.protocol.TProtocol, struct: Bonk) {
      iproto.apply {
        readStruct {
          var stopped = false
          while (!stopped) {
            stopped = readField {
              val skipNext = { org.apache.thrift.protocol.TProtocolUtil.skip(iproto, type) }
              when (id.toInt()) {
                1 -> {
                  if (type == org.apache.thrift.protocol.TType.STRING) {
                    struct.message = readString()
                  } else {
                    skipNext()
                  }
                }
                2 -> {
                  if (type == org.apache.thrift.protocol.TType.I32) {
                    struct.type = readI32()
                  } else {
                    skipNext()
                  }
                }
                else -> skipNext()
              }
            }
          }
          struct.validate()
        }
      }
    }

    override fun write(oproto: org.apache.thrift.protocol.TProtocol, struct: Bonk) {
      struct.validate()
      oproto.apply {
        writeStruct(STRUCT_DESC) {
          struct.message?.let {
            writeField(MESSAGE_FIELD_DESC) {
              writeString(it)
            }
          }
          struct.type?.let {
            writeField(TYPE_FIELD_DESC) {
              writeI32(it)
            }
          }
          writeFieldStop()
        }
      }
    }

  }

  override fun compareTo(other: Bonk?): kotlin.Int {
    val comparator = compareBy<Bonk> { it::class.java.name }
      .thenBy { it.message } 
      .thenBy { it.type } 
    return nullsFirst(comparator).compare(this, other)
  }

  override fun fieldForId(fieldId: kotlin.Int): _Fields {
    return _Fields.findByValue(fieldId) ?: throw kotlin.IllegalArgumentException("invalid fieldId $fieldId")
  }

  override fun getFieldValue(field: _Fields): kotlin.Any? {
    return when (field) {
      _Fields.MESSAGE -> this.message
      _Fields.TYPE -> this.type
    }
  }

  @Suppress("UNCHECKED_CAST")
  override fun setFieldValue(field: _Fields, value: kotlin.Any?): kotlin.Unit {
    when (field) {
      _Fields.MESSAGE -> this.message = value as kotlin.String?
      _Fields.TYPE -> this.type = value as kotlin.Int?
    }
  }

  override fun isSet(field: _Fields): kotlin.Boolean {
    return when (field) {
      _Fields.MESSAGE -> this.message != null
      _Fields.TYPE -> this.type != null
    }
  }

  override fun deepCopy(): Bonk {
    return Bonk (
      message,
      type,
    )
  }

  override fun clear(): kotlin.Unit {
    message = null
    type = null
  }

  @kotlin.jvm.Throws(org.apache.thrift.TException::class)
  fun validate(): kotlin.Unit {
  }

  override fun read(iproto: org.apache.thrift.protocol.TProtocol): kotlin.Unit {
    require(org.apache.thrift.scheme.StandardScheme::class.java == iproto.scheme) { "only standard scheme is supported for now" }
    BonkStandardScheme.read(iproto, this)
  }

  override fun write(oproto: org.apache.thrift.protocol.TProtocol): kotlin.Unit {
    require(org.apache.thrift.scheme.StandardScheme::class.java == oproto.scheme) { "only standard scheme is supported for now" }
    BonkStandardScheme.write(oproto, this)
  }

}

@jimexist jimexist changed the title add metadata map building to kotlin generator THRIFT-5571: add metadata map building to kotlin generator Apr 21, 2022
@jimexist jimexist force-pushed the kotlin-lib-add-meta branch 2 times, most recently from a5aa0cc to 3962156 Compare April 22, 2022 05:25
@jimexist
Copy link
Member Author

had to rebase to pickup fix from #2585

@jimexist jimexist force-pushed the kotlin-lib-add-meta branch from 3962156 to 38e9dd6 Compare April 27, 2022 11:41
@ctubbsii ctubbsii merged commit 6c002b6 into apache:master May 6, 2022
@jimexist jimexist deleted the kotlin-lib-add-meta branch May 6, 2022 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants