Comments (2)
How about something like this:
module Options where
-- 'Options' is some opaque foreign type that holds a heterogeneous collection of `Option a`
foreign import data Options :: Type
foreign import optionsEmpty :: Options
foreign import optionsAdd :: forall a. Options -> Option a -> Options
foreign import optionsGet :: forall a. Options -> String -> Maybe (Option a)
data OptionKind
= Boolean
| String
| Choice (Array String)
type Option a =
{ kind :: OptionKind
, value :: Json -- this is foreign data that should be an `a` but we don't assume it is
, default :: a
, label :: String
}
-- We use this from JavaScript to put values into options
-- Storing an invalid value will cause the Option to revert to its default value
setValueFromJS :: forall a. Option a -> Json -> Option a
setValueFromJS opt v = opt { value = v }
booleanOption :: String -> Boolean -> Option Boolean
booleanOption label default =
{ kind: Boolean
, value: fromBoolean default
, default
, label
}
-- An example option
renderProperties :: Option Boolean
renderProperties = booleanOption "Render Properties" true
class OptionValue a where
getValue :: Option a -> Maybe a
setValue :: Option a -> a -> Option a
instance optionValueBoolean :: OptionValue Boolean where
getValue { value } = toBoolean value
setValue opt b = opt { value = fromBoolean b }
-- Helpers for reading Options
getValueOrDefault :: forall a. Option a -> a
getValueOrDefault opt@{ default } = fromMaybe default $ getValue opt
getOption :: forall a. Options -> Option a -> Maybe (Option a)
getOption opts { label } = optionsGet opts label
getOptionOrDefault :: forall a. Options -> Option a -> a
getOptionOrDefault opts opt@{ default } = fromMaybe default $ getOption opts opt
-- Nice, but definitely not necessary
infixr 5 getOptionOrDefault as ~>
-- Later when we have an `options :: Options` coming from the CLI or web app:
let shouldRender = options ~> renderProperties
Then in Options.js
:
exports.optionsEmpty = [];
exports.optionsAdd = function (opts) {
return functions (opt) {
opts.push(opt);
};
};
...
from quicktype.
Implemented in b91eff8
from quicktype.
Related Issues (20)
- C++ output with "https://app.quicktype.io/" is very different from the one generated locally with master repo HOT 1
- [BUG]: no quicktype binary for 23.0.159 release
- [BUG]: quicktype version 23.0.161 command not found HOT 3
- [FEATURE]: Swift - Sendable JSONAny HOT 1
- [FEATURE]: Optional Parameters in Dart
- [FEATURE]: "empty" method in Dart
- [BUG]: Cannot fetch external links from $ref due inputStream.once error
- [FEATURE]: Use externally provided class
- [FEATURE]: cJSON - generate source/header pairs
- [FEATURE]: Can we add string to all json types?
- [BUG]: Uncaught ReferenceError: process is not defined in the browser HOT 3
- [BUG]: your dependency on @YousefED/typescript-json-schema depends on old/buggy packages
- [FEATURE]: Optionally append enum type name to Golang enum constants
- [BUG]: Minimum integer values are not handled correctly when generating C++ code.
- [FEATURE]: better separation of types and serialization / deserialilzation code in generated C++ output
- [FEATURE]: CSharp - make emitting DateOnlyConverter & TimeOnlyConverter optional
- [FEATURE]: xml2typescript using quicktype ( doxygen XLST into typescript definitions)
- [BUG]: Unable to handle `string|integer` in JSON schema.
- [BUG]: Generates an extra class if using $refs to URLs that don't exist HOT 1
- [BUG]: Generating from a folder of schema files gives invalid results
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from quicktype.