Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita58f6b9

Browse files
authored
Remove Json module (#1066)
* Remove Json module* add helper function optionToNull
1 parent318ac31 commita58f6b9

File tree

10 files changed

+247
-614
lines changed

10 files changed

+247
-614
lines changed

‎src/SyntaxLookup.res‎

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -147,24 +147,32 @@ type props = {mdxSources: array<MdxRemote.output>}
147147
typeparams= {slug:string}
148148

149149
letdecode= (json:JSON.t)=> {
150-
openJson.Decode
151-
letid=json->field("id",string,_)
152-
letkeywords=json->field("keywords",array(string,...),_)
153-
letname=json->field("name",string,_)
154-
letsummary=json->field("summary",string,_)
155-
letcategory=json->field("category",string,_)->Category.fromString
156-
letstatus=
157-
json
158-
->optional(field("status",string,_),_)
159-
->Option.mapOr(Status.Active,Status.fromString)
160-
161-
{
162-
id,
163-
keywords,
164-
name,
165-
summary,
166-
category,
167-
status,
150+
openJSON
151+
switchjson {
152+
|Object(dict{
153+
"id":String(id),
154+
"keywords":Array(keywords),
155+
"name":String(name),
156+
"summary":String(summary),
157+
"category":String(category),
158+
"status": ?status,
159+
})=> {
160+
id,
161+
name,
162+
summary,
163+
category:Category.fromString(category),
164+
keywords:keywords->Array.filterMap(k=>
165+
switchk {
166+
|String(k)=>Some(k)
167+
|_=>None
168+
}
169+
),
170+
status:switchstatus {
171+
|Some(String(status))=>status->Status.fromString
172+
|_=>Status.Active
173+
},
174+
}
175+
|_=>throw(Failure(`Failed to decode SyntaxLookup. ${__LOC__}`))
168176
}
169177
}
170178

‎src/bindings/RescriptCompilerApi.res‎

Lines changed: 149 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ module Lang = {
1818
}
1919

2020
letdecode= (json):t=> {
21-
open!Json.Decode
22-
switchstring(json) {
23-
|"re"=>Reason
24-
|"res"=>Res
25-
|other=>throw(DecodeError(`Unknown language "${other}"`))
21+
openJSON
22+
switchjson {
23+
|String("re")=>Reason
24+
|String("res")=>Res
25+
|other=>throw(Failure(`Unknown language "${other->stringify}". ${__LOC__}`))
2626
}
2727
}
2828
}
@@ -91,14 +91,24 @@ module LocMsg = {
9191
}
9292

9393
letdecode= (json):t=> {
94-
openJson.Decode
95-
{
96-
fullMsg:json->field("fullMsg",string,_),
97-
shortMsg:json->field("shortMsg",string,_),
98-
row:json->field("row",int,_),
99-
column:json->field("column",int,_),
100-
endRow:json->field("endRow",int,_),
101-
endColumn:json->field("endColumn",int,_),
94+
openJSON
95+
switchjson {
96+
|Object(dict{
97+
"fullMsg":String(fullMsg),
98+
"shortMsg":String(shortMsg),
99+
"row":Number(row),
100+
"column":Number(column),
101+
"endRow":Number(endRow),
102+
"endColumn":Number(endColumn),
103+
})=> {
104+
fullMsg,
105+
shortMsg,
106+
row:row->Float.toInt,
107+
column:column->Float.toInt,
108+
endRow:endRow->Float.toInt,
109+
endColumn:endColumn->Float.toInt,
110+
}
111+
|_=>throw(Failure(`Failed to decode LocMsg. ${__LOC__}`))
102112
}
103113
}
104114

@@ -143,12 +153,18 @@ module Warning = {
143153
|WarnErr({warnNumber:int,details:LocMsg.t})// Describes an erronous warning
144154

145155
letdecode= (json):t=> {
146-
open!Json.Decode
147-
148-
letwarnNumber=field("warnNumber",int,json)
156+
openJSON
157+
letwarnNumber=switchjson {
158+
|Object(dict{"warnNumber":Number(warnNumber)})=>warnNumber->Float.toInt
159+
|_=>throw(Failure(`Failed to decode warn number. ${__LOC__}`))
160+
}
149161
letdetails=LocMsg.decode(json)
150162

151-
field("isError",bool,json) ?WarnErr({warnNumber,details}) :Warn({warnNumber,details})
163+
switchjson {
164+
|Object(dict{"isError":Boolean(isError)})=>
165+
isError ?WarnErr({warnNumber,details}) :Warn({warnNumber,details})
166+
|_=>throw(Failure(`Failed to decode warnings. ${__LOC__}`))
167+
}
152168
}
153169

154170
// Useful for showing errors in a more compact format
@@ -178,11 +194,14 @@ module WarningFlag = {
178194
}
179195

180196
letdecode= (json):t=> {
181-
openJson.Decode
182-
{
183-
msg:field("msg",string,json),
184-
warn_flags:field("warn_flags",string,json),
185-
warn_error_flags:field("warn_error_flags",string,json),
197+
openJSON
198+
switchjson {
199+
|Object(dict{
200+
"msg":String(msg),
201+
"warn_flags":String(warn_flags),
202+
"warn_error_flags":String(warn_error_flags),
203+
})=> {msg,warn_flags,warn_error_flags}
204+
|_=>throw(Failure(`Failed to decode WarningFlag. ${__LOC__}`))
186205
}
187206
}
188207
}
@@ -206,27 +225,37 @@ module TypeHint = {
206225
|CoreType(data)
207226

208227
letdecodePosition=json=> {
209-
openJson.Decode
210-
{
211-
line:field("line",int,json),
212-
col:field("col",int,json),
228+
openJSON
229+
switchjson {
230+
|Object(dict{"line":Number(line),"col":Number(col)})=> {
231+
line:line->Float.toInt,
232+
col:col->Float.toInt,
233+
}
234+
|_=>throw(Failure(`Failed to decode position. ${__LOC__}`))
213235
}
214236
}
215237

216238
letdecode= (json):t=> {
217-
openJson.Decode
218-
letdata= {
219-
start:field("start",decodePosition,json),
220-
end:field("end",decodePosition,json),
221-
hint:field("hint",string,json),
239+
openJSON
240+
letdata=switchjson {
241+
|Object(dict{"start":startPosition,"end":endPosition,"hint":String(hint)})=> {
242+
start:decodePosition(startPosition),
243+
end:decodePosition(endPosition),
244+
hint,
245+
}
246+
|_=>throw(Failure(`Failed to decode type hint position. ${__LOC__}`))
222247
}
223248

224-
switchfield("kind",string,json) {
225-
|"expression"=>Expression(data)
226-
|"type_declaration"=>TypeDeclaration(data)
227-
|"binding"=>Binding(data)
228-
|"core_type"=>CoreType(data)
229-
|other=>throw(DecodeError(`Unknown kind "${other}" type hint`))
249+
switchjson {
250+
|Object(dict{"kind":String(kind)})=>
251+
switchkind {
252+
|"expression"=>Expression(data)
253+
|"type_declaration"=>TypeDeclaration(data)
254+
|"binding"=>Binding(data)
255+
|"core_type"=>CoreType(data)
256+
|other=>throw(Failure(`Unknown kind "${other}" type hint. ${__LOC__}`))
257+
}
258+
|_=>throw(Failure(`Failed to decode type hint kind. ${__LOC__}`))
230259
}
231260
}
232261
}
@@ -242,12 +271,17 @@ module CompileSuccess = {
242271
}
243272

244273
letdecode= (~time:float,json):t=> {
245-
openJson.Decode
246-
{
247-
jsCode:field("js_code",string,json),
248-
warnings:field("warnings",array(Warning.decode,...),json),
249-
typeHints:withDefault([],field("type_hints",array(TypeHint.decode,...),...),json),
250-
time,
274+
openJSON
275+
switchjson {
276+
|Object(dict{
277+
"js_code":String(jsCode),
278+
"warnings":Array(warnings),
279+
"type_hints":Array(typeHints),
280+
})=>
281+
letwarnings=warnings->Array.map(Warning.decode)
282+
lettypeHints=typeHints->Array.map(TypeHint.decode)
283+
{jsCode,warnings,typeHints,time}
284+
|_=>throw(Failure(`Failed to decode CompileSuccess. ${__LOC__}`))
251285
}
252286
}
253287
}
@@ -260,11 +294,14 @@ module ConvertSuccess = {
260294
}
261295

262296
letdecode= (json):t=> {
263-
openJson.Decode
264-
{
265-
code:field("code",string,json),
266-
fromLang:field("fromLang",Lang.decode,json),
267-
toLang:field("toLang",Lang.decode,json),
297+
openJSON
298+
switchjson {
299+
|Object(dict{"code":String(code),"fromLang":fromLang,"toLang":toLang})=> {
300+
code,
301+
fromLang:fromLang->Lang.decode,
302+
toLang:toLang->Lang.decode,
303+
}
304+
|_=>throw(Failure(`Failed to decode ConvertSuccess. ${__LOC__}`))
268305
}
269306
}
270307
}
@@ -278,28 +315,41 @@ module CompileFail = {
278315
|OtherErr(array<LocMsg.t>)
279316

280317
letdecode= (json):t=> {
281-
open!Json.Decode
282-
283-
switchfield("type",string,json) {
284-
|"syntax_error"=>
285-
letlocMsgs=field("errors",array(LocMsg.decode,...),json)
286-
// TODO: There seems to be a bug in the ReScript bundle that reports
287-
// back multiple LocMsgs of the same value
288-
locMsgs->LocMsg.dedupe->SyntaxErr
289-
|"type_error"=>
290-
letlocMsgs=field("errors",array(LocMsg.decode,...),json)
291-
TypecheckErr(locMsgs)
292-
|"warning_error"=>
293-
letwarnings=field("errors",array(Warning.decode,...),json)
294-
WarningErr(warnings)
295-
|"other_error"=>
296-
letlocMsgs=field("errors",array(LocMsg.decode,...),json)
297-
OtherErr(locMsgs)
298-
299-
|"warning_flag_error"=>
300-
letwarningFlag=WarningFlag.decode(json)
301-
WarningFlagErr(warningFlag)
302-
|other=>throw(DecodeError(`Unknown type "${other}" in CompileFail result`))
318+
openJSON
319+
switchjson {
320+
|String(type_)=>
321+
switchtype_ {
322+
|"syntax_error"=>
323+
letlocMsgs=switchjson {
324+
|Object(dict{"erros":Array(errors)})=>errors->Array.map(LocMsg.decode)
325+
|_=>throw(Failure(`Failed to decode erros from syntax_error. ${__LOC__}`))
326+
}
327+
// TODO: There seems to be a bug in the ReScript bundle that reports
328+
// back multiple LocMsgs of the same value
329+
locMsgs->LocMsg.dedupe->SyntaxErr
330+
|"type_error"=>
331+
letlocMsgs=switchjson {
332+
|Object(dict{"erros":Array(errors)})=>errors->Array.map(LocMsg.decode)
333+
|_=>throw(Failure(`Failed to decode erros from type_error. ${__LOC__}`))
334+
}
335+
TypecheckErr(locMsgs)
336+
|"warning_error"=>
337+
letwarnings=switchjson {
338+
|Object(dict{"erros":Array(warnings)})=>warnings->Array.map(Warning.decode)
339+
|_=>throw(Failure(`Failed to decode errors from warning_error. ${__LOC__}`))
340+
}
341+
WarningErr(warnings)
342+
|"other_error"=>
343+
letlocMsgs=switchjson {
344+
|Object(dict{"erros":Array(errors)})=>errors->Array.map(LocMsg.decode)
345+
|_=>throw(Failure(`Failed to decode errors from other_error. ${__LOC__}`))
346+
}
347+
OtherErr(locMsgs)
348+
349+
|"warning_flag_error"=>WarningFlagErr(WarningFlag.decode(json))
350+
|other=>throw(Failure(`Unknown type "${other}" in CompileFail result. ${__LOC__}`))
351+
}
352+
|_=>throw(Failure(`Failed to decode CompileFail. ${__LOC__}`))
303353
}
304354
}
305355
}
@@ -313,14 +363,19 @@ module CompilationResult = {
313363

314364
// TODO: We might change this specific api completely before launching
315365
letdecode= (~time:float,json:JSON.t):t=> {
316-
open!Json.Decode
317-
318-
tryswitchfield("type",string,json) {
319-
|"success"=>Success(CompileSuccess.decode(~time,json))
320-
|"unexpected_error"=>UnexpectedError(field("msg",string,json))
321-
|_=>Fail(CompileFail.decode(json))
322-
}catch {
323-
|DecodeError(errMsg)=>Unknown(errMsg,json)
366+
openJSON
367+
switchjson {
368+
|Object(dict{"type":String(type_)})=>
369+
switchtype_ {
370+
|"success"=>Success(CompileSuccess.decode(~time,json))
371+
|"unexpected_error"=>
372+
switchjson {
373+
|Object(dict{"msg":String(msg)})=>UnexpectedError(msg)
374+
|_=>throw(Failure(`Failed to decode msg from unexpected_error. ${__LOC__}`))
375+
}
376+
|_=>Fail(CompileFail.decode(json))
377+
}
378+
|_=>throw(Failure(`Failed to decode CompilationResult. ${__LOC__}`))
324379
}
325380
}
326381
}
@@ -333,16 +388,22 @@ module ConversionResult = {
333388
|Unknown(string,JSON.t)
334389

335390
letdecode= (~fromLang:Lang.t, ~toLang:Lang.t,json):t=> {
336-
open!Json.Decode
337-
tryswitchfield("type",string,json) {
338-
|"success"=>Success(ConvertSuccess.decode(json))
339-
|"unexpected_error"=>UnexpectedError(field("msg",string,json))
340-
|"syntax_error"=>
341-
letlocMsgs=field("errors",array(LocMsg.decode,...),json)
342-
Fail({fromLang,toLang,details:locMsgs})
343-
|other=>Unknown(`Unknown conversion result type "${other}"`,json)
344-
}catch {
345-
|DecodeError(errMsg)=>Unknown(errMsg,json)
391+
openJSON
392+
switchjson {
393+
|Object(dict{
394+
"type":String(type_),
395+
"msg": ?Some(String(msg)),
396+
"errors": ?Some(Array(errors)),
397+
})=>
398+
switchtype_ {
399+
|"success"=>Success(ConvertSuccess.decode(json))
400+
|"unexpected_error"=>msg->UnexpectedError
401+
|"syntax_error"=>
402+
letlocMsgs=errors->Array.map(LocMsg.decode)
403+
Fail({fromLang,toLang,details:locMsgs})
404+
|other=>Unknown(`Unknown conversion result type "${other}"`,json)
405+
}
406+
|_=>throw(Failure(`Failed to decode ConversionResult. ${__LOC__}`))
346407
}
347408
}
348409
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp