Skip to content

Commit

Permalink
fixup! WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ezzatron committed Mar 24, 2024
1 parent 1bacc76 commit 8428b41
Show file tree
Hide file tree
Showing 37 changed files with 403 additions and 364 deletions.
34 changes: 21 additions & 13 deletions ENVIRONMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export EARTH_ATOM_COUNT=-123456 # negative
```

```sh
export EARTH_ATOM_COUNT=0x1E240 # hexadecimal
export EARTH_ATOM_COUNT=0x1e240 # hexadecimal
```

```sh
Expand All @@ -104,6 +104,16 @@ that takes **ISO 8601 duration** values with these constraints:

- Must be >= PT0.1S and <= PT10S

### Example values

```sh
export GRPC_TIMEOUT=PT0.3S # 300 milliseconds
```

```sh
export GRPC_TIMEOUT=PT5S # 5 seconds
```

## `LOG_LEVEL`

_The minimum log level to record_
Expand Down Expand Up @@ -167,6 +177,12 @@ that takes **string** values with these constraints:

- Must have a minimum length of 30

### Example values

```sh
export READ_DSN='host=localhost dbname=readmodels user=projector' # local database
```

## `REDIS_PRIMARY_SERVICE_HOST`

_Kubernetes `redis-primary` service host_
Expand Down Expand Up @@ -223,7 +239,7 @@ export SAMPLE_RATIO=1.23456e+2 # exponential
```

```sh
export SAMPLE_RATIO=0x1E240 # hexadecimal
export SAMPLE_RATIO=0x1e240 # hexadecimal
```

```sh
Expand Down Expand Up @@ -259,21 +275,13 @@ that takes **integer** values with these constraints:
### Example values

```sh
export WEIGHT=123456 # positive
```

```sh
export WEIGHT=1.23456e+5 # exponential
```

```sh
export WEIGHT=0x1E240 # hexadecimal
export WEIGHT=1 # lowest
```

```sh
export WEIGHT=0o361100 # octal
export WEIGHT=100 # high
```

```sh
export WEIGHT=0b11110001001000000 # binary
export WEIGHT=1000 # very high
```
24 changes: 13 additions & 11 deletions src/declaration/big-integer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ import {
type ExactOptions,
} from "../declaration.js";
import { registerVariable } from "../environment.js";
import { normalize } from "../error.js";
import { SpecError, normalize } from "../error.js";
import {
marshalExamples,
resolveExamples,
type DeclarationExampleOptions,
type MarshalledExample,
type Example,
} from "../example.js";
import { resolve } from "../maybe.js";
import { ScalarSchema, createScalar, toString } from "../schema.js";
import { SpecError } from "../variable.js";

export type Options = DeclarationOptions<bigint> &
DeclarationExampleOptions<bigint> &
Expand All @@ -41,7 +40,7 @@ export function bigInteger<O extends Options>(
default: def,
isSensitive,
schema,
examples: examples ? marshalExamples(schema, examples) : buildExamples(),
examples: resolveExamples(name, schema, buildExamples, examples),
});

return {
Expand Down Expand Up @@ -73,26 +72,29 @@ function createSchema(name: string, options: Options): ScalarSchema<bigint> {
return createScalar("big integer", toString, unmarshal, constraints);
}

function buildExamples(): MarshalledExample[] {
function buildExamples(): Example<bigint>[] {
return [
{
value: "123456",
value: 123456n,
description: "positive",
},
{
value: "-123456",
value: -123456n,
description: "negative",
},
{
value: "0x1E240",
value: 123456n,
as: "0x1e240",
description: "hexadecimal",
},
{
value: "0o361100",
value: 123456n,
as: "0o361100",
description: "octal",
},
{
value: "0b11110001001000000",
value: 123456n,
as: "0b11110001001000000",
description: "binary",
},
];
Expand Down
23 changes: 11 additions & 12 deletions src/declaration/binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ import {
type ExactOptions,
} from "../declaration.js";
import { registerVariable } from "../environment.js";
import { normalize } from "../error.js";
import { SpecError, normalize } from "../error.js";
import {
marshalExamples,
resolveExamples,
type DeclarationExampleOptions,
type MarshalledExample,
type Example,
} from "../example.js";
import { resolve } from "../maybe.js";
import { ScalarSchema, createScalar } from "../schema.js";
import { SpecError } from "../variable.js";

const PATTERNS: Partial<Record<BufferEncoding, RegExp>> = {
base64: /^[A-Za-z0-9+/]*={0,2}$/,
Expand Down Expand Up @@ -53,9 +52,12 @@ export function binary<O extends Options>(
default: def,
isSensitive,
schema,
examples: examples
? marshalExamples(schema, examples)
: buildExamples(encoding, schema),
examples: resolveExamples(
name,
schema,
() => buildExamples(encoding),
examples,
),
});

return {
Expand Down Expand Up @@ -109,13 +111,10 @@ function createUnmarshal(
};
}

function buildExamples(
encoding: BufferEncoding,
schema: ScalarSchema<Buffer>,
): MarshalledExample[] {
function buildExamples(encoding: BufferEncoding): Example<Buffer>[] {
return [
{
value: schema.marshal(Buffer.from("conquistador", "utf-8")),
value: Buffer.from("conquistador", "utf-8"),
description: `${encoding} encoded string`,
},
];
Expand Down
20 changes: 12 additions & 8 deletions src/declaration/boolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import {
type ExactOptions,
} from "../declaration.js";
import { registerVariable } from "../environment.js";
import { SpecError } from "../error.js";
import {
marshalExamples,
resolveExamples,
type DeclarationExampleOptions,
type MarshalledExample,
type Example,
} from "../example.js";
import { resolve } from "../maybe.js";
import { EnumSchema, InvalidEnumError, createEnum } from "../schema.js";
import { SpecError } from "../variable.js";

export type Options = DeclarationOptions<boolean> &
DeclarationExampleOptions<boolean> & {
Expand Down Expand Up @@ -43,9 +43,12 @@ export function boolean<O extends Options>(
default: def,
isSensitive,
schema,
examples: examples
? marshalExamples(schema, examples)
: buildExamples(literals),
examples: resolveExamples(
name,
schema,
() => buildExamples(literals),
examples,
),
});

return {
Expand Down Expand Up @@ -90,9 +93,10 @@ function findLiteral(
throw new MissingLiteralError(name, native);
}

function buildExamples(literals: Literals): MarshalledExample[] {
function buildExamples(literals: Literals): Example<boolean>[] {
return Object.entries(literals).map(([literal, native]) => ({
value: literal,
value: native,
as: literal,
description: String(native),
}));
}
Expand Down
15 changes: 7 additions & 8 deletions src/declaration/duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ import {
type ExactOptions,
} from "../declaration.js";
import { registerVariable } from "../environment.js";
import { normalize } from "../error.js";
import { SpecError, normalize } from "../error.js";
import {
marshalExamples,
resolveExamples,
type DeclarationExampleOptions,
type MarshalledExample,
type Example,
} from "../example.js";
import { resolve } from "../maybe.js";
import { ScalarSchema, createScalar, toString } from "../schema.js";
import { SpecError } from "../variable.js";

const { Duration } = Temporal;
type Duration = Temporal.Duration;
Expand All @@ -45,7 +44,7 @@ export function duration<O extends Options>(
default: def,
isSensitive,
schema,
examples: examples ? marshalExamples(schema, examples) : buildExamples(),
examples: resolveExamples(name, schema, buildExamples, examples),
});

return {
Expand Down Expand Up @@ -77,14 +76,14 @@ function createSchema(name: string, options: Options): ScalarSchema<Duration> {
return createScalar("ISO 8601 duration", toString, unmarshal, constraints);
}

function buildExamples(): MarshalledExample[] {
function buildExamples(): Example<Duration>[] {
return [
{
value: Duration.from({ minutes: 1, seconds: 30 }).toString(),
value: Duration.from({ minutes: 1, seconds: 30 }),
description: "ISO 8601 duration",
},
{
value: Duration.from({ months: 1, days: 15, hours: 12 }).toString(),
value: Duration.from({ months: 1, days: 15, hours: 12 }),
description: "ISO 8601 duration",
},
];
Expand Down
22 changes: 13 additions & 9 deletions src/declaration/enumeration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import {
type ExactOptions,
} from "../declaration.js";
import { registerVariable } from "../environment.js";
import { SpecError } from "../error.js";
import {
marshalExamples,
resolveExamples,
type DeclarationExampleOptions,
type MarshalledExample,
type Example,
} from "../example.js";
import { resolve } from "../maybe.js";
import { EnumSchema, InvalidEnumError, createEnum } from "../schema.js";
import { SpecError } from "../variable.js";

export type Members<T> = Record<string, Member<T>>;

Expand Down Expand Up @@ -41,9 +41,12 @@ export function enumeration<T, O extends Options<T>>(
default: def,
isSensitive,
schema,
examples: examples
? marshalExamples(schema, examples)
: buildExamples(members),
examples: resolveExamples(
name,
schema,
() => buildExamples(members),
examples,
),
});

return {
Expand Down Expand Up @@ -81,9 +84,10 @@ function createSchema<T>(name: string, members: Members<T>): EnumSchema<T> {
return createEnum(schemaMembers, marshal, unmarshal, []);
}

function buildExamples<T>(members: Members<T>): MarshalledExample[] {
return Object.entries(members).map(([literal, { description }]) => ({
value: literal,
function buildExamples<T>(members: Members<T>): Example<T>[] {
return Object.entries(members).map(([literal, { value, description }]) => ({
value,
as: literal,
description,
}));
}
Expand Down
27 changes: 15 additions & 12 deletions src/declaration/integer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ import {
type ExactOptions,
} from "../declaration.js";
import { registerVariable } from "../environment.js";
import { normalize } from "../error.js";
import { SpecError, normalize } from "../error.js";
import {
marshalExamples,
resolveExamples,
type DeclarationExampleOptions,
type MarshalledExample,
type Example,
} from "../example.js";
import { resolve } from "../maybe.js";
import { ScalarSchema, createScalar, toString } from "../schema.js";
import { SpecError } from "../variable.js";

export type Options = DeclarationOptions<number> &
DeclarationExampleOptions<number> &
Expand All @@ -43,7 +42,7 @@ export function integer<O extends Options>(
default: def,
isSensitive,
schema,
examples: examples ? marshalExamples(schema, examples) : buildExamples(),
examples: resolveExamples(name, schema, buildExamples, examples),
});

return {
Expand Down Expand Up @@ -72,30 +71,34 @@ function createSchema(name: string, options: Options): ScalarSchema<number> {
return createScalar("integer", toString, unmarshal, constraints);
}

function buildExamples(): MarshalledExample[] {
function buildExamples(): Example<number>[] {
return [
{
value: "123456",
value: 123456,
description: "positive",
},
{
value: "-123456",
value: -123456,
description: "negative",
},
{
value: "1.23456e+5",
value: 1.23456e5,
as: "1.23456e5",
description: "exponential",
},
{
value: "0x1E240",
value: 0x1e240,
as: "0x1e240",
description: "hexadecimal",
},
{
value: "0o361100",
value: 0o361100,
as: "0o361100",
description: "octal",
},
{
value: "0b11110001001000000",
value: 0b11110001001000000,
as: "0b11110001001000000",
description: "binary",
},
];
Expand Down
Loading

0 comments on commit 8428b41

Please sign in to comment.