faustinoaq changed the topic of #amber to: Welcome to Amber Framework community! | https://amberframework.org | Developer happiness, productivity and bare metal performance | GH: https://github.com/amberframework | Docs: https://docs.amberframework.org | Gitter: https://gitter.im/amberframework/amber | IRC Logger: https://irclog.whitequark.org/amber | Amber::Server.start
<FromGitter> <eliasjpr> @Blacksmoke16 https://github.com/amberframework/granite/issues/361
<FromGitter> <Blacksmoke16> is a PR to fix it when doing `#to_h`
<FromGitter> <eliasjpr> no
<FromGitter> <eliasjpr> I just open a ticket
<FromGitter> <Blacksmoke16> https://github.com/amberframework/granite/pull/359 no i mean i already fixed that part
<FromGitter> <eliasjpr> I need to look into the current implementation to propose a fix
<FromGitter> <Blacksmoke16> however the `#create` or `#new` is going to be a bit more tricky
<FromGitter> <Blacksmoke16> since you cant use `Enum` in a union
<FromGitter> <eliasjpr> yeah
<FromGitter> <eliasjpr> No?
<FromGitter> <Blacksmoke16> nope
<FromGitter> <eliasjpr> You cant use an Enum in a union? WTF!
<FromGitter> <eliasjpr> Is a type no?
<FromGitter> <Blacksmoke16> not like `Enum`
<FromGitter> <Blacksmoke16> but `MyEnum` would work
<FromGitter> <Blacksmoke16> so either we can maybe remove some type restrictions on new/create to allow anything, or define some way to "register" custom types?
<FromGitter> <eliasjpr> I created an enum ⏎ ⏎ ```enum ProductTypes ⏎ Goods ⏎ Service ⏎ end``` [https://gitter.im/amberframework/amber?at=5d803efcd5c523462f96c2c4]
<FromGitter> <Blacksmoke16> yea you could use `ProductTypes` in a union, but not the base `Enum` type
<FromGitter> <Blacksmoke16> i.e. like `alias = String | Int32 | Enum`
<FromGitter> <eliasjpr> got it
<FromGitter> <eliasjpr> We should be able to push the custom type to the `Granite::ModelArgs`
<FromGitter> <eliasjpr> I would expect that each model would have a `ModelArgs` constant that we can add the custom type
<FromGitter> <Blacksmoke16> i have an idea
<FromGitter> <Blacksmoke16> i wonder if we can generate that alias based on types in the model
<FromGitter> <eliasjpr> that should would since the column is a macro
<FromGitter> <Blacksmoke16> sec
<FromGitter> <eliasjpr> Like `alias ModelArgs = Bool | ProductType` in my case?
<FromGitter> <Blacksmoke16> like i did for `#to_h`
<FromGitter> <eliasjpr> just started to look at the PR
<FromGitter> <eliasjpr> Yeah I see what you did
<FromGitter> <eliasjpr> but instead of having the fields inside the method we should be able to create a reusable constant. Wdyt?
<FromGitter> <eliasjpr> `ModelTypes = {{"Hash(String, Union(#{@type.instance_vars.select { |ivar| ivar.annotation(Granite::Column) }.map(&.type.id).splat})).new".id}}`
<FromGitter> <Blacksmoke16> hmm can you use a const as a type restriction like that?
<FromGitter> <eliasjpr> I believe is doable since is a constant (pre compilation), we can also do an alias
<FromGitter> <Blacksmoke16> https://play.crystal-lang.org/#/r/7ks5 nope
<FromGitter> <eliasjpr> https://play.crystal-lang.org/#/r/7ks8 it worked with alias
<FromGitter> <eliasjpr> that will work for me.
<FromGitter> <eliasjpr> And I like having the alias per model definition since it will allow me to create any method that takes that union alias
<FromGitter> <eliasjpr> This works too `MODEL_TYPES = Union(Int32 | String)`
<FromGitter> <Blacksmoke16> cool yea, and looks like you can define an alias per model
<FromGitter> <Blacksmoke16> let me see if i can get this working
<FromGitter> <eliasjpr> This makes me think that `(alias TYPE = Int32 | String) == (MODEL_TYPES = Union(Int32 | String)) `
<FromGitter> <Blacksmoke16> think it has to be `alias`
<FromGitter> <eliasjpr> im fine with that, what ever works :)
<FromGitter> <eliasjpr> I really like the idea of having the `MODEL_TYPES` restricted only to the types defined for that model
<FromGitter> <Blacksmoke16> i dont think i can be done
<FromGitter> <Blacksmoke16> only have access to the ivars within a method
<FromGitter> <Blacksmoke16> :/
<FromGitter> <Blacksmoke16> let me try something else
<FromGitter> <Blacksmoke16> hm
<FromGitter> <eliasjpr> we cannot define a create method like ⏎ ⏎ ```Model#create(args : ModelArgs)``` [https://gitter.im/amberframework/amber?at=5d8044d9c82c2e0d7eea8044]
<FromGitter> <Blacksmoke16> we can, but how would you define the `ModelArgs` type?
<FromGitter> <eliasjpr> within the macro
<FromGitter> <Blacksmoke16> based on ivar types?
<FromGitter> <Blacksmoke16> oh
<FromGitter> <Blacksmoke16> i have an idea
<FromGitter> <eliasjpr> we keep appending to the constant 😅 :
<FromGitter> <eliasjpr> so each model overwrites the ModelArgs constants from Granite::Base
<FromGitter> <eliasjpr> ```code paste, see link``` [https://gitter.im/amberframework/amber?at=5d8045fc05fd3716953b1603]
<FromGitter> <Blacksmoke16> should be doable
<FromGitter> <eliasjpr> yeah, it might take some thinking before approaching
<FromGitter> <Blacksmoke16> indeed
<FromGitter> <Blacksmoke16> well there's a problem
<FromGitter> <Blacksmoke16> ```code paste, see link``` [https://gitter.im/amberframework/amber?at=5d80485853bbf77ee077f4f6]
<FromGitter> <Blacksmoke16> granite supports converting string input to their types
<FromGitter> <Blacksmoke16> holy shit it worked
<FromGitter> <Blacksmoke16> Will have to think more if this would work
<FromGitter> <eliasjpr> :0
feepbot has quit [Ping timeout: 240 seconds]
feepbot has joined #amber
<FromGitter> <eliasjpr> I was able to get my stuff working with the following ⏎ ⏎ ```code paste, see link``` [https://gitter.im/amberframework/amber?at=5d80c581bda39316949929a0]
<FromGitter> <Blacksmoke16> neat
<FromGitter> <eliasjpr> I am running into a problem
<FromGitter> <eliasjpr> I cannot generate the alias because @type.instance_vars is only available at the instance level
<FromGitter> <Blacksmoke16> right, you'd have to do that mutable constant thing you were talking about
<FromGitter> <Blacksmoke16> declare it as empty array and push the type within the `column` macro
<FromGitter> <eliasjpr> we would have to convert the array into an Union after
<FromGitter> <eliasjpr> thats where I am stuck
<FromGitter> <Blacksmoke16> thats easy enough
<FromGitter> <eliasjpr> something like ⏎ ⏎ ``` macro finished ⏎ ModelArgs = {{ModelArgsBag.join(" | ").id}} ⏎ end ⏎ ``` [https://gitter.im/amberframework/amber?at=5d80df1f53bbf77ee07bfee0]
<FromGitter> <Blacksmoke16> just call `.splat` on the array
<FromGitter> <eliasjpr> sweet
<FromGitter> <Blacksmoke16> and use `Union()`
<FromGitter> <Blacksmoke16> not sure im a big fan of bringing back some mutable constants tho
<FromGitter> <Blacksmoke16> also requires the user use the macro
<FromGitter> <Blacksmoke16> the user to use the*
<FromGitter> <Blacksmoke16> would it not be doable to just remove/loosen the restrictions by default?
<FromGitter> <eliasjpr> Well the idea this constant will only mutate before compilation
<FromGitter> <eliasjpr> I mean I can live with me defining the ModelArgs alias manually
<FromGitter> <Blacksmoke16> hmm
<FromGitter> <eliasjpr> I basically created a helper module
<FromGitter> <Blacksmoke16> gotcha
<FromGitter> <eliasjpr> I would be nice to generate that alias
<FromGitter> <Blacksmoke16> i had that working
<FromGitter> <eliasjpr> show me
<FromGitter> <eliasjpr> 😅
<FromGitter> <Blacksmoke16> ```code paste, see link``` [https://gitter.im/amberframework/amber?at=5d80e56e05fd3716953f9812]
<FromGitter> <Blacksmoke16> ```code paste, see link``` [https://gitter.im/amberframework/amber?at=5d80e576d3283306ba598923]
<FromGitter> <Blacksmoke16> something like that
<FromGitter> <Blacksmoke16> always push string because of that case i put yesterday
<FromGitter> <eliasjpr> I think I am going to switch over to Clear
<FromGitter> <Blacksmoke16> oh?
<FromGitter> <eliasjpr> Yeah it seems that it has all the needed features to ease Dev
<FromGitter> <Blacksmoke16> i remember it was quite nice when i tried it a while ago
<FromGitter> <Blacksmoke16> but couldnt use annotations and serialization was less useful than granite was
<FromGitter> <Blacksmoke16> but its been a while
<FromGitter> <eliasjpr> If it wasn’t because of the enums custom types I would stick to granite
<FromGitter> <eliasjpr> But I need to come up with a poc soon and I don’t want to wait until the next release
<FromGitter> <eliasjpr> Clear has similar Api
<FromGitter> <Blacksmoke16> its less than ideal but you could just do like
<FromGitter> <Blacksmoke16> ```model = MyModel.new(val: 1, name: "string")```
<FromGitter> <Blacksmoke16> ```model.enum = MyEnum::One```
<FromGitter> <Blacksmoke16> only problem is including it within `#new` or `#create`