Skip to content
This repository has been archived by the owner on Jun 28, 2022. It is now read-only.

Add ConfigV2 Validator #2672

Merged

Conversation

andreamlin
Copy link
Contributor

@andreamlin andreamlin commented Mar 29, 2019

Add util to validate a ConfigProto object as valid under the V2 schema.

This util is currently not used, but it is planned to be used very soon.

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Mar 29, 2019
@codecov
Copy link

codecov bot commented Mar 29, 2019

Codecov Report

Merging #2672 into gapic_config_v2 will decrease coverage by <.01%.
The diff coverage is 82.75%.

Impacted file tree graph

@@                  Coverage Diff                  @@
##             gapic_config_v2    #2672      +/-   ##
=====================================================
- Coverage              86.77%   86.76%   -0.01%     
- Complexity              5584     5588       +4     
=====================================================
  Files                    466      467       +1     
  Lines                  22193    22222      +29     
  Branches                2428     2430       +2     
=====================================================
+ Hits                   19257    19281      +24     
- Misses                  2078     2081       +3     
- Partials                 858      860       +2
Impacted Files Coverage Δ Complexity Δ
...e/api/codegen/util/ConfigNextVersionValidator.java 82.75% <82.75%> (ø) 4 <4> (?)

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 280e279...98fe47a. Read the comment docs.

@codecov
Copy link

codecov bot commented Mar 29, 2019

Codecov Report

Merging #2672 into gapic_config_v2 will decrease coverage by <.01%.
The diff coverage is 84.21%.

Impacted file tree graph

@@                  Coverage Diff                  @@
##             gapic_config_v2    #2672      +/-   ##
=====================================================
- Coverage              86.76%   86.76%   -0.01%     
- Complexity              5584     5589       +5     
=====================================================
  Files                    466      467       +1     
  Lines                  22185    22204      +19     
  Branches                2428     2430       +2     
=====================================================
+ Hits                   19249    19265      +16     
- Misses                  2078     2080       +2     
- Partials                 858      859       +1
Impacted Files Coverage Δ Complexity Δ
...oogle/api/codegen/util/ConfigVersionValidator.java 84.21% <84.21%> (ø) 5 <5> (?)

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 1214ae5...43a9a6c. Read the comment docs.

@andreamlin
Copy link
Contributor Author

PTAL

import com.google.protobuf.util.JsonFormat.TypeRegistry;
import javax.annotation.Nonnull;

public class ConfigNextVersionValidator {
Copy link
Contributor

Choose a reason for hiding this comment

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

This name is a bit odd. What about just ConfigVersionValidator?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure


try {
// Serializing utils for Config V2.
TypeRegistry typeRegistryV2 =
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it necessary to use a TypeRegistry and json formatting functions? Can we not just use toByteArray, parseFrom(byte[]), and check for unknown fields?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we use the toByteArray and parseFrom functions, those will still preserve the unknown fields. The thing with the unknown fields is that they can exist at every single level of the protobuf object tree, and I don't feel like writing a tree search function to parse through every child message type and looking for unknown types (i've already been burned trying to implement a BFS haha)

Copy link
Contributor

Choose a reason for hiding this comment

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

What about using a DiscardUnknownFieldsParser? I don't know, but my intuition is that there is an almost-one-liner that does this, something like:

configBytes = config.toByteArray();
configV2Bytes = DiscardUnknownFieldsParser.wrap(ConfigV2.parser())
  .parseFrom(configBytes)
  .toByteArray();
// compare byte arrays ...

Copy link
Contributor

Choose a reason for hiding this comment

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

But - maybe I'm just wrong? Like, does the above fail because of unknown fields other than v1/v2 differences?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh i didn't see that! i heard that Go had the same thing, but i didn't look hard enough for a java version

Copy link
Contributor Author

Choose a reason for hiding this comment

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

much shorter now :) i must have been looking at stackoverflow posts that are outdated now for help on this

* Throw {@link IllegalStateException} iff the given input contains fields unknown to the {@link
* com.google.api.codegen.v2.ConfigProto} schema.
*/
public void checkIsNextVersionConfig(@Nonnull com.google.api.codegen.ConfigProto configV1Proto)
Copy link
Contributor

Choose a reason for hiding this comment

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

What about something like validateV2Config? I don't think it is desirable to make the function name version-agnostic (unless the implementation is also version-agnostic)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok!

@andreamlin
Copy link
Contributor Author

andreamlin commented Mar 29, 2019

asking protobuf team to make a java release too protocolbuffers/protobuf#5972 wrong pr

@andreamlin
Copy link
Contributor Author

renamed things
PTAL

@andreamlin
Copy link
Contributor Author

PTAL

}

try {

Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: remove extra line

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

if (!Arrays.equals(configV2.toByteArray(), configV1Proto.toByteArray())) {
throw new IllegalStateException(
String.format(
"Unknown fields in to ConfigProto v2 in configProto: %s",
Copy link
Contributor

Choose a reason for hiding this comment

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

s/in to ConfigProto/to ConfigProto/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@andreamlin andreamlin merged commit 5ecc799 into googleapis:gapic_config_v2 Apr 1, 2019
@andreamlin andreamlin deleted the validate_config_v2 branch April 1, 2019 16:01
andreamlin added a commit that referenced this pull request Apr 22, 2019
* Add Gapic config v2 (#2665)
* Whittling down config_v2 (#2666)
* Add ConfigV2 Validator (#2672)
* AutoValue LongRunningConfig; always use gapic config's polling settings (#2698)
* ResourceNameOneofConfig fixes (#2704)
* Start parsing GAPIC config v2 (#2703)
* Bring back timeout millis in GAPIC config v2 (#2708)
* Resource names across different protofiles (#2711)
* Fix missing default retries (#2718)
* Bug fixes for gapic config v2 parsing (#2717)
busunkim96 pushed a commit to busunkim96/gapic-generator that referenced this pull request Nov 7, 2019
* Add Gapic config v2 (googleapis#2665)
* Whittling down config_v2 (googleapis#2666)
* Add ConfigV2 Validator (googleapis#2672)
* AutoValue LongRunningConfig; always use gapic config's polling settings (googleapis#2698)
* ResourceNameOneofConfig fixes (googleapis#2704)
* Start parsing GAPIC config v2 (googleapis#2703)
* Bring back timeout millis in GAPIC config v2 (googleapis#2708)
* Resource names across different protofiles (googleapis#2711)
* Fix missing default retries (googleapis#2718)
* Bug fixes for gapic config v2 parsing (googleapis#2717)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants