Skip to content

Commit 6d9c754

Browse files
committed
add stringify
1 parent 8ec6bbc commit 6d9c754

File tree

5 files changed

+177
-13
lines changed

5 files changed

+177
-13
lines changed

.github/workflows/stable_check.yml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: check
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
9+
jobs:
10+
check:
11+
strategy:
12+
fail-fast: false
13+
runs-on: ubuntu-latest
14+
continue-on-error: false
15+
steps:
16+
- uses: actions/checkout@v4
17+
18+
- name: install
19+
run: |
20+
curl -fsSL https://cli.moonbitlang.com/install/unix.sh | bash
21+
echo "$HOME/.moon/bin" >> $GITHUB_PATH
22+
23+
- name: moon version
24+
run: |
25+
moon version --all
26+
27+
- name: install module dependencies
28+
run: |
29+
git submodule init
30+
git submodule update
31+
moon update
32+
moon install
33+
34+
- name: moon check
35+
run: moon check
36+
37+
- name: format diff
38+
run: |
39+
moon fmt
40+
git diff --exit-code
41+
42+
- name: info check
43+
run: |
44+
moon info
45+
git diff --exit-code
46+
47+
- name: Set ulimit
48+
run: |
49+
ulimit -s 8176
50+
51+
- name: moon test
52+
run: |
53+
moon test --target all

jsondiff.mbt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ struct JsonDiff {
99
/// The JSON structural difference of two JSON files.
1010
/// If None: the two JSON files are identical.
1111
diff : JsonEdit?
12-
} derive(Show)
12+
}
1313

