Skip to content

Commit

Permalink
test(googletest/rust): generate the type golden crate
Browse files Browse the repository at this point in the history
  • Loading branch information
coryan committed Nov 13, 2024
1 parent d1c6e5c commit 0208e22
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 25 deletions.
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ members = [
"types",
"gax",
"generator/testdata/rust/openapi/golden",
"generator/testdata/rust/gclient/golden/type",
"generator/testdata/rust/gclient/golden/secretmanager",
]
67 changes: 42 additions & 25 deletions generator/cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,38 +70,55 @@ func TestRustFromOpenAPI(t *testing.T) {
func TestRustFromProtobuf(t *testing.T) {
const (
projectRoot = ".."
outDir = "testdata/rust/gclient/golden/secretmanager"
outDir = "testdata/rust/gclient/golden"
)

popts := &genclient.ParserOptions{
Source: "../testdata/rust/gclient/protos",
Options: map[string]string{
"googleapis-root": "../testdata/googleapis",
"input-root": "../testdata",
},
type Config struct {
Source string
Name string
}

copts := &genclient.CodecOptions{
Language: "rust",
ProjectRoot: projectRoot,
OutDir: outDir,
TemplateDir: "../templates",
Options: map[string]string{
"package-name-override": "secretmanager-golden-gclient",
"package:gax_placeholder": "package=types,path=../../../../../../types,source=google.protobuf",
"package:gax": "package=gax,path=../../../../../../gax",
configs := []Config{
{
Source: "../testdata/rust/gclient/protos",
Name: "secretmanager",
},
{
Source: "../testdata/googleapis/google/type",
Name: "type",
},
}
err := Generate("protobuf", popts, copts)
if err != nil {
t.Fatal(err)
}

cmd := exec.Command("cargo", "fmt", "--manifest-path", path.Join(projectRoot, outDir, "Cargo.toml"))
if output, err := cmd.CombinedOutput(); err != nil {
if ee := (*exec.ExitError)(nil); errors.As(err, &ee) && len(ee.Stderr) > 0 {
t.Fatalf("%v: %v\n%s", cmd, err, ee.Stderr)
for _, config := range configs {
popts := &genclient.ParserOptions{
Source: config.Source,
Options: map[string]string{
"googleapis-root": "../testdata/googleapis",
"input-root": "../testdata",
},
}
copts := &genclient.CodecOptions{
Language: "rust",
ProjectRoot: projectRoot,
OutDir: path.Join(outDir, config.Name),
TemplateDir: "../templates",
Options: map[string]string{
"package-name-override": config.Name + "-golden-gclient",
"package:gax_placeholder": "package=types,path=../../../../../../types,source=google.protobuf",
"package:gax": "package=gax,path=../../../../../../gax",
},
}
err := Generate("protobuf", popts, copts)
if err != nil {
t.Fatal(err)
}

cmd := exec.Command("cargo", "fmt", "--manifest-path", path.Join(projectRoot, outDir, config.Name, "Cargo.toml"))
if output, err := cmd.CombinedOutput(); err != nil {
if ee := (*exec.ExitError)(nil); errors.As(err, &ee) && len(ee.Stderr) > 0 {
t.Fatalf("%v: %v\n%s", cmd, err, ee.Stderr)
}
t.Fatalf("%v: %v\n%s", cmd, err, output)
}
t.Fatalf("%v: %v\n%s", cmd, err, output)
}
}
14 changes: 14 additions & 0 deletions generator/testdata/rust/gclient/golden/type/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "type-golden-gclient"
version = "0.1.0"
edition = "2021"

[dependencies]
serde = { version = "1.0.214", features = ["serde_derive"] }
serde_with = "3.11.0"
serde_json = "1.0.132"
time = { version = "0.3.36", features = ["formatting", "parsing"] }
reqwest = { version = "0.12.9", features = ["json"] }
bytes = { version = "1.8.0", features = ["serde"] }
gax = { path = "../../../../../../gax", package = "gax" }
gax_placeholder = { path = "../../../../../../types", package = "types" }
3 changes: 3 additions & 0 deletions generator/testdata/rust/gclient/golden/type/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# - Rust Client Library


29 changes: 29 additions & 0 deletions generator/testdata/rust/gclient/golden/type/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#![allow(dead_code)]

use std::sync::Arc;

pub mod model;

#[derive(Clone, Debug)]
pub struct Client {
inner: Arc<ClientRef>,
}

#[derive(Debug)]
struct ClientRef {
http_client: reqwest::Client,
token: String,
}

impl Client {
pub fn new(tok: String) -> Self {
let client = reqwest::Client::builder().build().unwrap();
let inner = ClientRef {
http_client: client,
token: tok,
};
Self {
inner: Arc::new(inner),
}
}
}
54 changes: 54 additions & 0 deletions generator/testdata/rust/gclient/golden/type/src/model.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#![allow(dead_code)]

/// Represents a textual expression in the Common Expression Language (CEL)
/// syntax. CEL is a C-like expression language. The syntax and semantics of CEL
/// are documented at https://github.com/google/cel-spec.
///
/// Example (Comparison):
///
/// title: "Summary size limit"
/// description: "Determines if a summary is less than 100 chars"
/// expression: "document.summary.size() < 100"
///
/// Example (Equality):
///
/// title: "Requestor is owner"
/// description: "Determines if requestor is the document owner"
/// expression: "document.owner == request.auth.claims.email"
///
/// Example (Logic):
///
/// title: "Public documents"
/// description: "Determine whether the document should be publicly visible"
/// expression: "document.type != 'private' && document.type != 'internal'"
///
/// Example (Data Manipulation):
///
/// title: "Notification string"
/// description: "Create a notification string with a timestamp."
/// expression: "'New message received at ' + string(document.create_time)"
///
/// The exact variables and functions that may be referenced within an expression
/// are determined by the service that evaluates it. See the service
/// documentation for additional information.
#[derive(Clone, Debug, Default, PartialEq, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Expr {
/// Textual representation of an expression in Common Expression Language
/// syntax.
pub expression: String,

/// Optional. Title for the expression, i.e. a short string describing
/// its purpose. This can be used e.g. in UIs which allow to enter the
/// expression.
pub title: String,

/// Optional. Description of the expression. This is a longer text which
/// describes the expression, e.g. when hovered over it in a UI.
pub description: String,

/// Optional. String indicating the location of the expression for error
/// reporting, e.g. a file name and a position in the file.
pub location: String,
}

0 comments on commit 0208e22

Please sign in to comment.