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

Respect java_package option in protobuf files #324

Merged
merged 14 commits into from Aug 31, 2020
Merged

Conversation

dmarticus
Copy link
Contributor

@dmarticus dmarticus commented Aug 25, 2020

Closes #318. Includes test cases provided by the issue filer :), plus some additional ones that cover missing package info altogether.

This PR is intended to give Skeuomorph comparable behavior to ScalaPB when it comes to handling the package structure -- I want to make it so that the package hierarchy goes like this (1) java package (if present) (2) package name (if present) (3) file name. More on how ScalaPB does it here here.

@codecov
Copy link

codecov bot commented Aug 25, 2020

Codecov Report

Merging #324 into master will increase coverage by 0.07%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #324      +/-   ##
==========================================
+ Coverage   83.93%   84.01%   +0.07%     
==========================================
  Files          29       29              
  Lines        1899     1908       +9     
  Branches       23       27       +4     
==========================================
+ Hits         1594     1603       +9     
  Misses        305      305              
Impacted Files Coverage Δ
...cala/higherkindness/skeuomorph/avro/Protocol.scala 31.81% <ø> (ø)
.../scala/higherkindness/skeuomorph/avro/schema.scala 57.62% <ø> (ø)
...main/scala/higherkindness/skeuomorph/Printer.scala 82.14% <100.00%> (-0.62%) ⬇️
...igherkindness/skeuomorph/protobuf/ParseProto.scala 96.62% <100.00%> (+0.20%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 999618f...1c2e040. Read the comment docs.

@dmarticus dmarticus changed the title [WIP] Respect java_package option in protobuf files Respect java_package option in protobuf files Aug 27, 2020
val fileName: String = formatName(file.getName)
// This package naming behavior is consistent with ScalaPB, see
// https://scalapb.github.io/docs/generated-code/#default-package-structure
if (javaPackage.isEmpty) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Quick note here: I implemented this method to stay consistent with the parsing behavior of ScalaPB. If we want to make it so that we don’t use the filename on the absence of any package information (which would fail the parsing step), I’m open to that as well. At this point, without better direction, I'm just going for compatibility with an existing solution.

// header (which will just return the name of the type). This method trims the type name from a fully-
// qualified name if necessary, which provides consistent behavior when comparing the name of a message
// to the name of a type
fullName.substring(fullName.lastIndexOf(".") + 1)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another long comment that basically explains the caveat here: we no longer will be comparing fully-qualified type names to the type that appears in the method, because getTypeName behaves differently depending on what it's called against (if called against a file with a package header, it returns the fully-qualified type, whereas if called against a file with a java_package header, it just returns the typeName). In this case, we always want to just use the typename, so we parse the string down if necessary. Yes, I know it's ugly "strings as types" but when working with the java methods that only return strings there's not a lot I can do about it :|

@@ -1,6 +1,6 @@
syntax = "proto3";

package com.acme;
option java_package = "com.acme";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

testing that the java_package interop works seamlessly when importing proto files (spoiler: it does)

@@ -25,7 +25,7 @@ import org.apache.avro.{Schema, Protocol => AvroProtocol}
import higherkindness.droste._
import higherkindness.droste.syntax.all._

import scala.jdk.CollectionConverters._
import scala.collection.JavaConverters._
Copy link
Contributor Author

Choose a reason for hiding this comment

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

fwiw the explanation on why I'm making this change is nicely laid out by Birchall here: #322 (comment)

Copy link
Member

@juanpedromoreno juanpedromoreno left a comment

Choose a reason for hiding this comment

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

Great job @dmarticus!

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.

Investigate allowing optional package parameters when parsing Protobuf schema
2 participants