1414
///|
1515
priv struct BestMatch {
@@ -44,16 +44,17 @@ pub fn JsonDiff::diff_string(
4444
json1 : Json,
4545
json2 : Json,
4646
keys_only~ : Bool,
47+
terminal? : Bool = false,
4748
) -> String? {
4849
let jsondiff = JsonDiff::diff(json1, json2, keys_only~)
49-
jsondiff.stringify()
50+
jsondiff.stringify(terminal~)
5051
}
5152

5253
///|
53-
fn JsonDiff::stringify(self : JsonDiff) -> String? {
54+
fn JsonDiff::stringify(self : JsonDiff, terminal~ : Bool) -> String? {
5455
match self.diff {
5556
None => None
56-
Some(value) => ...
57+
Some(value) => Some(value.stringify(terminal~))
5758
}
5859
}
5960

pkg.generated.mbti

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@
22
package "moonbit-community/jsondiff"
33

44
// Values
5-
pub fn calculate_ratio(Int, Int) -> Double
65

76
// Errors
87

98
// Types and methods
10-
pub struct JsonDiff {
11-
score : Double
12-
diff : Json?
13-
}
9+
type JsonArrayEdit
10+
pub impl Eq for JsonArrayEdit
11+
pub impl Show for JsonArrayEdit
12+
13+
type JsonDiff
1414
pub fn JsonDiff::diff(Json, Json, keys_only? : Bool) -> Self
15-
pub fn JsonDiff::diff_string(Json, Json, keys_only~ : Bool) -> String?
16-
pub impl Show for JsonDiff
15+
pub fn JsonDiff::diff_string(Json, Json, keys_only~ : Bool, terminal? : Bool) -> String?
16+
17+
type JsonEdit
18+
pub impl Eq for JsonEdit
19+
pub impl Show for JsonEdit
1720

1821
// Type aliases
1922

sequence_matcher.mbt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ priv enum OpTag {
1616
Insert
1717
Delete
1818
Replace
19-
} derive(Eq, Show)
19+
} derive(Eq)
2020

2121
///|
2222
priv struct OpCode {
@@ -25,7 +25,7 @@ priv struct OpCode {
2525
first_end : Int
2626
second_start : Int
2727
second_end : Int
28-
} derive(Show)
28+
}
2929

3030
///|
3131
fn OpCode::new(

stringify.mbt

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,108 @@
1+
///|
2+
fn JsonEdit::stringify(self : JsonEdit, terminal~ : Bool) -> String {
3+
output_colorful_text_for_terminal.val = terminal
4+
let output = Array::new()
5+
substringify(None, self, output, " ", 0)
6+
return output.join("\n")
7+
}
18

9+
///|
10+
fn json_substringify(
11+
key : String?,
12+
json : Json,
13+
output : Array[String],
14+
tag : String,
15+
indent : Int,
16+
) -> Unit {
17+
let prefix = if key is Some(key) { "\{key}: " } else { "" }
18+
let subindent = indent + 2
19+
match json {
20+
Object(object) => {
21+
output.push("\{String::make(indent, ' ')}\{prefix}{")
22+
for subkey, subvalue in object.iterator2() {
23+
json_substringify(Some(subkey), subvalue, output, tag, subindent)
24+
}
25+
output.push_line(tag, "\{String::make(indent, ' ')}}")
26+
}
27+
Array(arr) => {
28+
output.push_line(tag, "\{String::make(indent, ' ')}\{prefix}[")
29+
for subvalue in arr {
30+
json_substringify(None, subvalue, output, tag, subindent)
31+
}
32+
}
33+
json =>
34+
output.push_line(
35+
tag,
36+
String::make(indent, ' ') + prefix + json.to_string(),
37+
)
38+
}
39+
}
40+
41+
///|
42+
fn substringify(
43+
key : String?,
44+
jsonedit : JsonEdit,
45+
output : Array[String],
46+
tag : String,
47+
indent : Int,
48+
) -> Unit {
49+
let prefix = if key is Some(key) { "\{key}: " } else { "" }
50+
let subindent = indent + 2
51+
match jsonedit {
52+
Replace(old~, new~) => {
53+
json_substringify(key, old, output, "-", indent)
54+
json_substringify(key, new, output, "-", indent)
55+
}
56+
Array(edits) => {
57+
output.push_line(tag, "\{String::make(indent, ' ')}\{prefix}[")
58+
for edit in edits {
59+
match edit {
60+
None => output.push_line(tag, "\{String::make(subindent, ' ')}...")
61+
Some(Modification(change)) =>
62+
substringify(None, change, output, " ", subindent)
63+
Some(NoChange(json)) =>
64+
json_substringify(None, json, output, " ", subindent)
65+
Some(Delete(json)) =>
66+
json_substringify(None, json, output, "-", subindent)
67+
Some(Insert(json)) =>
68+
json_substringify(None, json, output, "+", subindent)
69+
}
70+
}
71+
output.push_line(tag, "\{String::make(indent, ' ')}]")
72+
}
73+
Object(diff, added~, deleted~) => {
74+
output.push_line(tag, "\{String::make(indent, ' ')}\{prefix}{")
75+
for key, value in deleted.iterator2() {
76+
json_substringify(Some(key), value, output, "-", subindent)
77+
}
78+
for key, value in added.iterator2() {
79+
json_substringify(Some(key), value, output, "+", subindent)
80+
}
81+
for key, value in diff.iterator2() {
82+
substringify(Some(key), value, output, " ", subindent)
83+
}
84+
output.push_line(tag, "\{String::make(indent, ' ')}}")
85+
}
86+
}
87+
}
88+
89+
///|
90+
let output_colorful_text_for_terminal : Ref[Bool] = Ref::new(false)
91+
92+
///|
93+
fn Array::push_line(self : Array[String], tag : String, line : String) -> Unit {
94+
let tagged_line = "\{tag}\{line}"
95+
if output_colorful_text_for_terminal.val {
96+
match tag {
97+
"+" =>
98+
// green
99+
self.push("\u{1b}[32m\{tag}\{line}\u{1b}[0m")
100+
"-" =>
101+
// red
102+
self.push("\u{1b}[31m\{tag}\{line}\u{1b}[0m")
103+
_ => self.push(tagged_line)
104+
}
105+
} else {
106+
self.push(tagged_line)
107+
}
108+
}

0 commit comments

Comments
 (0)