Skip to content

Instantly share code, notes, and snippets.

@andreievg
andreievg / narrowingDiscriminatedUnionArray.ts
Last active September 5, 2023 10:37
A method to narrow down an array of discriminated unions
type One = { one: 'one'; t: 'one' }
type Two = { two: 2; t: 'two' }
type OneOrTwo = One | Two
const rows: OneOrTwo[] = []
const getOneOrTwo = <T extends OneOrTwo['t']>(t: T) => {
return rows.find((obj): obj is Extract<OneOrTwo, {t: T}> => obj.t === t)
}
@andreievg
andreievg / bench.rs
Created August 22, 2023 23:31
Rust bench helper
pub fn bench_point(identifier: &str) {
BENCH_POINTS.lock().unwrap().push(BenchPoint {
identifier: identifier.to_string(),
time: Utc::now(),
})
}
pub static BENCH_POINTS: Mutex<Vec<BenchPoint>> = Mutex::new(Vec::new());
// ideally this would be a macro to capture file and location
@andreievg
andreievg / commit_and_compare.sh
Created July 18, 2023 11:32
Bash script to create a branch with current changes and compare to current branch for github
#!/bin/bash
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
# Replace every character that is not letter or number with dash
COMMIT_MESSAGE="$@"
SANITISED_COMMIT=$(echo $COMMIT_MESSAGE | sed -E 's/[^a-zA-Z0-9]/\-/g')
NEW_BRANCH="$CURRENT_BRANCH($SANITISED_COMMIT)"
git checkout -b $NEW_BRANCH
git add -A
@andreievg
andreievg / usePartialState.ts
Last active July 18, 2023 11:46
Use state wrapper to expose partial state setter
export const usePartialState = <T extends object>(
initial: T
): {
state: T;
setState: Dispatch<SetStateAction<T>>;
setPartialState: (newPartialState: Partial<T>) => void;
} => {
const [state, setState] = useState<T>(initial);
return {
@andreievg
andreievg / serde_enum_with_unknown_variant.rs
Created September 26, 2022 07:18
Rust serde for enum with unknown value in associated string variant
use serde::{Deserialize, Deserializer, Serialize, Serializer};
const FROM: &'static str = r#"[{"value":"unknown"},{"value": "one"}]"#;
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "snake_case")]
pub enum To {
One,
Two,
Three,
@andreievg
andreievg / reset_pg_serial.sql
Created July 28, 2022 03:26
Reset Postgres Sequence/Serial numbers
-- When a field is created as `SERIAL` postgres adds a seqeunce for it, and adds default values for that field
-- For example:
-- CREATE TABLE test_serial (id SERIAL)
-- Will result in:
-- CREATE TABLE IF NOT EXISTS public.test_serial
-- (
-- id integer NOT NULL DEFAULT nextval('test_serial_id_seq'::regclass)
-- )
@andreievg
andreievg / a_methods.4dm
Created July 16, 2022 06:19
4d vscode-like search for methods
// Quick search, open and run method, should be named to appear first in "Execute Method"
// ctrl + r + enter to quickly open method search
$MIN_NUMBER_OF_SEARCH_LETTERS:=3
$INPUT_NAME:="input"
$LIST_NAME:="list"
$event:=FORM Event
Case of
@andreievg
andreievg / schemaspy_postgres_enum.md
Created June 11, 2022 09:10
SchemaSpy Postgres Enum

As per this issue, and own requirements, it would be great for SchemaSpy to show enum variants.

Fix for now:

  • Before running SchemaSpy
  • Manually introspect pg schema
  • Insert enum variants into a comment for the column using enum
  • Make sure to retain column and type comments

Here is example schema: