Skip to content

Commit

Permalink
Merge pull request #6 from NatLabs/renaming
Browse files Browse the repository at this point in the history
[feat] add new options object to support field key renaming
  • Loading branch information
tomijaga committed Jul 16, 2023
2 parents fbb41c1 + 1268f13 commit 1f39aff
Show file tree
Hide file tree
Showing 66 changed files with 1,016 additions and 219 deletions.
29 changes: 16 additions & 13 deletions .github/workflows/makefile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,31 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 14
- uses: aviate-labs/setup-dfx@v0.2.3
node-version: 18
- uses: aviate-labs/setup-dfx@v0.2.5
with:
vessel-version: 0.6.3
dfx-version: 0.12.1
dfx-version: 0.14.1

- name: install wasmtime
run: |
curl https://wasmtime.dev/install.sh -sSf | bash
echo "$HOME/.wasmtime/bin" >> $GITHUB_PATH
- name: Install mocv
run: |
npm --yes -g i mocv
- name: Select mocv version
run: mocv use 0.9.4

- name: install mops
run: |
npm --yes -g i ic-mops
mops i
mops sources
- name: install wasmtime
run: |
curl https://wasmtime.dev/install.sh -sSf | bash
echo "$HOME/.wasmtime/bin" >> $GITHUB_PATH
- name: Detect Warnings
run: make no-warn

- name: Run Tests
run: make compile-tests

# - name: Generate Docs
# run: make docs
run: mops test
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@
# frontend code
node_modules/
dist/

160 changes: 160 additions & 0 deletions compile-tests.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
#!/usr/bin/env zx

import fs, { createReadStream, existsSync, statSync } from "fs";
import events from "events";
import { readFile, stat } from "fs/promises";
import readline from "readline";
import { spawn } from "child_process";
import path from "path";
// import { chalk } from "zx";
// import 'zx/globals'

Array.prototype.last = function () {
return this[this.length - 1];
};

const getImports = async (file) => {
let rl = readline.createInterface({
input: fs.createReadStream(file),
crlfDelay: Infinity,
});

let import_paths = [];

rl.on("line", (line) => {
if (line.startsWith("import")) {
let path = line.split(" ").last();
path = path.slice(1, path.length - 2);
import_paths.push(path);
} else if (line.length !== 0 && !line.startsWith("//")) {
rl.close();
rl.removeAllListeners();
}
});

await events.once(rl, "close");

return import_paths;
};

const wasm_path = (file) => {
let segments = file.split("/");
segments.splice(1, 0, ".wasm");
let basename = segments.last().replace(".mo", ".wasm");
segments[segments.length - 1] = basename;
return segments.join("/");
};

let test_files = await glob("./test?(s)/**/*.(test|Test).mo");

let moc = (await $`dfx cache show`).stdout.toString().trim() + "/moc";
let mops_sources = (await $`mops sources`).stdout.toString().split("\n");

let packages = {};
let package_args = [];


for (const source of mops_sources) {
let segments = source.split(" ");
package_args.push(...segments);
packages[segments[1]] = segments[2];
}

const compile_motoko = async (src, dest) => {
await $`${moc} ${package_args} -wasi-system-api ${src} -o ${dest}`;
};

if (test_files.length) {
let wasm_dir = test_files[0].split("/")[0] + "/.wasm";
await $`mkdir -p ${wasm_dir}`;
}

const last_modified_cache = {};

const is_recently_modified = (file, time) => {
let cached_mtime = last_modified_cache[file];

if (cached_mtime) {
return cached_mtime > time;
}

let file_mtime = statSync(file).mtimeMs;
last_modified_cache[file] = file_mtime;

return file_mtime > time;
};

const is_dep_tree_recently_modified = async (file, wasm_mtime, visited) => {
let modified = is_recently_modified(file, wasm_mtime);

if (modified) {
return true;
}

let imports = await getImports(file);

// console.log({file, imports})

for (let imp_path of imports) {
if (imp_path.startsWith("mo:")) {
let pkg = imp_path.slice(3).split("/")[0];
let pkg_path = packages[pkg];

pkg_path = (await glob(pkg_path + "/**/*.mo"))[0];

if (pkg_path && !visited.has(pkg_path)) {
visited.add(pkg_path);
modified = is_recently_modified(pkg_path, wasm_mtime);
}

continue;
}

imp_path = path.resolve(path.dirname(file), imp_path);

if (existsSync(imp_path.concat(".mo"))) {
imp_path = imp_path.concat(".mo");
} else {
imp_path = imp_path.concat("/lib.mo");
}

if (visited.has(imp_path)) { continue; }

visited.add(imp_path);

// console.log({imp_path})

if (await is_dep_tree_recently_modified(imp_path, wasm_mtime, visited)) {
return true
};

}

return false;
};

