Skip to content

Instantly share code, notes, and snippets.

@ryuheechul
Last active October 13, 2018 07:36
Show Gist options
  • Select an option

  • Save ryuheechul/e9b9fd7ddd2ed94ddbaa5f5cbadc4f09 to your computer and use it in GitHub Desktop.

Select an option

Save ryuheechul/e9b9fd7ddd2ed94ddbaa5f5cbadc4f09 to your computer and use it in GitHub Desktop.
Describing an issue with Reason that not processing nested object properly via `jsConverter`

Claim: Difficulty with interop with JS when accepting nested object param from JS to Reason

Reasonml Ver: 3.3.2

  • No problem with taking parameters from JS and matching to any type contains optional with one depth

  • But it seems to lose values wrapped by option if you have more depth like below

type file = {
  name: option(string),
  content: option(string),
};

[@bs.deriving {jsConverter: newType}]
type configMap = {
  name: option(string),
  namespace: option(string),
  file: file
};

proof using Test.re

console log image from the proof: screen shot 2018-10-13 at 4 34 30 pm

type file = {
name: option(string),
content: option(string),
};
type reConfigMap = {
name: option(string),
namespace: option(string),
file: file
};
[@bs.deriving {jsConverter: newType}]
type jsConfigMap = {
name: option(string),
namespace: option(string),
file: file
};
let rCM: reConfigMap = {
name: Some("name"),
namespace: Some("namespace"),
file: {
name: Some("filename"),
content: Some("filecontent")
}
};
let inspectOptStr = oStr => switch (oStr) {
| Some(s) => s
| None => "NOTHING!"
};
let inspectFile = ({name, content}) => {
inspectOptStr(name)
++ ", "
++ inspectOptStr(content)
};
let inspectCM = ({name, namespace, file}: reConfigMap) => {
inspectOptStr(name)
++ ", "
++ inspectOptStr(namespace)
++ ", "
++ inspectFile(file)
};
let inspectJSCM = ({name, namespace, file}: jsConfigMap) => {
inspectOptStr(name)
++ ", "
++ inspectOptStr(namespace)
++ ", "
++ inspectFile(file)
};
"reason type works well as below"->Js.log;
rCM->inspectCM->Js.log;
let inspectCMFromJS = absJSCM => {
absJSCM->jsConfigMapFromJs->inspectJSCM;
};
""->Js.log;
"------------------------------"->Js.log;
""->Js.log;
[%raw {|
(() => {
const cm = {
name: 'cm-name',
namespace: 'cm-name',
file: {
name: 'cm-filename',
content: 'cm-filecontent'
}
};
console.log("jsConfigMap can't handle nested file as below");
console.log(inspectCMFromJS(cm));
})()
|}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment