-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(sdl): separates yaml parser and factory from SDL (#77)
- Loading branch information
1 parent
5bb5a70
commit b5cade2
Showing
9 changed files
with
335 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,6 @@ | ||
{ | ||
"extends": [ | ||
"@commitlint/config-conventional" | ||
], | ||
"extends": ["@commitlint/config-conventional"], | ||
"rules": { | ||
"scope-enum": [ | ||
2, | ||
"always", | ||
[ | ||
"certificates", | ||
"network", | ||
"wallet", | ||
"api", | ||
"stargate" | ||
] | ||
] | ||
"scope-enum": [2, "always", ["certificates", "network", "wallet", "api", "stargate", "sdl"]] | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { NetworkId } from "../types/network"; | ||
|
||
export const MAINNET_ID: NetworkId = "mainnet"; | ||
export const SANDBOX_ID: NetworkId = "sandbox"; | ||
|
||
export const USDC_IBC_DENOMS: Record<NetworkId, string> = { | ||
[MAINNET_ID]: "ibc/170C677610AC31DF0904FFE09CD3B5C657492170E7E52372E48756B71E56F2F1", | ||
[SANDBOX_ID]: "ibc/12C6A0C374171B595A0A9E18B83FA09D295FB1F2D8C6DAA3AC28683471752D84" | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { NetworkId } from "../types/network"; | ||
import { NetworkVersion, BetaSdl, NetworkBeta2, NetworkBeta3 } from "./types"; | ||
import { SDL } from "./index"; | ||
import { YamlSDLParser } from "./YamlSDLParser"; | ||
|
||
export class SDLFactory { | ||
static create<V extends NetworkVersion>({ data, version, network }: { data: BetaSdl<V>; version: NetworkVersion; network: NetworkId }): SDL<V> { | ||
return new SDL<V>(data, version, network); | ||
} | ||
|
||
static createFromYaml({ data, version, network }: { data: string; version: NetworkBeta2; network: NetworkId }): SDL<NetworkBeta2>; | ||
static createFromYaml({ data, version, network }: { data: string; version: NetworkBeta3; network: NetworkId }): SDL<NetworkBeta3>; | ||
static createFromYaml({ data, version, network }: { data: string; version: NetworkVersion; network: NetworkId }): SDL<NetworkVersion> { | ||
let parsed: BetaSdl<NetworkVersion>; | ||
if (version === "beta2") { | ||
parsed = YamlSDLParser.parse(data, version); | ||
} else { | ||
parsed = YamlSDLParser.parse(data, version); | ||
} | ||
|
||
return this.create({ data: parsed, version, network }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
import { z } from "zod"; | ||
import { BetaSdl, NetworkBeta2, NetworkBeta3, NetworkVersion } from "./types"; | ||
import YAML from "js-yaml"; | ||
|
||
export const v2HTTPOptions = z.object({ | ||
max_body_size: z.number(), | ||
read_timeout: z.number(), | ||
send_timeout: z.number(), | ||
next_tries: z.number(), | ||
next_timeout: z.number(), | ||
next_cases: z.array(z.string()) | ||
}); | ||
|
||
export const v2ExposeTo = z.object({ | ||
service: z.string().optional(), | ||
global: z.boolean().optional(), | ||
http_options: v2HTTPOptions, | ||
ip: z.string() | ||
}); | ||
|
||
export const v2Accept = z.object({ | ||
items: z.array(z.string()).optional() | ||
}); | ||
|
||
export const v2Expose = z.object({ | ||
port: z.number(), | ||
as: z.number(), | ||
proto: z.string().optional(), | ||
to: z.array(v2ExposeTo).optional(), | ||
accept: v2Accept, | ||
http_options: v2HTTPOptions | ||
}); | ||
|
||
export const v2ServiceStorageParams = z.object({ | ||
name: z.string(), | ||
mount: z.string(), | ||
readOnly: z.boolean() | ||
}); | ||
|
||
export const v2ServiceParams = z.object({ | ||
storage: z.record(v2ServiceStorageParams).optional() | ||
}); | ||
|
||
export const v2Dependency = z.object({ | ||
service: z.string() | ||
}); | ||
|
||
export const v2Service = z.object({ | ||
image: z.string(), | ||
command: z.array(z.string()).nullable(), | ||
args: z.array(z.string()).nullable(), | ||
env: z.array(z.string()).nullable(), | ||
expose: z.array(v2Expose), | ||
dependencies: z.array(v2Dependency).optional(), | ||
params: v2ServiceParams.optional() | ||
}); | ||
|
||
export const v2ServiceDeployment = z.object({ | ||
profile: z.string(), | ||
count: z.number() | ||
}); | ||
|
||
export const v2Deployment = z.record(v2ServiceDeployment); | ||
|
||
export const v2Endpoint = z.object({ | ||
kind: z.string() | ||
}); | ||
|
||
export const v2Profiles = z.object({ | ||
compute: z.record( | ||
z.object({ | ||
resources: z.object({ | ||
cpu: z.object({ | ||
units: z.union([z.number(), z.string()]), | ||
attributes: z.record(z.any()).optional() | ||
}), | ||
memory: z.object({ | ||
size: z.string(), | ||
attributes: z.record(z.any()).optional() | ||
}), | ||
storage: z.union([ | ||
z.array( | ||
z.object({ | ||
name: z.string(), | ||
size: z.string(), | ||
attributes: z.record(z.any()) | ||
}) | ||
), | ||
z.object({ | ||
name: z.string(), | ||
size: z.string(), | ||
attributes: z.record(z.any()) | ||
}) | ||
]) | ||
}) | ||
}) | ||
), | ||
placement: z.record( | ||
z.object({ | ||
attributes: z.record(z.string()), | ||
signedBy: z.object({ | ||
allOf: z.array(z.string()).optional(), | ||
anyOf: z.array(z.string()).optional() | ||
}), | ||
pricing: z.record( | ||
z.object({ | ||
denom: z.string(), | ||
value: z.number(), | ||
amount: z.number() | ||
}) | ||
) | ||
}) | ||
) | ||
}); | ||
|
||
const v2Storage = z.object({ | ||
name: z.string(), | ||
size: z.string(), | ||
attributes: z.record(z.any()) | ||
}); | ||
|
||
export const v2ProfileCompute = z.object({ | ||
resources: z.object({ | ||
cpu: z.object({ | ||
units: z.union([z.number(), z.string()]), | ||
attributes: z.record(z.any()).optional() | ||
}), | ||
memory: z.object({ | ||
size: z.string(), | ||
attributes: z.record(z.any()).optional() | ||
}), | ||
storage: z.union([z.array(v2Storage), v2Storage]) | ||
}) | ||
}); | ||
|
||
export const v3ProfileCompute = z.object({ | ||
resources: z.object({ | ||
cpu: v2ProfileCompute.shape.resources.shape.cpu, | ||
memory: v2ProfileCompute.shape.resources.shape.memory, | ||
storage: v2ProfileCompute.shape.resources.shape.storage, | ||
gpu: z.object({ | ||
units: z.union([z.number(), z.string()]), | ||
attributes: z.object({ | ||
vendor: z.record( | ||
z.array( | ||
z.object({ | ||
model: z.string(), | ||
ram: z.string().optional(), | ||
interface: z.string().optional() | ||
}) | ||
) | ||
) | ||
}) | ||
}), | ||
id: z.number() | ||
}) | ||
}); | ||
|
||
export const v3Profiles = z.object({ | ||
compute: z.record(v3ProfileCompute), | ||
placement: v2Profiles.shape.placement | ||
}); | ||
|
||
const Beta2Sdl = z.object({ | ||
services: z.record(v2Service), | ||
deployment: z.record(v2Deployment), | ||
endpoints: z.record(v2Endpoint), | ||
profiles: v2Profiles | ||
}); | ||
|
||
const Beta3Sdl = z.object({ | ||
services: z.record(v2Service), | ||
deployment: z.record(v2Deployment), | ||
endpoints: z.record(v2Endpoint), | ||
profiles: v3Profiles | ||
}); | ||
|
||
export class YamlSDLParser { | ||
static parse(yaml: string, version: NetworkBeta2): BetaSdl<NetworkBeta2>; | ||
static parse(yaml: string, version: NetworkBeta3): BetaSdl<NetworkBeta3>; | ||
static parse(yaml: string, version: NetworkVersion): BetaSdl<NetworkVersion> { | ||
const json = YAML.load(yaml); | ||
|
||
if (version === "beta2") { | ||
return Beta2Sdl.parse(json) as BetaSdl<NetworkBeta2>; | ||
} else if (version === "beta3") { | ||
return Beta3Sdl.parse(json) as BetaSdl<NetworkBeta3>; | ||
} else { | ||
throw new Error("Unsupported version"); | ||
} | ||
} | ||
} |
Oops, something went wrong.