const compile_test = async (test_file) => {
const wasm_file = wasm_path(test_file);

let should_compile = !existsSync(wasm_file);

if (!should_compile) {
let wasm_mtime = statSync(wasm_file).mtimeMs;
should_compile = await is_dep_tree_recently_modified(test_file, wasm_mtime, new Set());
}

if (should_compile) {
console.log(`Compiling ${test_file}`);
await compile_motoko(test_file, wasm_file);
}

return wasm_file;
}

const wasm_files = await Promise.all(
test_files.map(compile_test)
);

for (const wasm_file of wasm_files) {
await $`wasmtime ${wasm_file}`;
}
1 change: 0 additions & 1 deletion dfx.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"version": 1,
"dfx": "0.12.1",
"defaults": {
"build": {
"packtool": "mops sources",
Expand Down
25 changes: 25 additions & 0 deletions docs/Candid/Decoder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Candid/Decoder

## Type `Options`
``` motoko no-repl
type Options = { renameKeys : [(Text, Text)] }
```


## Function `decode`
``` motoko no-repl
func decode(blob : Blob, record_keys : [Text], options : ?Options) : [Candid]
```

Decodes a blob encoded in the candid format into a list of the [Candid](./Types.mo#Candid) type in motoko

### Inputs
- **blob** - A blob encoded in the candid format
**record_keys** - The record keys to use when decoding a record.
**options** - An optional arguement to specify options for decoding.

## Function `fromArgs`
``` motoko no-repl
func fromArgs(args : [Arg], recordKeyMap : TrieMap.TrieMap<Nat32, Text>) : [Candid]
```

19 changes: 19 additions & 0 deletions docs/Candid/Encoder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Candid/Encoder

## Function `encode`
``` motoko no-repl
func encode(candid_values : [Candid]) : Blob
```


## Function `encodeOne`
``` motoko no-repl
func encodeOne(candid : Candid) : Blob
```


## Function `toArgs`
``` motoko no-repl
func toArgs(candid_values : [Candid]) : [Arg]
```

7 changes: 7 additions & 0 deletions docs/Candid/Parser/Array.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Candid/Parser/Array

## Function `arrayParser`
``` motoko no-repl
func arrayParser(valueParser : () -> Parser<Char, Candid>) : Parser<Char, Candid>
```

7 changes: 7 additions & 0 deletions docs/Candid/Parser/Blob.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Candid/Parser/Blob

## Function `blobParser`
``` motoko no-repl
func blobParser() : Parser<Char, Candid>
```

7 changes: 7 additions & 0 deletions docs/Candid/Parser/Bool.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Candid/Parser/Bool

## Function `boolParser`
``` motoko no-repl
func boolParser() : Parser<Char, Candid>
```

49 changes: 49 additions & 0 deletions docs/Candid/Parser/Common.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Candid/Parser/Common

## Function `ignoreSpace`
``` motoko no-repl
func ignoreSpace<A>(parser : P.Parser<Char, A>) : P.Parser<Char, A>
```


## Function `removeUnderscore`
``` motoko no-repl
func removeUnderscore<A>(parser : P.Parser<Char, A>) : P.Parser<Char, List<A>>
```


## Function `any`
``` motoko no-repl
func any<T>() : Parser<T, T>
```


## Function `hexChar`
``` motoko no-repl
func hexChar() : Parser<Char, Char>
```


## Function `consIf`
``` motoko no-repl
func consIf<T, A>(parserA : Parser<T, A>, parserAs : Parser<T, List<A>>, cond : (A, List<A>) -> Bool) : Parser<T, List<A>>
```


## Function `fromHex`
``` motoko no-repl
func fromHex(char : Char) : Nat8
```


## Function `toText`
``` motoko no-repl
func toText(chars : List<Char>) : Text
```


## Function `listToNat`
``` motoko no-repl
func listToNat(digits : List<Char>) : Nat
```

7 changes: 7 additions & 0 deletions docs/Candid/Parser/Float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Candid/Parser/Float

## Function `floatParser`
``` motoko no-repl
func floatParser() : Parser<Char, Candid>
```

13 changes: 13 additions & 0 deletions docs/Candid/Parser/Int.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Candid/Parser/Int

## Function `intParser`
``` motoko no-repl
func intParser() : Parser<Char, Candid>
```


## Function `parseInt`
``` motoko no-repl
func parseInt() : Parser<Char, Int>
```

7 changes: 7 additions & 0 deletions docs/Candid/Parser/IntX.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Candid/Parser/IntX

## Function `intXParser`
``` motoko no-repl
func intXParser() : Parser<Char, Candid>
```

13 changes: 13 additions & 0 deletions docs/Candid/Parser/Nat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Candid/Parser/Nat

## Function `natParser`
``` motoko no-repl
func natParser() : Parser<Char, Candid>
```


## Function `parseNat`
``` motoko no-repl
func parseNat() : Parser<Char, Nat>
```

7 changes: 7 additions & 0 deletions docs/Candid/Parser/NatX.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Candid/Parser/NatX

## Function `natXParser`
``` motoko no-repl
func natXParser() : Parser<Char, Candid>
```

Loading

0 comments on commit 1f39aff

Please sign in to comment.