Skip to content

Commit

Permalink
Reorganize UUID project
Browse files Browse the repository at this point in the history
  • Loading branch information
tgross35 committed Dec 10, 2022
1 parent 855ae60 commit c3639a4
Show file tree
Hide file tree
Showing 9 changed files with 351 additions and 309 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/release-rust.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ jobs:
- build: linux
os: ubuntu-latest
target: x86_64-unknown-linux-gnu
# extension: ''
# Unsuccessful compilation; try on local
# - build: linux-arm
# os: ubuntu-latest
Expand All @@ -33,12 +32,11 @@ jobs:
- build: windows-msvc
os: windows-latest
target: x86_64-pc-windows-msvc
# extension: .exe
env:
CARGO: cargo
TARGET_DIR: ./target
TARGET_FLAGS: ""

MYSQLCLIENT_LIB_DIR: C:\mysql\lib
steps:
# Retreive git files
- uses: actions/checkout@v2
Expand Down Expand Up @@ -141,7 +139,7 @@ jobs:
mkdir "$staging"
# Remove the "unreleased" section from our changelog
perl -0777 -i -pe "s/\n*(<\!-- next-header -->.*## \[Unreleased\].*?\n)(?=\n*^## )//gms" CHANGELOG.md
perl -0777 -i -pe "s/\n*(<\!-- next-header -->.*## \[Unreleased\].*?\n)(?:## |<\!--)//gms" CHANGELOG.md
cp {README.md,LICENSE,CHANGELOG.md} "$staging/"
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/validation-rust.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ jobs:
- build: linux
os: ubuntu-latest
target: x86_64-unknown-linux-musl
# extension: ''
# - build: macos
# os: macos-latest
# target: x86_64-apple-darwin
Expand All @@ -61,8 +60,10 @@ jobs:
os: windows-latest
target: x86_64-pc-windows-msvc

name: "Test on ${{ matrix.name }} (cargo test)"
name: "Test on ${{ matrix.os }} (cargo test)"
runs-on: ${{ matrix.os }}
env:
MYSQLCLIENT_LIB_DIR: C:\mysql\lib
steps:
- uses: actions/checkout@v2
- name: List files
Expand Down
2 changes: 1 addition & 1 deletion udf-jsonify/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ publish = false
crate-type = ["cdylib"]

[dependencies]
udf = { version = "0.4.2", features = ["mock"] }
udf = { version = "0.4.4", features = ["mock"] }
serde_json = "1.0"
2 changes: 1 addition & 1 deletion udf-lipsum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ publish = false
crate-type = ["cdylib"]

[dependencies]
udf = { version = "0.4.2", features = ["mock"] }
udf = { version = "0.4.4", features = ["mock"] }
lipsum = "0.8.2"
2 changes: 1 addition & 1 deletion udf-uuid/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ publish = false
crate-type = ["cdylib"]

[dependencies]
udf = { version = "0.4.2", features = ["mock"] }
udf = { version = "0.4.4", features = ["mock"] }
uuid = { version = "1.2.1", features = ["v1", "v3", "v4", "v5", "fast-rng"] }
mac_address = "1.1.4"
rand = "0.8.5"
120 changes: 120 additions & 0 deletions udf-uuid/src/generate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
use mac_address::get_mac_address;
use udf::prelude::*;
use uuid::Uuid;

use crate::validate_arg_count;

#[derive(Debug)]
struct UuidGenerateV1 {
/// We save the mac address during the `init` call because that won't change.
/// Saves a few ms, maybe
mac: [u8; 6],
}

#[register]
impl BasicUdf for UuidGenerateV1 {
type Returns<'a> = String;

fn init(_cfg: &UdfCfg<Init>, args: &ArgList<Init>) -> Result<Self, String> {
validate_arg_count(args.len(), 0, "uuid_generate_v1")?;

Ok(Self {
mac: get_mac_address()
.ok()
.flatten()
.map(|m| m.bytes())
.unwrap_or([0u8; 6]),
})
}

fn process<'a>(
&'a mut self,
_cfg: &UdfCfg<Process>,
_args: &ArgList<Process>,
_error: Option<NonZeroU8>,
) -> Result<Self::Returns<'a>, ProcessError> {
// Try to get the mac address; just return zeroes if there are any issues
Ok(Uuid::now_v1(&self.mac).as_hyphenated().to_string())
}
}

// /// V1 UUID with specified MAC address
// struct UuidGenerateV1arg;

// #[register]
// impl BasicUdf for UuidGenerateV1arg {
// type Returns<'a> = String;

// fn init(_cfg: &UdfCfg<Init>, args: &ArgList<Init>) -> Result<Self, String> {
// if args.len() != 1 {
// Err(format!(
// "uuid_generate_v1arg takes 1 argument but got {}",
// args.len()
// ))
// } else {
// args.get(0).unwrap().set_type_coercion(SqlType::String);
// Ok(Self)
// }
// }

// fn process<'a>(
// &'a mut self,
// _cfg: &UdfCfg<Process>,
// _args: &ArgList<Process>,
// _error: Option<NonZeroU8>,
// ) -> Result<Self::Returns<'a>, ProcessError> {

// Ok(Uuid::now_v1(&fake_mac).as_hyphenated().to_string())
// }
// }

/// V1 UUID with randomized MAC address
#[derive(Debug)]
struct UuidGenerateV1mc;

#[register]
impl BasicUdf for UuidGenerateV1mc {
type Returns<'a> = String;

fn init(_cfg: &UdfCfg<Init>, args: &ArgList<Init>) -> Result<Self, String> {
validate_arg_count(args.len(), 0, "uuid_generate_v1mc")?;
Ok(Self)
}

fn process<'a>(
&'a mut self,
_cfg: &UdfCfg<Process>,
_args: &ArgList<Process>,
_error: Option<NonZeroU8>,
) -> Result<Self::Returns<'a>, ProcessError> {
let mut fake_mac: [u8; 6] = rand::random();

// magic bits for multicast address
fake_mac[0..=3].copy_from_slice(&[0x01u8, 0x00, 0x5e]);

Ok(Uuid::now_v1(&fake_mac).as_hyphenated().to_string())
}
}

/// V4 (completely random) UUID
#[derive(Debug)]
struct UuidGenerateV4;

#[register]
impl BasicUdf for UuidGenerateV4 {
type Returns<'a> = String;

fn init(_cfg: &UdfCfg<Init>, args: &ArgList<Init>) -> Result<Self, String> {
validate_arg_count(args.len(), 0, "uuid_generate_v4")?;
Ok(Self)
}

fn process<'a>(
&'a mut self,
_cfg: &UdfCfg<Process>,
_args: &ArgList<Process>,
_error: Option<NonZeroU8>,
) -> Result<Self::Returns<'a>, ProcessError> {
Ok(Uuid::new_v4().as_hyphenated().to_string())
}
}
Loading

0 comments on commit c3639a4

Please sign in to comment.