-
-
Notifications
You must be signed in to change notification settings - Fork 173
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
Discussion: Support for importing from JSON #121
Comments
My humble opinion about this is that Dhall should always require a type annotation, regardless of how 'guessable' the imported type is. The rationale is that even though your list stores only ints now it may end up with booleans later and it feels inappropriate to implicitly guess when importing files. Better would be to make Also, I think that only implicit conversion that is sensible is to equate null & missing values to optional. |
I don’t think the idea is to guess types. From #326: ./foo.json as JSON : List { name : Text, age : Natural } |
Well, since outlined options above are "reject" and "accept with type annotation" I thought that there would be a form where type annotation wasn't necessary ie. type would arise from imported data. Sorry about my confusion. |
I think the last section was about how JSON should be typed in dhall by default (yet still with deterministic rules what gets which type). For example the aforementioned
would accept
could accept the latter and would type it as @Gabriel439 I personally think dynamically adding optionals would only make sense if Dhall can infer the needed fields from usage, which it can’t. Since everywhere else types are not optional (and can’t be inferred) I think this would break consistency (and maybe bring up the expectation that it infers from usage). |
Yeah, my personal preference is for a mandatory type signature, too. I just didn't want to bias the discussion at the very beginning. My reasoning is that it would be very weird for this: [ 1 ] ... to have an inferred type of [ 1, null ] ... has an inferred type of However, there is still the question of whether or not Dhall should allow importing this JSON: [ 1, true ] ... using a type annotation with a sum type like this: ./foo.json as JSON : List < Left : Integer, Right : Bool > The main downside of that proposal that I'm aware of is that you have to specify what happens if you start nesting sum types or if you have sum types with multiple constructors that wrap the same type. My inclination is to still reject that, but I just wanted to mention it because
|
Well, I was referring to "typed by default" as guessing. I think there are two use cases for dhall-from-json:
As a final thought, how about adding import plugins (using similar scheme as in pandoc) to dhall? You would supply dhall with a program/script that can output dhall expressions and then import other bits of data through that script. For example, you could do something like:
This would allow testing different JSON import schemes or interacting with other more task specific data sources. Successful data providing plugins could be merged to dhall after they've seen some real world use. (This could also handle things like dhall-lang/dhall-haskell#292) |
Yeah, I like the plugin idea, although I would prefer to do it through the Haskell API instead of the command line |
Untagged unions should be different from sum types in my opinion. I wouldn’t have expected |
Command line vs. Haskell API depends on who you wish to write plugins. I would guess that today most dhall is consumed by Haskell programs and the plugin is easiest and safest to add there. However, if you use dhall from command line a lot then you'll need to build your own binary. Not a problem for Haskell users but probably a bit of a hurdle for the rest. |
Keep in mind that the long term goal of Dhall is language bindings other than Haskell. So ideally there would be a language binding in that user's preferred language that they could use to customize the import resolution behavior. The main reason I want to avoid a plugin API is that then I have to standardize the semantics and interface for plugins and every Dhall implementation would need to support that standard plugin semantics. Note that in the long run I don't want users to have to use any binaries at all. The integration with their preferred language should be through a library rather than a subprocess call to some In other words: I agree with the goal that users shouldn't have to build their own binaries, but I believe that the correct solution to that goal is to finish standardizing import semantics in order to create more language bindings rather than make the binaries of one implementation hyper-customizable. |
I met an another case where having some kind of extended importing would be useful. I'm using dhall to describe some course exercises. Now, some exercises are in want of bibliography links and all I have is a large bibtex file. In this case I converted the bibliography, partly and by hand, into dhall so I could import the required entries. It would've been nice if I could've imported the .bib file directly. Doing the bib->dhall conversion means that the .bib file is no longer the primary data source and that I need to write a converter from dhall to bib to make use of the entries that I converted into dhall. Perhaps extending the syntax so that |
I've been pondering this for a little while and I feel that The tool could also possibly take two corpus' that reflect both valid and error JSON responses so allow for a union type to cover both circumstances... |
I've spent the afternoon making a toy json-to-dhall tool (https://gist.github.com/madjar/252c517644c0e13ef28a2a7ca71f5fa4). It's very prototypey code, and just supports most basic types, as well as optionals and dynamic maps (mapKey/mapValue). The question is: if I want to transform this into something that's actually useful, where should it live:
|
@madjar: We want to add this to the language standard and once it's there then it will live in all implementations of the standard using the The key thing to emphasize is that the standardization process and agreeing upon the desired behavior is the bottleneck here because once it is standardized then I expect it will be pretty straightforward to update the implementation to match. |
@Gabriel439 If your review it closely, then I'll have to apologize for the quality. It was kind of rushed this afternoon. The approach I've take is the one describe in dhall-lang/dhall-json#23 (comment), under "Convert and type check together", which is to recursively traverse both the json Having this tool made the conversion of a json file and the definition of its dhall type quite nice, allowing to incrementally add the missing parts to the type definition while having quick feedback. But I understand that you see this not as tooling, but as part of the language, thus requiring more standardization than "whatever the tool does". I'll familiarize myself with the processes of the project, then. Thanks! |
My opinion on this is that this would be extremely cool. |
I agree that a With that said, I don't think that the type inputs for Consider, for instance, the dhall-terraform-output script which takes Terraform's JSON output and assembles both a type and a record from that output. Because the record keys are variable, it's not possible to define a Dhall type for arbitrary Terraform JSON output ahead of time (or rather, it is, but it would be fragile). However, this doesn't mean that Terraform's JSON output doesn't follow a predictable pattern, and ideally, upon parsing Terraform's JSON output, it would be best to verify that the JSON output fits that pattern, and possibly even get a type that fit the predicted pattern. What's the best way to do that? I don't know. Maybe, instead of Probably it would be best to start with |
I think requiring type annotations will make it mostly impossible to import large JSON structures like CloudFormation data. |
@alexanderkjeldaas: Wouldn't they also fail to import without type annotations? Usually those kinds of JSON files mix records of various types |
@alexanderkjeldaas : Please, try the (new) |
Json-to-dhall is great! I'm not sure how 'done' it is considered to be? Does this unlock this issue, i.e. creating the syntax for the core language and command line dhall utility to This would be great! |
The decoder idea sounds powerful, and seems a special case of this:
Is that correct? If this is the case, we can also solve the text manipulation issue view plugins/decoders: I'm sure there are some concerns here :-) It is a powerful idea, but the decoding thing may possibly benefit from some more maturation. How bad is it to have a syntactic sugar like but how do you deal with the security challenges? |
I think the issue in question is #613. In particular, this comment. |
I think we could support custom encoders (including for json and yaml) and direct importing only from json and yaml due their special status.
|
The biggest issue is that an import could run an arbitrary executable. However, we could do something similar to the referential sanity check (i.e. only local imports can run executables, since they are trusted anyway). After all, we already trust local imports to send environment variables in custom headers (i.e. The second biggest issue is that relying on external executables complicates Dhall's distribution model (compared to native |
Take a look at how cue is doing this: https://cuelang.org/ Whereas I found that although dhall-kubernetes is great, I am spending a massive amount of time converting just a single existing valid configuration from YAML to dhall. Solely due to this waste of time I will have to try using cue instead. TypeScript took off I think in large part because of the ease of transition:
Ideally dhall would support not just importing but also applying type definitions to existing files. Either feature though would help work with the existing world. If dhall is going to be designed to interoperate with the outside world then it does need to work with different formats. Plugins need to be able to produce a common data structure that preserve file location information so that users can get good error messages. It might be possible for now to tell users to convert their YAML, etc to JSON and that they won't get good information about the file location of their error. |
@gregwebs: We're pretty close. We've had |
@gregwebs Would neither of the |
Yes, in theory yaml-to-dhall will work for me, I didn't realize it was in dhall-json. That could make the process of using existing files go from hours to minutes! In practice, however it doesn't actually work for dhall-kubernetes. This is because most of the K8s fields are actually optional. dhall-kuberntes is designed so that one will write definitions with the help of defaults so you will write: defaults.Deployment // { metadata = defaults.ObjectMeta } However, yaml-to-dhall doesn't know about these defaults and complains about missing fields. I think this is a separate issue reported here. But there is some relation here since dhall-kubernetes works fine when writing in dhall but is unable to import yaml. It seems like dhall-to-yaml needs a |
I think we all agree that |
@Nadrieril: The ambiguity was not an issue for me. I think this is worth standardizing now |
The use of How about using e.g.
Or maybe |
Another idea:
|
@sjakobi: Yeah, I like |
I prefer
because using a colon to indicate “this thing has that type” is a well-established part of the language - type assertions, empty lists, empty merge, etc, whereas the existing meaning of |
I would love the ability to import from JSON and agree that the use of Would one have to fully specify the type of the JSON to import it, or could one specify only the desired structure (with anything else being polymorphic). E.g. if I have the JSON
but only care about the name fields, would this import allow you to do
or would you have to do
|
Would love to see |
I created a separate issue to track the idea of customizable parsers: #989 ... since that's one way we might address this (by implementing JSON support within the language) |
One way we can make progress on this issue is to split it into two steps:
|
@Gabriel439 If I understood correctly, the issue is blocked by the lack of an implementation / spec? Seems like an easy way to move forward is to have something like the following (which was already suggested) ./file.json as Json : FileConfig which doesn't introduce any new keyword nor it needs support for |
@mujx: Yeah, this requires a change to the standard since it cannot be implemented entirely within |
One pretty heavily requested feature is importing JSON values directly into Dhall. The most common requested reasons for doing this are:
I'm open to the idea although I probably won't implement it until the import semantics are standardized (any day now :) ). In the meantime, though, I can still gather feedback on whether Dhall should support this feature and to flesh out what it might look like if it were proposed.
This would most likely be similar in spirit to Dhall's existing support for importing raw text using
as Text
. In other words, you would be able to write./foo.json as JSON
to import a JSON file into Dhall), however there are still some open issues that need to be resolved.For those who are in favor of this feature, the easiest way to drive this discussion is to discuss how Dhall should behave when importing the following JSON expressions which cover most of the corner cases that I'm aware of:
For each of these imports, should Dhall:
Optional
type)?The text was updated successfully, but these errors were encountered: