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

Commit

Permalink
Nodeify Discovery objects (#1322)
Browse files Browse the repository at this point in the history
* merge master to Discogapic (#1304)

* Fix JSON pretty print in Ruby sample (#1254)

* Ruby: README Overhaul (feat. examples) (#1245)

* go: use new metadata methods (#1262)

Updates googleapis/google-cloud-go#624.

metadata.FromContext and metdata.NewContext
are replaced by metadata.{New,From}{Incoming,Outgoing}Context.

Generated unit tests now verify that x-goog-api-client header
is inserted.

Additionally, clients now save a string slice containing x-goog-api-client
header value instead of the value by itself.
This is a small optimization so that we don't create a new slice every time.

* Change media body from an object to a string (#1256)

Samples fail before sending a request if the media body is an object.

* Fix NONE auth Go sample (#1261)

* Change Node.js unit test require. (#1264)

* Ruby: Allow doc gapic generation to generate tests. (#1266)

* Ruby: Remove aliasing from method samples and tests (#1253)

* Ruby: Fix broken baselines from aliasing fix (#1269)

* Remove unused code (#1270)

* Python: README overhaul. (#1263)

* Ruby: Update gemspec and add useful metadata files (#1258)

* Ruby: Update gemspec and add useful metadata files

* Specify HTTP client when constructing client (#1275)

If a HTTP client is not passed to `build`, the Google API Python client
will create a default one and attempt to authenticate it. This causes
failures in environments where ADC auth is not available (Travis).

In any case, it's not useful to access any auth code in the mock tests.
This commit removes that possibility.

* Fix JSON print in Ruby (#1274)

It turns out that `to_h` is not a method available on every object
returned by the samples at the moment. Also, there's some kind of
decoding bug showing up in certain samples where the JSON module is
unable to unparse the response.

It's troublesome to have so much boilerplate just to pretty print. This
commit removes the pretty calls in favor of the vanilla `to_json` method
implemented by the base model class in the Ruby client. In the future,
once the bugs w.r.t `pretty_generate` have been resolved, we can revert.

* NodeJS: Update version index to support partial veneers (#1267)

* NodeJS: Updates to package.json (#1268)

* Migrate import disambiguation to Python MVVM (#1243)

* Fix Node package.json (#1282)

* go: use time.UnixNano instead of testutil.UIDSpace (#1279)

Since generated code should be testable from api-client-staging repo,
we want to reduce dependency on other cloud.google.com/go packages,
especially internal ones.

Clients will still depend on testutil for ProjID and TokenSource.
This is OK, since they can be easily reimplemented in google-client-staging
repo itself.

* Update Java grpc metadata for new staging structure (#1265)

* PHP: share credentials with operations client (#1283)

* java: move CredentialProvider up to ClientSettings (#1251)

This implements the client side changes to support
googleapis/gax-java#305.

* NodeJS: generate readme. (#1240)

* NodeJS: Small additions to the package.json (#1289)

* NodeJS: Use fileheader for copyright lines. (#1293)

* NodeJS: Use correct casing for partial veneers (#1295)

* NodeJS: Small readme fix from GCN reviews (#1294)

* go: make longrunning methods use generated longrunning client (#1288)

The cloud.google.com/go/longrunning package will need to change
slightly to accomodate this change.

xGoogHeader is now gone from the generated longrunnging types,
as the header will be automatically inserted by LROClient itself.

* NodeJS: Update partial veneer surface to add api documentation. (#1296)

* NodeJS: Metadata updates. (#1298)

* Readme: Update readme templates to add api summaries. (#1299)

* Add PHP Exception tests (#951)

* Add exception tests
* Remove unnecessary checks in exception tests

* Process markdown cloud links for PHP (#1091)

* Process markdown cloud links for PHP
* Restructure comment formatting

* go: return error creating LRO client instead of panicking (#1300)

* fails b/c Node.empty() uses null params

* one test passes

* all current tests pass

* tests for Node parent()

* Test

* removed Method.empty():

* reordering methods for convention

* made Node.from(Node, path) methods package private

* removed path() from Document, Schema, and Method

* removed static constructors with empty parent node
  • Loading branch information
andreamlin authored Jun 5, 2017
1 parent 41e4791 commit f50074f
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 92 deletions.
76 changes: 52 additions & 24 deletions src/main/java/com/google/api/codegen/discovery/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
*/
package com.google.api.codegen.discovery;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.auto.value.AutoValue;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;

/**
* A representation of a Discovery Document.
Expand All @@ -29,7 +31,7 @@
* example, this class combines all methods in the Discovery Document into one list for convenience.
*/
@AutoValue
public abstract class Document {
public abstract class Document implements Node {

// TODO(saicheems): Assert that all references link to a valid schema?

Expand All @@ -54,8 +56,9 @@ public static Document from(DiscoveryNode root) {
}
String canonicalName = root.getString("canonicalName");
String description = root.getString("description");
String id = root.getString("id");
Map<String, Schema> schemas = parseSchemas(root);
List<Method> methods = parseMethods(root, "");
List<Method> methods = parseMethods(root);
Collections.sort(methods); // Ensure methods are ordered alphabetically by their ID.
String name = root.getString("name");
if (canonicalName.isEmpty()) {
Expand All @@ -68,21 +71,32 @@ public static Document from(DiscoveryNode root) {
String version = root.getString("version");
boolean versionModule = root.getBoolean("version_module");

return new AutoValue_Document(
"", // authInstructionsUrl (only intended to be overridden).
authType,
canonicalName,
description,
"", // discoveryDocUrl (only intended to be overridden).
methods,
name,
revision,
rootUrl,
schemas,
servicePath,
title,
version,
versionModule);
Document thisDocument =
new AutoValue_Document(
"", // authInstructionsUrl (only intended to be overridden).
authType,
canonicalName,
description,
"", // discoveryDocUrl (only intended to be overridden).
id,
methods,
name,
revision,
rootUrl,
schemas,
servicePath,
title,
version,
versionModule);

for (Schema schema : schemas.values()) {
schema.setParent(thisDocument);
}
for (Method method : methods) {
method.setParent(thisDocument);
}

return thisDocument;
}

/**
Expand All @@ -105,20 +119,17 @@ public Schema dereferenceSchema(Schema schema) {
return schema;
}

private static List<Method> parseMethods(DiscoveryNode root, String path) {
private static List<Method> parseMethods(DiscoveryNode root) {
List<Method> methods = new ArrayList<>();
DiscoveryNode methodsNode = root.getObject("methods");
List<String> resourceNames = methodsNode.getFieldNames();
if (!path.isEmpty()) {
path += ".";
}
for (String name : resourceNames) {
methods.add(Method.from(methodsNode.getObject(name), path + "methods." + name));
methods.add(Method.from(methodsNode.getObject(name), null));
}
DiscoveryNode resourcesNode = root.getObject("resources");
resourceNames = resourcesNode.getFieldNames();
for (String name : resourceNames) {
methods.addAll(parseMethods(resourcesNode.getObject(name), path + "resources." + name));
methods.addAll(parseMethods(resourcesNode.getObject(name)));
}
return methods;
}
Expand All @@ -127,11 +138,23 @@ private static Map<String, Schema> parseSchemas(DiscoveryNode root) {
Map<String, Schema> schemas = new HashMap<>();
DiscoveryNode schemasNode = root.getObject("schemas");
for (String name : schemasNode.getFieldNames()) {
schemas.put(name, Schema.from(schemasNode.getObject(name), "schemas." + name));
schemas.put(name, Schema.from(schemasNode.getObject(name), null));
}
return schemas;
}

/** @return the parent Node that contains this node. */
@JsonIgnore @Nullable private Node parent;

public Node parent() {
return parent;
}

// Package private for use by other Node objects.
void setParent(Node parent) {
this.parent = parent;
}

/** @return the auth instructions URL. */
@JsonProperty("authInstructionsUrl")
public abstract String authInstructionsUrl();
Expand All @@ -152,6 +175,11 @@ private static Map<String, Schema> parseSchemas(DiscoveryNode root) {
@JsonProperty("discoveryDocUrl")
public abstract String discoveryDocUrl();

/** @return the ID of the Discovery document for the API. */
@Override
@JsonProperty("id")
public abstract String id();

/** @return the list of all methods. */
@JsonProperty("methods")
public abstract List<Method> methods();
Expand Down
61 changes: 40 additions & 21 deletions src/main/java/com/google/api/codegen/discovery/Method.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@
* <p>Note that this class is not necessarily a 1-1 mapping of the official specification.
*/
@AutoValue
public abstract class Method implements Comparable<Method> {
public abstract class Method implements Comparable<Method>, Node {

/**
* Returns a method constructed from root.
*
* @param root the root node to parse.
* @param path the full path to this node (ex: "resources.foo.methods.bar").
* @return a method.
*/
public static Method from(DiscoveryNode root, String path) {
public static Method from(DiscoveryNode root, Node parent) {
String description = root.getString("description");
String httpMethod = root.getString("httpMethod");
String id = root.getString("id");
Expand All @@ -49,7 +48,7 @@ public static Method from(DiscoveryNode root, String path) {
DiscoveryNode parametersNode = root.getObject("parameters");
HashMap<String, Schema> parameters = new HashMap<>();
for (String name : root.getObject("parameters").getFieldNames()) {
Schema schema = Schema.from(parametersNode.getObject(name), path + ".parameters." + name);
Schema schema = Schema.from(parametersNode.getObject(name), null);
// TODO: Remove these checks once we're sure that parameters can't be objects/arrays.
// This is based on the assumption that these types can't be serialized as a query or path parameter.
Preconditions.checkState(schema.type() != Schema.Type.ANY);
Expand All @@ -58,11 +57,11 @@ public static Method from(DiscoveryNode root, String path) {
parameters.put(name, schema);
}

Schema request = Schema.from(root.getObject("request"), path + ".request");
Schema request = Schema.from(root.getObject("request"), null);
if (request.reference().isEmpty()) {
request = null;
}
Schema response = Schema.from(root.getObject("response"), path + ".response");
Schema response = Schema.from(root.getObject("response"), null);
if (response.reference().isEmpty()) {
response = null;
}
Expand All @@ -73,25 +72,48 @@ public static Method from(DiscoveryNode root, String path) {
boolean supportsMediaDownload = root.getBoolean("supportsMediaDownload");
boolean supportsMediaUpload = root.getBoolean("supportsMediaUpload");

return new AutoValue_Method(
description,
httpMethod,
id,
parameterOrder,
parameters,
path,
request,
response,
scopes,
supportsMediaDownload,
supportsMediaUpload);
Method thisMethod =
new AutoValue_Method(
description,
httpMethod,
id,
parameterOrder,
parameters,
request,
response,
scopes,
supportsMediaDownload,
supportsMediaUpload);

thisMethod.parent = parent;
if (request != null) {
request.setParent(thisMethod);
}
if (response != null) {
response.setParent(thisMethod);
}
for (Schema schema : parameters.values()) {
schema.setParent(thisMethod);
}
return thisMethod;
}

@Override
public int compareTo(Method other) {
return id().compareTo(other.id());
}

/** @return the parent Node. */
private Node parent;

void setParent(Node parent) {
this.parent = parent;
}

public Node parent() {
return parent;
}

/** @return the description. */
public abstract String description();

Expand All @@ -107,9 +129,6 @@ public int compareTo(Method other) {
/** @return the map of parameter names to schemas. */
public abstract Map<String, Schema> parameters();

/** @return the fully qualified path to this method. */
public abstract String path();

/** @return the request schema, or null if none. */
@Nullable
public abstract Schema request();
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/com/google/api/codegen/discovery/Node.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* Copyright 2017 Google Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.api.codegen.discovery;

import javax.annotation.Nullable;

/** Represents a node in a tree of nodes. */
public interface Node {
/** @return the ID of this node. */
@Nullable
String id();

/** @return the immediate parent of this node. */
@Nullable
Node parent();
}
Loading

1 comment on commit f50074f

@andreamlin
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Document, Method, and Schema are all created from DiscoveryNodes, which should be part of a JSON tree structure. This commit unites Document, Method, and Schema under one Node interface, which exposes a method to get the parent of a node.

The ability to get the parent of a node is useful in the context it provides when operating on a single Node; ex. when transforming a Method, it would be easy to fetch the name "pubsub" from the parent Document node.

Please sign in to comment.