Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ocaml-decoders conversion experiment #1280

Merged
merged 9 commits into from
Feb 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Extensions/ExtHostInitData.re
Expand Up @@ -77,6 +77,7 @@ module ExtensionInfo = {
data.extensionKind |> ExtensionManifest.Encode.kind,
),
("contributes", data.contributes |> ExtensionContributions.encode),
("enableProposedApi", data.enableProposedApi |> bool),
])
);
};
Expand Down
140 changes: 43 additions & 97 deletions src/Extensions/ExtensionContributions.re
Expand Up @@ -19,15 +19,12 @@ module Command = {

let decode =
Json.Decode.(
field("command", string)
>>= (
command =>
field("title", LocalizedToken.decode)
>>= (
title =>
field_opt("category", string)
>>= (category => succeed({command, title, category}))
)
obj(({field, _}) =>
{
command: field.required("command", string),
title: field.required("title", LocalizedToken.decode),
category: field.optional("category", string),
}
)
);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An example of a small decoder using obj


Expand Down Expand Up @@ -88,24 +85,13 @@ module Language = {

let decode =
Json.Decode.(
field("id", string)
>>= (
id =>
field_opt("extensions", list(string))
|> default([])
>>= (
extensions =>
field_opt("aliases", list(string))
|> default([])
>>= (
aliases =>
field_opt("configuration", string)
>>= (
configuration =>
succeed({id, extensions, aliases, configuration})
)
)
)
obj(({field, _}) =>
{
id: field.required("id", string),
extensions: field.withDefault("extensions", [], list(string)),
aliases: field.withDefault("aliases", [], list(string)),
configuration: field.optional("configuration", string),
}
)
);

Expand All @@ -131,22 +117,13 @@ module Grammar = {

let decode =
Json.Decode.(
field_opt("language", string)
>>= (
language =>
field("scopeName", string)
>>= (
scopeName =>
field("path", string)
>>= (
path =>
field_opt("treeSitterPath", string)
>>= (
treeSitterPath =>
succeed({language, scopeName, path, treeSitterPath})
)
)
)
obj(({field, _}) =>
{
language: field.optional("language", string),
scopeName: field.required("scopeName", string),
path: field.required("path", string),
treeSitterPath: field.optional("treeSitterPath", string),
}
)
);

Expand Down Expand Up @@ -180,15 +157,12 @@ module Theme = {

let decode =
Json.Decode.(
field("label", string)
>>= (
label =>
field("uiTheme", string)
>>= (
uiTheme =>
field("path", string)
>>= (path => succeed({label, uiTheme, path}))
)
obj(({field, _}) =>
{
label: field.required("label", string),
uiTheme: field.required("uiTheme", string),
path: field.required("path", string),
}
)
);

Expand All @@ -212,14 +186,12 @@ module IconTheme = {

let decode =
Json.Decode.(
field("id", string)
>>= (
id =>
field("label", string)
>>= (
label =>
field("path", string) >>= (path => succeed({id, label, path}))
)
obj(({field, _}) =>
{
id: field.required("id", string),
label: field.required("label", string),
path: field.required("path", string),
}
)
);

Expand All @@ -245,43 +217,17 @@ type t = {

let decode =
Json.Decode.(
field_opt("commands", list(Command.decode))
|> default([])
>>= (
commands =>
field_opt("languages", list(Language.decode))
|> default([])
>>= (
languages =>
field_opt("grammars", list(Grammar.decode))
|> default([])
>>= (
grammars =>
field_opt("themes", list(Theme.decode))
|> default([])
>>= (
themes =>
field_opt("iconThemes", list(IconTheme.decode))
|> default([])
>>= (
iconThemes =>
field_opt("configuration", Configuration.decode)
|> default([])
>>= (
configuration =>
succeed({
commands,
languages,
grammars,
themes,
iconThemes,
configuration,
})
)
)
)
)
)
obj(({field, _}) =>
{
commands: field.withDefault("commands", [], list(Command.decode)),
languages: field.withDefault("languages", [], list(Language.decode)),
grammars: field.withDefault("grammars", [], list(Grammar.decode)),
themes: field.withDefault("themes", [], list(Theme.decode)),
iconThemes:
field.withDefault("iconThemes", [], list(IconTheme.decode)),
configuration:
field.withDefault("configuration", [], Configuration.decode),
}
)
);

Expand Down
145 changes: 33 additions & 112 deletions src/Extensions/ExtensionManifest.re
Expand Up @@ -51,118 +51,39 @@ module Decode = {
let engine = field("vscode", string);

let manifest =
field("name", string)
>>= (
name =>
field("version", string)
>>= (
version =>
one_of([
("author", field("author", author)),
("publisher", field("publisher", string)),
("default", succeed("Unknown Author")),
])
>>= (
author =>
field_opt("displayName", LocalizedToken.decode)
>>= (
displayName =>
field_opt("description", string)
>>= (
description =>
field_opt("main", string)
>>= (
main =>
field_opt("icon", string)
>>= (
icon =>
field_opt("categories", list(string))
|> default([])
>>= (
categories =>
field_opt("keywords", list(string))
|> default([])
>>= (
keywords =>
field("engines", engine)
>>= (
engines =>
field_opt(
"activationEvents",
list(string),
)
|> default([])
>>= (
activationEvents =>
field_opt(
"extensionDependencies",
list(string),
)
|> default([])
>>= (
extensionDependencies =>
field_opt(
"extensionPack",
list(string),
)
|> default([])
>>= (
extensionPack =>
field_opt(
"extensionKind",
kind,
)
|> default(Ui)
>>= (
extensionKind =>
field(
"contributes",
ExtensionContributions.decode,
)
>>= (
contributes =>
field_opt(
"enableProposedApi",
bool,
)
|> default(
false,
)
>>= (
enableProposedApi =>
succeed({
name,
version,
author,
displayName,
description,
main,
icon,
categories,
keywords,
engines,
activationEvents,
extensionDependencies,
extensionPack,
extensionKind,
contributes,
enableProposedApi,
})
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
Json.Decode.(
obj(({field, whatever, _}) =>
{
name: field.required("name", string),
version: field.required("version", string),
author:
whatever(
one_of([
("author", field.monadic("author", author)),
("publisher", field.monadic("publisher", string)),
("default", succeed("Unknown Author")),
]),
),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is one of the shortcomings of the obj decoder. Because the field.* decoders aren't "normal" monadic decoders, they can't be composed. So we need an escape hatch. This is it, called whatever while I think of a better name for it.

displayName: field.optional("displayName", LocalizedToken.decode),
description: field.optional("description", string),
main: field.optional("main", string),
icon: field.optional("icon", string),
categories: field.withDefault("categories", [], list(string)),
keywords: field.withDefault("keywords", [], list(string)),
engines: field.required("engines", engine),
activationEvents:
field.withDefault("activationEvents", [], list(string)),
extensionDependencies:
field.withDefault("extensionDependencies", [], list(string)),
extensionPack:
field.withDefault("extensionPack", [], list(string)),
extensionKind: field.withDefault("extensionKind", Ui, kind),
contributes:
field.required("contributes", ExtensionContributions.decode),
enableProposedApi:
field.withDefault("enableProposedApi", false, bool),
}
)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise I think this looks significantly better than the alternatives.

);
};

Expand Down