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

SampleGen: default calling forms #2713

Merged
merged 10 commits into from
Apr 22, 2019

Conversation

yihanzhen
Copy link
Contributor

@yihanzhen yihanzhen commented Apr 12, 2019

When:

- samples:
    standalone:
    - region_tag: some_tag
   # unspecified calling forms
      value_sets: a_value_set, b_value_set

Default calling forms are used.

Also use region tags as file names when there is only one sample that has this region tag.

Apologies that this PR is causing huge changes in the baselines :( This is unavoidable when we rename files.

To review the styles of default calling forms: Please search "Test default calling forms for unary methods." (unary), "Test default calling form for paging methods." (paging) and "this_tag_should_be_the_name_of_the_file" (LRO).

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

codecov bot commented Apr 12, 2019

Codecov Report

Merging #2713 into master will increase coverage by <.01%.
The diff coverage is 97.68%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #2713      +/-   ##
============================================
+ Coverage     86.95%   86.96%   +<.01%     
- Complexity     5622     5645      +23     
============================================
  Files           468      469       +1     
  Lines         22446    22532      +86     
  Branches       2433     2437       +4     
============================================
+ Hits          19519    19595      +76     
- Misses         2079     2085       +6     
- Partials        848      852       +4
Impacted Files Coverage Δ Complexity Δ
...api/codegen/viewmodel/OptionalArrayMethodView.java 100% <ø> (ø) 14 <0> (ø) ⬇️
.../transformer/py/PythonGapicSamplesTransformer.java 100% <100%> (ø) 20 <1> (+1) ⬆️
...api/codegen/transformer/py/PythonSurfaceNamer.java 92.02% <100%> (+0.04%) 80 <2> (+1) ⬆️
...api/codegen/transformer/ruby/RubySurfaceNamer.java 93.4% <100%> (+0.06%) 91 <2> (+2) ⬆️
...va/com/google/api/codegen/config/SampleConfig.java 100% <100%> (ø) 3 <3> (?)
...codegen/transformer/csharp/CSharpSurfaceNamer.java 91.77% <100%> (+0.05%) 89 <2> (+2) ⬆️
...en/transformer/php/PhpGapicSamplesTransformer.java 92.95% <100%> (ø) 11 <1> (+1) ⬆️
...nsformer/nodejs/NodeJSGapicSamplesTransformer.java 100% <100%> (ø) 11 <1> (+1) ⬆️
...codegen/transformer/nodejs/NodeJSSurfaceNamer.java 88.38% <100%> (+0.04%) 95 <2> (+1) ⬆️
...gle/api/codegen/transformer/SampleTransformer.java 97.69% <100%> (-0.89%) 23 <3> (-4)
... and 13 more

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 bcdf090...cf082c9. Read the comment docs.

@yihanzhen
Copy link
Contributor Author

PTAL @googleapis/samplegen

@yihanzhen
Copy link
Contributor Author

@vchudnov-g We can discuss more on whether to let users specify "default" to generate samples against default calling forms, but hopefully this PR (even on a fork) will unblock @beccasaurus when I am gone next week.

Copy link
Contributor

@vchudnov-g vchudnov-g left a comment

Choose a reason for hiding this comment

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

LGTM. Just some minor comments. See whether there are tests we should add, but I'm not sure there are.

@@ -0,0 +1,40 @@
/* Copyright 2018 Google LLC
Copy link
Contributor

Choose a reason for hiding this comment

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

2019


public abstract SampleSpec.SampleType type();

public static SampleConfig create(
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider sticking with the Builder pattern for consistency.

@@ -112,40 +114,76 @@ private static boolean expressionMatchesId(String expression, String id) {
}

/**
* Returns the SampleValueSets that were specified for this methodForm and sampleType.
* Returns all the `ids` that can match to one or more elements of `expressions` through regex.
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this correct?: "Matches all the IDs within targets that match one or more elements of expressions. The IDs are extracted from elements of targets via targetToId."

* sets, we use the default one. For incode samples, we always use the default value set derived
* from sample_code_init_fields for backward compatibility.
*
* <p>We should probably disable default value sets after incode samples stop using
Copy link
Contributor

Choose a reason for hiding this comment

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

IIUC, I agree. For all samples (standalone samples now, and in-code samples later), if no value set is specified, then no sample should be produced (this is the case already in standalone, right? That doesn't change?).
This is the only way to suppress samples with default calling forms: not specifying a value set.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Re last paragraph: or not specify samples directive at all.

* `defaultCallingForm` can match none of the calling forms in `allValidCallingForms`. For
* example, the `defaultCallingForm` for a C# unary call is `Request`, but it's not a valid
* calling form when generating asynchronous samples using the generated asynchonous public
* method. In this case we will not generate samples for this method.
Copy link
Contributor

Choose a reason for hiding this comment

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

To clarify: "Each RPC could result in multiple library methods. Not all these library methods will have default calling forms, but we intend for every RPC to have a default calling form in at least one of its library methods."

regionTagCount.get(regionTag) != null && regionTagCount.get(regionTag) > 0,
"Sample not registered.");
String fileName;
if (regionTagCount.get(regionTag) == 1) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this method and the previous share some code, consider whether factoring out the logic makes sense:

public ... GetSampleClassName(...) {
  return constructName(sample, method, namer.getApiSampleClassName);
}

public ... GetSampleFileName(...) {
  filename =  constructName(sample, method, namer.getApiSampleFileName); 
  addFile(fileName, ...);
  return fileName;
}

private ... constructName(MethodSampleView sample, String method, ... namingFunc) {
  ...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch! But I think passing the lambda here is a bit not Java idiomatic.

@@ -792,10 +792,10 @@ public String getApiSampleClassName(String... parts) {
}

/**
* The name of the file holding the sample class for a single API method and variant. The variant
* is typically a calling form.
* The name of the file holding the sample class for a single API method and variant. Constucted
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: "The name is constructed...." (and sp on "constructed")
(It seems like in languages where the first part of the comment is a phrase, subsequent content is full sentences)

@@ -25,13 +28,57 @@
public class SampleFileRegistry {

private final Map<String, SampleInfo> files = new HashMap<>();
private final Map<String, Integer> regionTagCount = new HashMap<>();
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you expand the class comments just slightly to mention how the region tag affects the naming?

@@ -181,6 +182,48 @@ static RpcType fromMethodContext(MethodContext context) {
.put(RUBY, RpcType.BIDI_STREAMING, ImmutableList.of(RequestStreamingBidi))
.build();

private static final Table<TargetLanguage, RpcType, CallingForm> DEFAULT_CALLING_FORM_TABLE =
ImmutableTable.<TargetLanguage, RpcType, CallingForm>builder()
Copy link
Contributor

Choose a reason for hiding this comment

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

It'll be nice when we can read this from a config file..... ;-)

sampleStreamBooks();
}
}
// FIXME: Insert here clean-up code.
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we still need this FIXME?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No we don't! I'm gonna do it in a chaser PR, together with some other Java template fixes.

@yihanzhen yihanzhen merged commit ff1e719 into googleapis:master Apr 22, 2019
busunkim96 pushed a commit to busunkim96/gapic-generator that referenced this pull request Nov 7, 2019
When:
```yaml
- samples:
    standalone:
    - region_tag: some_tag
   # unspecified calling forms
      value_sets: a_value_set, b_value_set
```
Default calling forms are used.

Also use region tags as file names when there is only one sample that has this region tag.
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. Core: Sample-gen
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants