kiota icon indicating copy to clipboard operation
kiota copied to clipboard

Add Crystal

Open dsisnero opened this issue 2 years ago • 7 comments

Add Crystal Language generation -

dsisnero avatar Jan 27 '23 20:01 dsisnero

Hi @dsisnero Thanks for reaching out. This is something we're unlikely to work on at this point unless we see additional popular demand (others reading this comment, please add a thumbs up to the initial post to help us track demand if you'd like to see support for this language). If this is something you'd like to implement, we'd be happy to guide you through. A lot of the ruby work can probably be reused as far as I understand.

baywet avatar Jan 30 '23 15:01 baywet

Is there a document that shows how to add a language?

dsisnero avatar Oct 19 '23 19:10 dsisnero

not today unfortunately as all languages available at this point have been implemented by internal contributors.

But I can provide pointers on this conversation, the first step would be to implement the abstractions library for the language. Most importantly the:

  • request information class
  • Serialization writer interface
  • Parse node interface
  • request adapter interface

from this first work we'll be able to know what models and request adapters need to look like, and start working on the generation side. We already have "examples" in multiple languages for you to look at (I'm guessing Ruby will be the closest to Crystal?) Before those packages get their own repo, we usually collocate them with the generator. (currently only Swift is in that state if you look at the repository).

If you want a better view of all the work that's required to get a language working end to end, you can look at all the swift issues

Let me know if you have additional questions.

baywet avatar Oct 20 '23 14:10 baywet

I'm sympathetic to this effort, this is a small detail, but opened this: https://github.com/std-uritemplate/std-uritemplate/issues/84 If someone will contribute I'll be happy to review, otherwise I'll eventually do it anyhow 🙂

andreaTP avatar Oct 20 '23 15:10 andreaTP

Thanks for the information. I am trying to figure out how to wrap the Parse node interface with the way Crystal libraries serialize JSON, YAML, etc. To serialize, and deserialize from Crystal - this is how it is usually done - a similar include Y

JSON::Serializable YAML::Serializable

require "json"

class Location
  include JSON::Serializable
  include YAML::Serializable

  @[JSON::Field(key: "lat")]
  @[YAML::Field(key: "lat")]
  property latitude : Float64

  @[JSON::Field(key: "lng")]
  @[YAML::Field(key: "lng")]
  property longitude : Float64
end

class House
  include JSON::Serializable
  include YAML::Serializable
  property address : String
  property location : Location?
end

house = House.from_json(%({"address": "Crystal Road 1234", "location": {"lat": 12.3, "lng": 34.5}}))
house.address  # => "Crystal Road 1234"
house.location # => #<Location:0x10cd93d80 @latitude=12.3, @longitude=34.5>
house.to_json  # => %({"address":"Crystal Road 1234","location":{"lat":12.3,"lng":34.5}})

houses = Array(House).from_json(%([{"address": "Crystal Road 1234", "location": {"lat": 12.3, "lng": 34.5}}]))
houses.size    # => 1
houses.to_json # => %([{"address":"Crystal Road 1234","location":{"lat":12.3,"lng":34.5}}])

Anyone have any ideas, or should I just re-implement Crystal serialization by converting the java or csharp code?

dsisnero avatar Oct 23 '23 23:10 dsisnero

@dsisnero I think that:

should I just re-implement Crystal serialization by converting the java or csharp code?

would be the way to go, you can internally rely on the low-level primitives of Crystal ser/de but the exposed interface should be similar to what you find in C# and Java.

andreaTP avatar Oct 24 '23 09:10 andreaTP

A couple of principles to keep in mind:

  • we don't want the generated code to depend on anything else than the abstractions to "build". (in a static type sense)
  • we want the generated code to be agnostic from any given serialization format/library
  • we want to avoid using reflection as much as possible (perf, side effects...)

This is why models implement the auto-serialization pattern (they describe how to serialize/deserialize themselves)

baywet avatar Oct 24 '23 12:10 baywet