Skip to content

Instantly share code, notes, and snippets.

View Sammons's full-sized avatar

Ben Sammons Sammons

View GitHub Profile
type InferFirewallNames<Builder> =
 Builder extends RootBuilder<infer FirewallNames, infer NodeNames, infer NodesVolumes, infer DockerServiceNames>
 ? FirewallNames
 : never
new RootBuilder().addFirewall('x').addFirewall('y'); // all good
new RootBuilder().addFirewall('x').addFirewall('x'); // no good
addFirewall<
 FirewallName extends string,
 ReturnType extends AddFirewall<this, FirewallName>
 >(firewallName: Exclude<FirewallName, FirewallNames>): ReturnType {
 // … do whatever logic needs to be done
 return this as RootBuilder as ReturnType
 }
type AddFirewall<Builder, FirewallName extends string> =
 Builder extends RootBuilder<infer FirewallNames, infer NodeNames, infer NodesVolumes, infer DockerServiceNames>
 ? RootBuilder<FirewallNames | FirewallName, NodeNames, NodesVolumes, DockerServiceNames>
 : never
class RootBuilder<
FirewallNames extends string = never,
NodeNames extends string = never,
NodesVolumes extends string = never,
DockerServiceNames extends string = never
> {}
// inferred shape is { x: { name: string; }; y: { name: string; } }[]
const results = await builder
.stack('test')
.addFirewall('edge-firewall', firewallBuilder => firewallBuilder.allowInbound('80', ['::']))
.addNode('gateway', // typechecked, no duplicates allowed
nodeBuilder => nodeBuilder
.size.v1cpu2gb()
.withDynamicVolume('edge-statics', {// typechecked, no duplicates allowed
target: '/mnt/edge-statics',
sizeGb: 20
@Sammons
Sammons / possible-types.ts
Created October 4, 2018 02:17
Possible where clause interfaces for typescript querybuilder
db // database interface generated by extracting database schema into typescript
.users.as('u')
.innerJoin.users.as('u2')
.onCol.u.name.trim
.equals.u2.name.trim
// shorthand support for exact matches, in clauses
.whereColumnsOf.u2({
id: [1, 2]
})
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
#include <utility>
#include <tuple>
#include <iostream>
#include <string>
using namespace std;
typedef tuple<string, float> DistributedFloatMatrixElement;
struct DistributedFloatMatrixFile {
@Sammons
Sammons / fuzzy_thinking.js
Created January 10, 2016 00:51
thinking about heuristic scoring for words against strings
var pretend_db = [
'hello guys lets hang out soon',
'hello ben',
'what is up?',
'hey when are you coming to the bay?',
'yo',
'when are we getting together?',
'hig',