-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Scala support #843
Scala support #843
Conversation
@JannikArndt That's amazing! Thank you. Do you have plans to support union types eventually? |
There are a few compile errors:
Also, would you mind adding Scala to the test suite? See |
Union Types Tests Compile warnings |
I thought cases classes is how you represent unions in Scala? Isn't that the equivalent to discriminated unions in ML-like languages? We'll wait for the test suite before merging. |
Btw, if you need any help, it's probably quicker if you join us on Slack. |
@JannikArndt What's the status on this? Can we help? |
Hey there, sorry, I didn't have the time to write the tests, yet :( |
@JannikArndt is there anything wrong with just having a sealed trait of one argument case classes to represent union types? I suppose it would play weird with singleton types like None or something so I suppose that partially answers the question. Dotty won't become Scala 3 until 2020 as far as I've heard, so I feel like there are precious limited options in the short term. |
@JannikArndt any opposition to handling this like https://scalapb.github.io/generated-code.html specifically the section on oneOf? |
@hderms Sounds good, I personally like the ScalaPB style. I still didn't find the time to implement anything further, though :( |
@JannikArndt @hderms I'd really like to get this closed. I'm happy to take over the work if you can give a short summary of what still needs to be done, from your point of view. If you could write a test driver (see |
@schani I'm not sure what needs to be done still but I felt like imitating the way ScalaPB does union types would be a reasonable way to do oneOf |
Doesn't look like this is moving forward, so I'm closing it for now. Please revive when/if there is new work. |
@schani I'm working on this now. Can you point me in the direction of a json schema or whatever that we would be expected to produce unions from? I tried |
@hderms Kotlin does not produce unions, but Rust does. It doesn't look like much in Rust, but that's because Rust's serialization framework does all the hard work. To see how to do it the hard way, look at C#, Swift, or Go. |
@schani maybe I'm misinterpreting it but I don't get how this output is a union?
namespace QuickType
{
using System;
using System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
public partial class Union
{
[JsonProperty("$schema")]
public Uri Schema { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("items")]
public Items Items { get; set; }
}
public partial class Items
{
[JsonProperty("oneOf")]
public OneOf[] OneOf { get; set; }
}
public partial class OneOf
{
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("properties")]
public Properties Properties { get; set; }
[JsonProperty("required")]
public string[] OneOfRequired { get; set; }
}
public partial class Properties
{
[JsonProperty("one", NullValueHandling = NullValueHandling.Ignore)]
public One One { get; set; }
[JsonProperty("two")]
public One Two { get; set; }
[JsonProperty("three", NullValueHandling = NullValueHandling.Ignore)]
public One Three { get; set; }
}
public partial class One
{
[JsonProperty("type")]
public string Type { get; set; }
}
public partial class Union
{
public static Union FromJson(string json) => JsonConvert.DeserializeObject<Union>(json, QuickType.Converter.Settings);
}
public static class Serialize
{
public static string ToJson(this Union self) => JsonConvert.SerializeObject(self, QuickType.Converter.Settings);
}
internal static class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
Converters =
{
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
},
};
}
} or this: package main
import "encoding/json"
func UnmarshalUnion(data []byte) (Union, error) {
var r Union
err := json.Unmarshal(data, &r)
return r, err
}
func (r *Union) Marshal() ([]byte, error) {
return json.Marshal(r)
}
type Union struct {
Schema string `json:"$schema"`
Type string `json:"type"`
Items Items `json:"items"`
}
type Items struct {
OneOf []OneOf `json:"oneOf"`
}
type OneOf struct {
Type string `json:"type"`
Properties Properties `json:"properties"`
Required []string `json:"required"`
}
type Properties struct {
One *One `json:"one,omitempty"`
Two One `json:"two"`
Three *One `json:"three,omitempty"`
}
type One struct {
Type string `json:"type"`
} is |
Sorry, you'll have to pass |
@schani my bad. That's what I get for trying to work on a project I barely understand :) |
We're also somewhat lacking in the documentation department ;-) |
I added scala as an output format.