ChanServ changed the topic of #crystal-lang to: The Crystal programming language | https://crystal-lang.org | Fund Crystal's development: https://crystal-lang.org/sponsors | GH: https://github.com/crystal-lang/crystal | Docs: https://crystal-lang.org/docs | Gitter: https://gitter.im/crystal-lang/crystal
_whitelogger has joined #crystal-lang
_whitelogger has joined #crystal-lang
<FromGitter> <emanzx> Hi guys..
<FromGitter> <emanzx> I have problem getting around this error
<FromGitter> <emanzx> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=60a6059b77c281623c683ad6]
<FromGitter> <emanzx> this is my code
<FromGitter> <emanzx> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=60a605cae4173528c6949506]
<FromGitter> <naqvis> 🙈
<FromGitter> <naqvis> i would say you need to think about your design
<FromGitter> <naqvis> more simple option would be to use `JSON::Any` as value, as that almost covers your value types
<FromGitter> <naqvis> but to fix your problem, you would have to separate your declaration with all possible cases of types you are expecting to put into your hash
<FromGitter> <naqvis> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=60a60ac224587f67f885a823]
<FromGitter> <naqvis> as you can see the pain
<FromGitter> <emanzx> okay.
<FromGitter> <emanzx> slowly getting it.
<FromGitter> <emanzx> if I wanna use JSON::Any as my value
<FromGitter> <emanzx> how to do it?
<FromGitter> <naqvis> ```mon_data = Hash(String, JSON::Any).new ⏎ mon_data["foo"] = JSON::Any.new(-1i64)``` [https://gitter.im/crystal-lang/crystal?at=60a60e85e2f43259f6628dfa]
<FromGitter> <naqvis> ```code paste, see link``` ⏎ ⏎ ugly, but will work for your use case [https://gitter.im/crystal-lang/crystal?at=60a6100782b87324a0d8e163]
<FromGitter> <naqvis> quick and dirty approach to convert `Hash` to `JSON::Any` would be, convert that to json and then invoke `JSON.parse` ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=60a6107971431f7a08ce505f]
<FromGitter> <naqvis> but you start seeing the code smell, which shows design or choice of Hash as data structure isn't the right option
willamin has quit [Ping timeout: 250 seconds]
willamin has joined #crystal-lang
<FromGitter> <emanzx> noted.
<FromGitter> <emanzx> thanks @naqvis
<FromGitter> <naqvis> 👍
woodruffw has quit [Ping timeout: 252 seconds]
woodruffw has joined #crystal-lang
hendursaga has quit [Ping timeout: 240 seconds]
hendursaga has joined #crystal-lang
sorcus has joined #crystal-lang
<FromGitter> <emanzx> Im getting this error
<FromGitter> <emanzx> {"port":80,"keyword":"","hostname":"mail.beriani_kerna_benar.com","protocol":"ping","destination":"1.2.3.4","record_id":12,"ref_id":"hb_9_12","tries":3} ⏎ {"port" => 80, "keyword" => "", "hostname" => "mail.beriani_kerna_benar.com", "protocol" => "ping", "destination" => "1.2.3.4", "record_id" => 12, "ref_id" => "hb_9_12", "tries" => 3} ⏎ W, [2021-05-20 09:24:58 UTC #95577] WARN -- faktory.cr: FAIL
<FromGitter> ... ce832cc54f1a1d17ad4d81c0: {"message":"cast from Int64 to String failed, at /usr/local/lib/crystal/json/any.cr:215:5:215","errtype":"#<TypeCastError:cast from Int64 to String failed, at /usr/local/lib/crystal/json/any.cr:215:5:215>","jid":"ce832cc54f1a1d17ad4d81c0","backtrace":["/usr/local/lib/crystal/json/any.cr:214:3 in 'as_s'","Work ... [https://gitter.im/crystal-lang/crystal?at=60a62b3c77c281623c689f4e]
<FromGitter> <emanzx> "destination" => "1.2.3.4", is a string.
<FromGitter> <emanzx> why crystal say it Int64
fifth has quit [Quit: WeeChat 3.1]
<straight-shoota> Are you sure the error is from accessing `destination`?
<straight-shoota> It is clearly a string in the Crystal output
<jhass> We gotta see WorkerMon.cr:122
<FromGitter> <emanzx> sorry.. was focusing on my code.. save the trouble by serializing the JSON.. haha.. first time I serialize my JSON.. dont know it was better that way
<FromGitter> <emanzx> anyway, I have problem ⏎ ⏎ `````` [https://gitter.im/crystal-lang/crystal?at=60a6521f02556214c0da10eb]
<FromGitter> <emanzx> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=60a6526f2af92c7a1fe8136c]
<FromGitter> <emanzx> Granite is complaining that it unable to process my data.
<FromGitter> <emanzx> this is my model ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=60a652d424587f67f88667d6]
<FromGitter> <emanzx> ```code paste, see link```
hendursaga has quit [Write error: Connection reset by peer]
hendursaga has joined #crystal-lang
<FromGitter> <Blacksmoke16> @emanzx use `.new`, `.save`, then `pp model`
<FromGitter> <Blacksmoke16> and check what the `errors` ivar says
HumanG33k has joined #crystal-lang
HumanG33k has quit [Remote host closed the connection]
HumanG33k has joined #crystal-lang
sorcus has quit [Ping timeout: 245 seconds]
HumanG33k has quit [Max SendQ exceeded]
<FromGitter> <emanzx> ok.
<FromGitter> <emanzx> now that shown some light
<FromGitter> <emanzx> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=60a670380bc2c759f4ceaf25]
<FromGitter> <Blacksmoke16> dont you mean `JSON::Any`?
<FromGitter> <emanzx> column extra : JSON::Any?
<FromGitter> <Blacksmoke16> yea
<FromGitter> <emanzx> oh..
<FromGitter> <emanzx> compiled error ⏎ ⏎ `````` [https://gitter.im/crystal-lang/crystal?at=60a670d96fe44c28cf243187]
deavmi has left #crystal-lang ["P A R T E D"]
<FromGitter> <emanzx> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=60a670e308746124895d273a]
<FromGitter> <Blacksmoke16> ah, prob would have to use a converter, so on write it does `.to_json` and stores as string, on read it dose `JSON.parse` to get it back into `JSON::Any`
<FromGitter> <emanzx> ok.. any doc I should refer too..
<FromGitter> <emanzx> I actually need write only..
<FromGitter> <Blacksmoke16> theres already a json convert built in, just need to use it
<FromGitter> <emanzx> in granite?
<FromGitter> <Blacksmoke16> yes
<FromGitter> <emanzx> `Json(M, T)` - Converters an `Object` of type `M` to/from a database column of type `T.` Supported types for `T` are: `String`, `JSON::Any`, and `Bytes`.
<FromGitter> <Blacksmoke16> ah, i think this built in one is mainly for like `SomeObj.from_json`
<FromGitter> <Blacksmoke16> versus supporting the `JSON::Any` type. Could define your own tho :shrug:
<FromGitter> <emanzx> trying to understand how to use the converter
<FromGitter> <emanzx> any tutorial I can look into..
<FromGitter> <Blacksmoke16> how to use them, or how to make one?
<FromGitter> <emanzx> yes
<FromGitter> <emanzx> enum OrderStatus ⏎ Active ⏎ Expired ⏎ Completed ⏎ end [https://gitter.im/crystal-lang/crystal?at=60a673036fe44c28cf243760]
<FromGitter> <emanzx> this is for enum
<FromGitter> <emanzx> I need to create a class?
<FromGitter> <Blacksmoke16> do you want to make a custom converter, or use one of the built in ones?
<FromGitter> <emanzx> sorry.. trying to digest.
<FromGitter> <emanzx> for my situation I only need to save the JSON to the field..
<FromGitter> <emanzx> I dont need to read any..
<FromGitter> <emanzx> so using this is enough?
<FromGitter> <emanzx> Json(M, T)
<FromGitter> <Blacksmoke16> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=60a673d702556214c0da6e38]
<FromGitter> <Blacksmoke16> ```code paste, see link``` ⏎ ⏎ better handle nil values [https://gitter.im/crystal-lang/crystal?at=60a6740ee2f43259f6638b59]
<FromGitter> <emanzx> for this one? ⏎ ⏎ `````` [https://gitter.im/crystal-lang/crystal?at=60a674856fe44c28cf243a9a]
<FromGitter> <emanzx> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=60a6748c82b87324a0d9efd7]
<FromGitter> <Blacksmoke16> yes
<FromGitter> <emanzx> noted
<FromGitter> <emanzx> I will look into it.
xybre is now known as xybre-libera
<FromGitter> <emanzx> I dont need to convert the extra to_json anymore right? ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=60a6775ae2f43259f6639527]
<FromGitter> <Blacksmoke16> shouldnt need to no
<FromGitter> <Blacksmoke16> is it nil?
<FromGitter> <emanzx> ```code paste, see link```
<FromGitter> <emanzx> should be not nill
<FromGitter> <emanzx> need to convert to string first?
<FromGitter> <Blacksmoke16> thats what the converter should be doing yes
<FromGitter> <Blacksmoke16> maybe add some `pp` to ensure it's running/going thru the right path
<FromGitter> <emanzx> ok let see
<FromGitter> <emanzx> hmm.. ⏎ ⏎ `````` [https://gitter.im/crystal-lang/crystal?at=60a679f827b35e5b426c00ed]
<FromGitter> <emanzx> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=60a67a060b9bbf5b32f05c8b]
<FromGitter> <Blacksmoke16> might want to change your password now if thats not just a local thing
<FromGitter> <Blacksmoke16> but it looks like it's being converted to a string correctly, what's it being read from the db as/
<FromGitter> <emanzx> ah shiit..
<FromGitter> <emanzx> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=60a67b51e4173528c695c14a]
<FromGitter> <emanzx> is this correct?
<FromGitter> <Blacksmoke16> looks like it yea, what happens if you add a `pp str` and then like `pp "failed to parse"` before returning `nil` in `from_rs`
<FromGitter> <Blacksmoke16> i guess one thing i should also ask, what type os the `extra` column in the db?
<FromGitter> <emanzx> \d heartbeat_data ⏎ ⏎ ``` Table "public.heartbeat_data"``` ⏎ ⏎ Column | Type | Collation | Nullable | Default ... [https://gitter.im/crystal-lang/crystal?at=60a67bb724587f67f886d1d0]
<FromGitter> <Blacksmoke16> oh, so its `jsonb` not string
<FromGitter> <emanzx> should be jsonb wrong type?
<FromGitter> <emanzx> yes
<FromGitter> <Blacksmoke16> postgres?
<FromGitter> <emanzx> yes
<FromGitter> <emanzx> no other way?
<FromGitter> <Blacksmoke16> i think you can just do like `result.read JSON::Any?`
<FromGitter> <Blacksmoke16> instead of string
<FromGitter> <Blacksmoke16> because the pg driver knows about that type, but granite doesnt
<FromGitter> <emanzx> ```code paste, see link``` ⏎ ⏎ like this? [https://gitter.im/crystal-lang/crystal?at=60a67cab82b87324a0da03ae]
<FromGitter> <Blacksmoke16> no, literally just `result.read JSON::Any`
<FromGitter> <emanzx> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=60a67cf208746124895d446a]
<FromGitter> <Blacksmoke16> yup
<FromGitter> <emanzx> ok noted.
<FromGitter> <emanzx> I still getting the cannot be nil error
<FromGitter> <Blacksmoke16> welp, im out of ideas
<FromGitter> <emanzx> hmm.. the weird thing is.. it does not display the pp value
<FromGitter> <emanzx> @Blacksmoke16 if I set the field type to String it wont work?
<FromGitter> <Blacksmoke16> on the model? idt because the data from the db isnt a string
<FromGitter> <Blacksmoke16> ill try and reproduce and see if i can figure it out
<FromGitter> <emanzx> ok.
<FromGitter> <emanzx> do you want snapshot of my code?
<FromGitter> <Blacksmoke16> i should have enough. already provided schema, model, and converter
<FromGitter> <emanzx> ok
<FromGitter> <emanzx> I would prefer if I can use JSONB in postgress.. because of indexing and casting of JSON data if not I need to change to String and just save the JSON string as String in DB
<FromGitter> <emanzx> or ditch the granite and use crystal-pg instead?
<FromGitter> <Blacksmoke16> if you care you wont be using an ORM at that piont
<FromGitter> <Blacksmoke16> dont care*
<FromGitter> <emanzx> I just need to save to DB only..
<FromGitter> <Blacksmoke16> never read/query?
<FromGitter> <emanzx> nope
<FromGitter> <Blacksmoke16> if so then yea, prob could just do that
<FromGitter> <emanzx> Ok I will look into that
<FromGitter> <emanzx> @Blacksmoke16 how can I pass variable into a struct?
<FromGitter> <Blacksmoke16> hm?
<FromGitter> <emanzx> I create my own class to save to DB
<FromGitter> <emanzx> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=60a68b4f0bc2c759f4cef23e]
<FromGitter> <Blacksmoke16> looks fine yea
<FromGitter> <Blacksmoke16> could do ⏎ ⏎ `def initialize(@db_url : String); end`
<FromGitter> <emanzx> and here is the work definition
<FromGitter> <emanzx> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=60a68b8e19c86162394b9ffe]
<FromGitter> <Blacksmoke16> whoa
<FromGitter> <emanzx> Im going to call the Tsdb inside the struct
<FromGitter> <Blacksmoke16> https://devblast.com/b/what-are-guard-clauses you should look into using these :)
<FromGitter> <Blacksmoke16> does it even need to be a class? versus just like a module with a private const to store the URL and some class methods for doing the stuff?
<FromGitter> <emanzx> actually my coding level still not surpass Class or other type apart from simple function using def
<FromGitter> <emanzx> so this is the first time im using class or others
<FromGitter> <emanzx> well mostly I follow tutorial..
<FromGitter> <Blacksmoke16> guard clause is basically doing this
<FromGitter> <Blacksmoke16> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=60a68d5227b35e5b426c3455]
<FromGitter> <Blacksmoke16> instead of having nested `if/else`
<FromGitter> <emanzx> oh..
<FromGitter> <emanzx> sometimes I like to use the puts "hahaha" if true
<FromGitter> <Blacksmoke16> why?
<FromGitter> <emanzx> so this guard clause is usable in function only right?
<FromGitter> <emanzx> if in main it will still works?
<FromGitter> <Blacksmoke16> you cant return from the top level so no
<FromGitter> <Blacksmoke16> but the majority of your code is prob going to be in methods so shouldnt be a problem
<FromGitter> <emanzx> okay noted.
HumanG33k has joined #crystal-lang
<FromGitter> <emanzx> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=60a690d230651c67e81ca6d8]
<FromGitter> <emanzx> clean abit
<FromGitter> <emanzx> haha
<FromGitter> <Blacksmoke16> yes
<FromGitter> <emanzx> btw.. trying to understand this ⏎ ⏎ > does it even need to be a class? versus just like a module with a private const to store the URL and some class methods for doing the stuff?
_whitelogger has joined #crystal-lang
<FromGitter> <Blacksmoke16> ```code paste, see link``` ⏎ ⏎ is what i was thinking [https://gitter.im/crystal-lang/crystal?at=60a6919d19c86162394baefa]
<FromGitter> <emanzx> yes..
<FromGitter> <emanzx> bear with me.. haha..
<FromGitter> <Blacksmoke16> still could use the module, just have lazily load the url i guess
<FromGitter> <emanzx> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=60a69231e7464514b593c727]
<FromGitter> <Blacksmoke16> id look into https://crystal-lang.org/api/master/YAML/Serializable.html
<FromGitter> <emanzx> if I use module so I can use it in struct?
<FromGitter> <Blacksmoke16> i was thinking a module because it's not likely you're going to be newing up more than 1 of them with different values
<FromGitter> <emanzx> I have many info inside the yml.. and some may not need in this code
<FromGitter> <emanzx> can I serialize somepart of it?
<FromGitter> <Blacksmoke16> should be possible to only deserialize the parts you need yea
<FromGitter> <emanzx> I will do that.. today is the first time I do serialize for my json data.. I dont know it was so good
<FromGitter> <emanzx> haha
<FromGitter> <emanzx> > i was thinking a module because it's not likely you're going to be newing up more than 1 of them with different values ⏎ ⏎ for the tsdb.. yes it wont change
<FromGitter> <emanzx> should I be doing to_json? ⏎ ⏎ ```Tsdb.create Time.utc, "node", true, "abc123", JSON.parse("1")``` [https://gitter.im/crystal-lang/crystal?at=60a693f819c86162394bb4f4]
<FromGitter> <Blacksmoke16> idk, code you posed earlier was typed to `JSON::Any`,
<FromGitter> <Blacksmoke16> `.to_json` would return a `String`
<FromGitter> <emanzx> oh
void09 has joined #crystal-lang
<FromGitter> <emanzx> I wanted to save it to JSONB type.
<FromGitter> <emanzx> so it should be in JSON right?
<FromGitter> <Blacksmoke16> prob want `JSON::Any` then
<void09> I'm thinking of hiring someone to build me a custom ecommerce platform, from scratch. I know someone that can do it for a reasonable price in ruby. But obviously not the fastest language out there.. Syntax of ruby is almost identical, i know, but the gems don't work out of the box
<void09> Anyone know attemps to build something like this with crystal ?
<void09> And how good of an idea would it be to choose crystal for such a project
<FromGitter> <emanzx> I would not say Syntax of ruby is identical.. the basic is identical.. but for most part I dont think it optimized
<FromGitter> <emanzx> I still having problem migrating to crystal myself
<FromGitter> <emanzx> hahaha
<FromGitter> <Blacksmoke16> yea, its impt to remember just because the syntax looks the same doesnt mean they're the same lang
<void09> well, that's what the person said, looking at crystal code, that it's no problem for him. But the problem is, can't use rubygems
<void09> they'd have to be converted, and some can be pretty large. Is there an attempt to rewrite ruby gems to shards ?
<FromGitter> <Blacksmoke16> what im getting at is just because the code looks familiar to him doesn't mean it'll be easy to learn/use/port things
<FromGitter> <Blacksmoke16> as its not as simple as 1:1 porting
deavmi has joined #crystal-lang
<deavmi> Is this room on oftc of libera.chat?
<FromGitter> <Blacksmoke16> there is not a goal to port all gems to shards no
<FromGitter> <Blacksmoke16> would have to see whats available and use one of them, or port what you need over/start from scratch
<FromGitter> <Blacksmoke16> whats gems in particular are you needing?
<void09> No idea, I just know he uses lots of them to code
<void09> I could ask
<FromGitter> <Blacksmoke16> thats one of the cons of crystal atm. Its a younger lang so the ecosystem isn't as large
<void09> so no ecommerce attempts ? even rudimentary ones?
_whitelogger has joined #crystal-lang
<void09> I found about 5 of them in elixir/phoenix, but all abandoned :(
<void09> still better than 0 in crystal.. i wonder why no one bothered?
<FromGitter> <Blacksmoke16> prob same reason those other 5 are abandoned :P just takes time/effort
<void09> well, elixir seems to be 3 years younger than crystal
<void09> faker, tinymce-rails,roo,activerecord-import,wicked_pdf,bcrypt - most needed gems
<xybre-libera> void09: bcrypt is in the stdlib
<FromGitter> <Blacksmoke16> there's a faker lib iirc
<FromGitter> <Blacksmoke16> AR import one is prob not doable given AR doesnt exist really
<xybre-libera> void09: there's also xlsx-parser
<void09> https://github.com/FartingNicoNyanCats/ecommerced/blob/dev/src/main.cr - found ecommerce attempt :d 1 file, 140 lines
<FromGitter> <Blacksmoke16> oof
<FromGitter> <Blacksmoke16> using tabs
<void09> Yeah, Elixir seems to have a reasonable amount of popularity, but it's functional, not oop, and I'm no expert here, but isn't that suboptimal for writing a huge oop-organic kind of project like an ecommerce CMS ?
<xybre-libera> I like how Elixir is so state averse that in order to keep a counter you have to spawn a thread and communicate with it via messages
sorcus has joined #crystal-lang
<FromGitter> <emanzx> I change my data type in db into string or varchar
<FromGitter> <emanzx> and I keep getting this error
<FromGitter> <emanzx> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=60a6aa56e4173528c6963c26]
<FromGitter> <Blacksmoke16> 😬
<FromGitter> <Blacksmoke16> rip
sagax has quit [Ping timeout: 240 seconds]
postmodern has joined #crystal-lang
<postmodern> what type should I use to represent bytes (ASCII or UTF8)? Crystal stdlib seems to use both UInt8 and Int32
<Stephie> bytes are by definition UInt8
<Stephie> UTF-8 is multiple bytes
<Stephie> if you want to store text, use Char/String
<Stephie> if you want binary which requires binary parsing/decoding, thats best done using UInt8/Slice(UInt8)
<postmodern> if you want to store whole bytes, or ordinal values, in a Array, what type would you use?
<FromGitter> <tenebrousedge> Array(UInt8), Slice(UInt8), or StaticArray(UInt8)
<postmodern> but that wouldn't store an ordinal (aka numeric) value of a UTF8 character?
<FromGitter> <tenebrousedge> if you want `Array(Char)` that's another thing
<FromGitter> <tenebrousedge> you're asking about byte values though
<FromGitter> <tenebrousedge> what are you actually doing?
<postmodern> storing the ordinal values of chars (both ASCII and potentially multi-byte UTF8)