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
17 changes: 17 additions & 0 deletions src/Core/Json.re
@@ -0,0 +1,17 @@
// API reference: https://mattjbray.github.io/ocaml-decoders/decoders/decoders-yojson/Decoders_yojson/index.html

type t = Yojson.Safe.json;

module Decode = {
include Decoders_yojson.Safe.Decode;

let default = default =>
map(Utility.Option.value(~default));
};

module Encode = {
include Decoders_yojson.Safe.Encode;
};

type decoder('a) = Decode.decoder('a);
type encoder('a) = Encode.encoder('a);
1 change: 1 addition & 0 deletions src/Core/Oni_Core.re
Expand Up @@ -30,6 +30,7 @@ module IndentationGuesser = IndentationGuesser;
module IndentationSettings = IndentationSettings;
module Input = Input;
module IntMap = Kernel.IntMap;
module Json = Json;
module Job = Job;
module LineNumber = LineNumber;
module Log = Kernel.Log;
Expand Down
1 change: 1 addition & 0 deletions src/Core/dune
Expand Up @@ -19,5 +19,6 @@
editor-core-types
timber
ReasonFuzz
decoders-yojson
)
(preprocess (pps ppx_deriving_yojson ppx_deriving.show)))
96 changes: 40 additions & 56 deletions src/Extensions/Configuration.re
Expand Up @@ -5,8 +5,7 @@
*
*/

let emptyJsonObject = `Assoc([]);
let emptyJsonArray = `List([]);
open Oni_Core;

// Type relating to 'ConfigurationModel' in VSCode
// This is an 'instance' of configuration - modelling user, workspace, or default configuration.
Expand All @@ -19,55 +18,38 @@ module Model = {
// overrides: ?
};

let empty = {contents: emptyJsonObject, keys: []};
let empty = {contents: Json.Encode.obj([]), keys: []};

let create = (~keys, contents) => {keys, contents};

let to_yojson = model => {
let {contents, keys} = model;

let keysJson = keys |> List.map(str => `String(str));
let encode = model =>
Json.Encode.(
obj([
("contents", model.contents),
("keys", model.keys |> list(string)),
("overrides", list(value, [])),
])
);

`Assoc([
("contents", contents),
("keys", `List(keysJson)),
("overrides", emptyJsonArray),
]);
};
let to_yojson = Json.Encode.encode_value(encode);

let ofExtensions = (extensions: list(ExtensionManifest.t)) => {
ExtensionManifest.(
{
let configModels: ExtensionContributions.Configuration.t =
extensions
|> List.map(manifest => manifest.contributes)
|> List.map(contributes =>
ExtensionContributions.getConfiguration(contributes)
)
|> List.flatten;

ExtensionContributions.Configuration.(
{
let keys =
List.map(
(configModel: config) => configModel.name,
configModels,
);

let json: Yojson.Safe.json =
`Assoc(
List.map(
({name, default}: config) => (name, default),
configModels,
),
);

let contents = Oni_Core.Utility.Json.explode(json);
{keys, contents};
}
);
}
);
open ExtensionManifest;
open ExtensionContributions.Configuration;

let configModels =
extensions
|> List.map(manifest => manifest.contributes.configuration)
|> List.flatten;

let keys = configModels |> List.map(it => it.name);
let contents =
configModels
|> List.map(({name, default}) => (name, default))
|> Json.Encode.obj
|> Oni_Core.Utility.Json.explode;

{keys, contents};
};

let toString = (model: t) => {
Expand All @@ -91,17 +73,19 @@ type t = {
// configurationScopes: {}
};

let to_yojson = config => {
let {defaults, user, workspace} = config;
`Assoc([
("defaults", defaults |> Model.to_yojson),
("user", user |> Model.to_yojson),
("workspace", workspace |> Model.to_yojson),
("folders", emptyJsonObject),
("isComplete", `Bool(true)),
("configurationScopes", emptyJsonObject),
]);
};
let encode = config =>
Json.Encode.(
obj([
("defaults", config.defaults |> Model.encode),
("user", config.user |> Model.encode),
("workspace", config.workspace |> Model.encode),
("folders", obj([])),
("isComplete", bool(true)),
("configurationScopes", obj([])),
])
);

let to_yojson = Json.Encode.encode_value(encode);

