Skip to content

Commit

Permalink
Update custom filter example and add CI
Browse files Browse the repository at this point in the history
Update the custom filter example to the new API surface, and also setup
CI so that it ensures that it always compiles against dev Quilkin, and
therefore will stay in sync going forward.

Next I want to block out pieces of code and inject them into our docs
via mdbook when updating
https://googleforgames.github.io/quilkin/main/book/filters/writing_custom_filters.html
as well, rather than have them copy pasted.

Work on googleforgames#373
  • Loading branch information
markmandel committed Sep 2, 2021
1 parent d2b0f7f commit cb6afde
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
12 changes: 11 additions & 1 deletion build/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ version:
@echo $(package_version)

# Run all tests
test: ensure-build-image
test: ensure-build-image test-quilkin test-examples

# test only the quilkin crate
test-quilkin: ensure-build-image
docker run --rm $(common_rust_args) \
--entrypoint=cargo $(BUILD_IMAGE_TAG) deny check
docker run --rm $(common_rust_args) \
Expand All @@ -57,6 +60,13 @@ test: ensure-build-image
docker run --rm $(common_rust_args) \
--entrypoint=cargo $(BUILD_IMAGE_TAG) test

# Run tests against the examples
test-examples: ensure-build-image
docker run --rm $(common_rust_args) -w /workspace/examples/quilkin-filter-example \
--entrypoint=cargo $(BUILD_IMAGE_TAG) clippy --tests -- -D warnings
docker run --rm $(common_rust_args) -w /workspace/examples/quilkin-filter-example \
--entrypoint=cargo $(BUILD_IMAGE_TAG) fmt -- --check

# Build all binaries, images and related artifacts
build: binary-archive build-image

Expand Down
14 changes: 9 additions & 5 deletions examples/quilkin-filter-example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@ repository = "https://github.com/googleforgames/quilkin"
edition = "2018"

[dependencies]
quilkin = "0.1.0"
tokio = { version = "1", features = ["full"]}
prost = "0.7"
prost-types = "0.7"
# If lifting this example, you will want to be explicit abou the Quilkin version, e.g.
# quilkin = "0.2.0"
quilkin = { path = "../../" }
tokio = { version = "1", features = ["full"] }
prost = "0.8"
prost-types = "0.8"
serde = "1.0"
serde_yaml = "0.8"
bytes = "1.0"

[build-dependencies]
prost-build = "0.7"
prost-build = "0.8"

[workspace]
33 changes: 19 additions & 14 deletions examples/quilkin-filter-example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,27 @@
* limitations under the License.
*/

use greet::Greet as ProtoGreet;
use quilkin::filters::prelude::*;

use bytes::Bytes;
use serde::{Deserialize, Serialize};
use std::convert::TryFrom;

#[derive(Serialize, Deserialize, Debug)]
struct Config {
greeting: String,
}

impl TryFrom<ProtoGreet> for Config {
type Error = ConvertProtoConfigError;

fn try_from(p: ProtoGreet) -> Result<Self, Self::Error> {
Ok(Config {
greeting: p.greeting,
})
}
}

mod greet {
include!(concat!(env!("OUT_DIR"), "/greet.rs"));
}
Expand Down Expand Up @@ -55,22 +66,16 @@ impl FilterFactory for GreetFilterFactory {
NAME
}
fn create_filter(&self, args: CreateFilterArgs) -> Result<Box<dyn Filter>, Error> {
let greeting = match args.config.unwrap() {
ConfigType::Static(config) => {
serde_yaml::from_str::<Config>(serde_yaml::to_string(config).unwrap().as_str())
.unwrap()
.greeting
}
ConfigType::Dynamic(config) => {
let config: greet::Greet = prost::Message::decode(Bytes::from(config.value)).unwrap();
config.greeting
}
};
Ok(Box::new(Greet(greeting)))
let greeting = self
.require_config(args.config)?
.deserialize::<Config, ProtoGreet>(self.name())?;
Ok(Box::new(Greet(greeting.greeting)))
}
}

#[tokio::main]
async fn main() {
quilkin::runner::run(vec![self::factory()].into_iter()).await.unwrap();
quilkin::run(vec![self::factory()].into_iter())
.await
.unwrap();
}

0 comments on commit cb6afde

Please sign in to comment.