[go: nahoru, domu]

Skip to content

Commit

Permalink
Ignore already with protoc shipped files
Browse files Browse the repository at this point in the history
  • Loading branch information
valaphee committed Apr 5, 2022
1 parent 369d62a commit 50fd3ee
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 31 deletions.
10 changes: 5 additions & 5 deletions src/main/kotlin/com/valaphee/protod/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ import java.io.File

fun main(arguments: Array<String>) {
val argumentParser = ArgParser("protod")
val inputArgument by argumentParser.option(ArgType.String, "input", "i", "Input file").required()
val outputArgument by argumentParser.option(ArgType.String, "output", "o", "Output path").required()
val input by argumentParser.option(ArgType.String, "input", "i", "Input file").required()
val output by argumentParser.option(ArgType.String, "output", "o", "Output path").required()
argumentParser.parse(arguments)

val bytes = File(inputArgument).readBytes()
val bytes = File(input).readBytes()
val files = mutableListOf<DescriptorProtos.FileDescriptorProto>()
String(bytes, Charsets.US_ASCII).occurrencesOf(".proto").forEach {
var offset = 0
Expand Down Expand Up @@ -78,14 +78,14 @@ fun main(arguments: Array<String>) {
val messageExtensions = mutableMapOf<String, MutableMap<Int, DescriptorProtos.FieldDescriptorProto>>()
files.forEach { file -> file.extensionList.forEach { extension -> messages[extension.typeName]?.let { messageExtensions.getOrPut(extension.extendee) { mutableMapOf() }[extension.number] = extension } } }

val outputPath = File(outputArgument)
val outputPath = File(output)
files.forEach { file ->
if (!included.contains(file.name)) File(outputPath, file.name).apply { parentFile.mkdirs() }.printWriter().use { printWriter ->
printWriter.println(
"""
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*/
""".trimIndent()
""".trimIndent()
)
ProtoWriter(printWriter, enums, messages, messageExtensions).print(file)
}
Expand Down
59 changes: 33 additions & 26 deletions src/main/kotlin/com/valaphee/protod/ProtoWriter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class ProtoWriter(
}

printImports(file.dependencyList, file.publicDependencyList)
println()
println("option java_multiple_files = true;")
if (!file.options.hasJavaGenericServices() && file.serviceCount != 0) println("option java_generic_services = true;")
printOptions(file.options)
printExtensions(file.extensionList)
printEnums(file.enumTypeList)
Expand Down Expand Up @@ -72,10 +75,12 @@ class ProtoWriter(
private fun generateOptions(options: GeneratedMessageV3.ExtendableMessage<*>): MutableList<String> {
val generatedOptions = mutableListOf<String>()
options.allFields.forEach {
generatedOptions += "${it.key.name} = ${when (it.value) {
is String -> "\"${it.value}\""
else -> it.value
}}"
generatedOptions += "${it.key.name} = ${
when (it.value) {
is String -> "\"${it.value}\""
else -> it.value
}
}"
}
val messageExtensions = messageExtensions[".${options.descriptorForType.fullName}"] ?: emptyMap()
options.unknownFields.asMap().forEach { unknownField ->
Expand All @@ -87,29 +92,31 @@ class ProtoWriter(
val tag = codedInputStream.readTag()
if (tag == 0) break
val fieldNumber = WireFormat.getTagFieldNumber(tag)
val messageExtensionField = messageExtensionFields.single { it.number == fieldNumber}
generatedOptions += "(${messageExtension.name}).${messageExtensionField.name} = ${when (messageExtensionField.type) {
DescriptorProtos.FieldDescriptorProto.Type.TYPE_DOUBLE -> codedInputStream.readDouble()
DescriptorProtos.FieldDescriptorProto.Type.TYPE_FLOAT -> codedInputStream.readFloat()
DescriptorProtos.FieldDescriptorProto.Type.TYPE_INT64 -> codedInputStream.readInt64()
DescriptorProtos.FieldDescriptorProto.Type.TYPE_UINT64 -> codedInputStream.readUInt64()
DescriptorProtos.FieldDescriptorProto.Type.TYPE_INT32 -> codedInputStream.readInt32()
DescriptorProtos.FieldDescriptorProto.Type.TYPE_FIXED64 -> codedInputStream.readFixed64()
DescriptorProtos.FieldDescriptorProto.Type.TYPE_FIXED32 -> codedInputStream.readFixed32()
DescriptorProtos.FieldDescriptorProto.Type.TYPE_BOOL -> codedInputStream.readBool()
DescriptorProtos.FieldDescriptorProto.Type.TYPE_STRING -> "\"${codedInputStream.readString()}\""
DescriptorProtos.FieldDescriptorProto.Type.TYPE_BYTES -> codedInputStream.readBytes()
DescriptorProtos.FieldDescriptorProto.Type.TYPE_UINT32 -> codedInputStream.readUInt32()
DescriptorProtos.FieldDescriptorProto.Type.TYPE_ENUM -> {
val value = codedInputStream.readEnum()
checkNotNull(enums[messageExtensionField.typeName]).valueList.single { it.number == value}.name
val messageExtensionField = messageExtensionFields.single { it.number == fieldNumber }
generatedOptions += "(${messageExtension.name}).${messageExtensionField.name} = ${
when (messageExtensionField.type) {
DescriptorProtos.FieldDescriptorProto.Type.TYPE_DOUBLE -> codedInputStream.readDouble()
DescriptorProtos.FieldDescriptorProto.Type.TYPE_FLOAT -> codedInputStream.readFloat()
DescriptorProtos.FieldDescriptorProto.Type.TYPE_INT64 -> codedInputStream.readInt64()
DescriptorProtos.FieldDescriptorProto.Type.TYPE_UINT64 -> codedInputStream.readUInt64()
DescriptorProtos.FieldDescriptorProto.Type.TYPE_INT32 -> codedInputStream.readInt32()
DescriptorProtos.FieldDescriptorProto.Type.TYPE_FIXED64 -> codedInputStream.readFixed64()
DescriptorProtos.FieldDescriptorProto.Type.TYPE_FIXED32 -> codedInputStream.readFixed32()
DescriptorProtos.FieldDescriptorProto.Type.TYPE_BOOL -> codedInputStream.readBool()
DescriptorProtos.FieldDescriptorProto.Type.TYPE_STRING -> "\"${codedInputStream.readString()}\""
DescriptorProtos.FieldDescriptorProto.Type.TYPE_BYTES -> codedInputStream.readBytes()
DescriptorProtos.FieldDescriptorProto.Type.TYPE_UINT32 -> codedInputStream.readUInt32()
DescriptorProtos.FieldDescriptorProto.Type.TYPE_ENUM -> {
val value = codedInputStream.readEnum()
checkNotNull(enums[messageExtensionField.typeName]).valueList.single { it.number == value }.name
}
DescriptorProtos.FieldDescriptorProto.Type.TYPE_SFIXED32 -> codedInputStream.readSFixed32()
DescriptorProtos.FieldDescriptorProto.Type.TYPE_SFIXED64 -> codedInputStream.readSFixed64()
DescriptorProtos.FieldDescriptorProto.Type.TYPE_SINT32 -> codedInputStream.readSInt32()
DescriptorProtos.FieldDescriptorProto.Type.TYPE_SINT64 -> codedInputStream.readSInt64()
else -> TODO()
}
DescriptorProtos.FieldDescriptorProto.Type.TYPE_SFIXED32 -> codedInputStream.readSFixed32()
DescriptorProtos.FieldDescriptorProto.Type.TYPE_SFIXED64 -> codedInputStream.readSFixed64()
DescriptorProtos.FieldDescriptorProto.Type.TYPE_SINT32 -> codedInputStream.readSInt32()
DescriptorProtos.FieldDescriptorProto.Type.TYPE_SINT64 -> codedInputStream.readSInt64()
else -> TODO()
}}"
}"
}
}
}
Expand Down

0 comments on commit 50fd3ee

Please sign in to comment.