let empty = {
defaults: Model.empty,
Expand Down
10 changes: 7 additions & 3 deletions src/Extensions/Configuration.rei
Expand Up @@ -5,23 +5,27 @@
*
*/

open Oni_Core;

// Type relating to 'ConfigurationModel' in VSCode
// This is an 'instance' of configuration - modelling user, workspace, or default configuration.
// The full configuration is set up by combining the various configuration 'instances'.
module Model: {
type t;

let empty: t;
let create: (~keys: list(string), Yojson.Safe.t) => t;
let to_yojson: t => Yojson.Safe.t;
let create: (~keys: list(string), Json.t) => t;
let to_yojson: t => Json.t;
let encode: Json.encoder(t);

let ofExtensions: list(ExtensionManifest.t) => t;
let toString: t => string;
};

type t;

let to_yojson: t => Yojson.Safe.t;
let to_yojson: t => Json.t;
let encode: Json.encoder(t);
let empty: t;
let create:
(~defaults: Model.t=?, ~user: Model.t=?, ~workspace: Model.t=?, unit) => t;
54 changes: 48 additions & 6 deletions src/Extensions/ExtHostInitData.re
Expand Up @@ -19,7 +19,7 @@ open ExtensionScanner;
* https://github.com/onivim/vscode/blob/051b81fd1fa4fb656a5e1dab7235ac62dea58cfd/src/vs/workbench/api/node/extHost.protocol.ts#L79
*/
module ExtensionInfo = {
[@deriving (show({with_path: false}), yojson({strict: false, exn: true}))]
[@deriving show({with_path: false})]
type t = {
/* TODO: */
/* isBuiltIn: bool, */
Expand All @@ -31,10 +31,10 @@ module ExtensionInfo = {
/* publisher: string, */
main: option(string),
version: string,
engines: Engine.t,
engines: string,
activationEvents: list(string),
extensionDependencies: list(string),
extensionKind: ExtensionKind.t,
extensionKind: ExtensionManifest.kind,
contributes: ExtensionContributions.t,
enableProposedApi: bool,
};
Expand All @@ -57,24 +57,54 @@ module ExtensionInfo = {
enableProposedApi: manifest.enableProposedApi,
};
};

let encode = data =>
Json.Encode.(
obj([
("identifier", data.identifier |> string),
("extensionLocationPath", data.extensionLocationPath |> string),
("name", data.name |> string),
("main", data.main |> option(string)),
("version", data.version |> string),
("engines", data.engines |> string),
("activationEvents", data.activationEvents |> list(string)),
("extensionDependencies", data.extensionDependencies |> list(string)),
("extensionKind", data.extensionKind |> ExtensionManifest.Encode.kind),
("contributes", data.contributes |> ExtensionContributions.encode),
])
);
};

module Workspace = {
[@deriving (show({with_path: false}), yojson({strict: false, exn: true}))]
[@deriving show({with_path: false})]
type t = {__test: string};

let encode = workspace =>
Json.Encode.(
obj([
("__test", workspace.__test |> string)
])
);
};

module Environment = {
[@deriving (show({with_path: false}), yojson({strict: false, exn: true}))]
[@deriving show({with_path: false})]
type t = {globalStorageHomePath: string};

let create = (~globalStorageHomePath=Filesystem.unsafeFindHome(), ()) => {
let ret: t = {globalStorageHomePath: globalStorageHomePath};
ret;
};

let encode = env =>
Json.Encode.(
obj([
("globalStorageHomePath", env.globalStorageHomePath |> string)
])
);
};

[@deriving (show({with_path: false}), yojson({strict: false, exn: true}))]
[@deriving show({with_path: false})]
type t = {
extensions: list(ExtensionInfo.t),
parentPid: int,
Expand Down Expand Up @@ -102,3 +132,15 @@ let create =
__test: "",
},
};

let encode = data =>
Json.Encode.(
obj([
("extensions", data.extensions |> list(ExtensionInfo.encode)),
("parentPid", data.parentPid |> int),
("environment", data.environment |> Environment.encode),
("logsLocationPath", data.logsLocationPath |> string),
("autoStart", data.autoStart |> bool),
("workspace", data.workspace |> Workspace.encode),
])
);
2 changes: 1 addition & 1 deletion src/Extensions/ExtHostTransport.re
Expand Up @@ -149,7 +149,7 @@ let start =

let _sendInitData = () => {
let _: int =
send(MessageType.initData, ExtHostInitData.to_yojson(initData));
send(MessageType.initData, ExtHostInitData.encode(initData));
();
};

Expand Down