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
pusewic|away_ has quit [Ping timeout: 264 seconds]
pusewic|away_ has joined #crystal-lang
ur5us has joined #crystal-lang
notzmv has quit [Ping timeout: 268 seconds]
<FromGitter> ... mapping.
<FromGitter> <aaaScript> Would anyone happen to know the best way to add a new key/value to a JSON::Any object? I took a look at the api but did not see a `#[]=` method. ⏎ ⏎ I also have not add much success with using `as_h` so I'm thinking about either making some sort of struct instead which would be neater, however, my JSON string is pretty complex and I would prefer to just to easily add in a new key too avoid any strict
<FromGitter> <Blacksmoke16> if the structure is known ahead of time i'd look into https://crystal-lang.org/api/master/JSON/Serializable.html
<FromGitter> <Blacksmoke16> otherwise you can do like https://play.crystal-lang.org/#/r/cbib
<FromGitter> <aaaScript> Yeah I've used Serializable before in the past but for complicated structures (that have variants) it's a bit difficult. Though I think it will be neater to work with if I do get it right.
<FromGitter> <aaaScript> Taking a look at your example: ⏎ ⏎ ```json.as_h["name"] = JSON::Any.new "Fred"``` ⏎ ⏎ This is exactly what I was trying to do but didn't occur to me to use `JSON:Any.new(string)`. [https://gitter.im/crystal-lang/crystal?at=619c58976104ea63b6adf03f]
<FromGitter> <aaaScript> I think that should work nicely. Thanks.
<FromGitter> <darkstego> Maybe there should be a #[]= method that implements that
<FromGitter> <Blacksmoke16> 👍
<FromGitter> <Blacksmoke16> mm not sure this is common enough to warrant but :shrug:
ur5us_ has joined #crystal-lang
ur5us has quit [Read error: Connection reset by peer]
ur5us_ has quit [Ping timeout: 264 seconds]
<FromGitter> <nileshtr:matrix.org> Has there been an attempt to run big Ruby libraries like ActiveRecord on Crystal as-is? This could be a step towards easy porting of Rails apps to Crystal.
notzmv has joined #crystal-lang
<FromGitter> <Blacksmoke16> Doesn't work like that
<FromGitter> <Blacksmoke16> Crystal isn't just compiled ruby. They are separate languages so changes are most likely going to be needed
<FromGitter> <nileshtr:matrix.org> I understand. I have an open-source hobby app written in Rails that I'm thinking about migrating to Crystal. Tools like this would make the migration easier.
Munto has quit [Quit: Leaving]
ur5us_ has joined #crystal-lang
ur5us_ has quit [Ping timeout: 265 seconds]
Elouin has quit [Quit: So long and thanks for all the fish!]
Elouin has joined #crystal-lang
<FromGitter> <Dan-Do> Is there any shard which convert html code `<strong>text</strong>` to markdown code `**text**`?
<FromGitter> <Dan-Do> Just search on github, there is `system reverse_markdown` function on ruby
<FromGitter> <paulocoghi> @Dan-Do , you can use the Markdown-Panda C library on Crystal, which provides HTML to Markdown and vice-versa
<FromGitter> <Blacksmoke16> @nileshtr:matrix.org yea i wouldnt bank on them existing
<FromGitter> <Blacksmoke16> but more so consider it a port to another lang. Keeping the same syntax is nice, but might also be worth rethinking the architecture of the app to make better use of crystal features
taupiqueur has joined #crystal-lang
<FromGitter> <Dan-Do> I got error `WARNING: Unsupported BIO ctrl call (76)`
<FromGitter> <Dan-Do> There is something related to openssl, don't how to fix :(
<FromGitter> <Dan-Do> Kyostya's json benchmark is now famous :)
<FromGitter> <Dan-Do> @kostya congrat
<FromGitter> <eliasjpr> Project Update - Authority OAuth 2.0 Server for Crystal - https://github.com/azutoolkit/authority Docs - https://azutopia.gitbook.io/authority/
<FromGitter> <paulocoghi> Haha, nice :)
<FromGitter> <paulocoghi> Congratulations on the new release!
<straight-shoota> @Dan-Do You could probably employ sanitize for a markdown conversion. Just need to define a custom transform policy and adapter for text output.
<FromGitter> <eliasjpr> Thank you!
avane has quit [Ping timeout: 240 seconds]
ur5us_ has joined #crystal-lang
avane has joined #crystal-lang
taupiqueur has quit [Quit: taupiqueur]
HumanG33k has quit [Ping timeout: 250 seconds]
<SamantazFox> require "json"
<SamantazFox> u = x["foo"].try &.as_a.select(&.as_s.== "u").try &.[0]
<SamantazFox> x = JSON.parse("{\"foo\":[\"a\", \"b\", \"c\", \"d\"]}")
<SamantazFox> why the hell do I get "out of bounds" exception
<FromGitter> <Blacksmoke16> Because it doesn't match anything so the array is empty
<SamantazFox> I know, but isn't `.try` supposed to catch that and give me `nil`?
<FromGitter> <oprypin:matrix.org> SamantazFox, i'm making a wild guess but does ruby return `nil` if `select` produced 0 results?
<SamantazFox> idk, I almost never coded in Ruby xD
<FromGitter> <oprypin:matrix.org> well anyway crystal doesnt return `nil` in that case
<FromGitter> <Blacksmoke16> `#try` doesnt yield if what you call it on is `nil`
<FromGitter> <Blacksmoke16> so in the end you're doing like `Array(String).new[0]`
<oprypin> both of your `.try` actually have no effect
<oprypin> oops wrong window D:
<FromGitter> <Blacksmoke16> ^ `#[]` raises if missing so it wouldnt actually ever be `nil`
<FromGitter> <oprypin:matrix.org> your code is same as `u = x["foo"].as_a.select(&.as_s.== "u")[0]`
ur5us_ has quit [Ping timeout: 264 seconds]
<SamantazFox> yeah, there is a missing `?` after `x["foo"]`
<FromGitter> <oprypin:matrix.org> 1) x["foo"] raises or returns a non-null value ⏎ 2) array.select always returns an array. even if it's empty doesn't matter, `try` only reacts to `nil`
<SamantazFox> how then can I select in a way that I'm ensured `nil` rather than `[]`?
<FromGitter> <Blacksmoke16> `#find` maybe?
<FromGitter> <Blacksmoke16> assuming you only care about the first match
<FromGitter> <oprypin:matrix.org> `u = x["foo"].as_a.find(&.as_s.== "u")`
<SamantazFox> wonderful :D
<SamantazFox> thanks y'all :)
<SamantazFox> Also, quick other question: is there a reason why `.dig?` doesn't work on `nil`s?
<FromGitter> <Blacksmoke16> kinda related i had the idea of adding `#presence` to Array (or maybe one of the modules) that also check `#empty?` and not `nil`
<FromGitter> <Blacksmoke16> probably the same reason `#+` doesn't
<FromGitter> <Blacksmoke16> or any other method
<SamantazFox> I mean, `my_json.dig?("foo", "bar")` works, but why `my_json.dig?("foo").dig?("bar")` wouldn't?
<FromGitter> <Blacksmoke16> because how would it work if the first dig returns `nil`? this is a case where `#try` comes into play
HumanG33k has joined #crystal-lang
<FromGitter> <Blacksmoke16> `my_json.dig?("foo").try &.dig?("bar")`
<SamantazFox> I really hate having `.try` every single time I access sub-elements...
<FromGitter> <oprypin:matrix.org> SamantazFox, `dig` is a method only on the `JSON::Any` type. the `Nil` type doesn't have such a method
<FromGitter> <Blacksmoke16> `JSON::Any` is a pita if you haven't noticed, surprised to see you havent gave up on it and moved to serializable or something yet
<FromGitter> <Blacksmoke16> assuming your data is structured and known ahead of time
<FromGitter> <oprypin:matrix.org> SamantazFox, right-- JSON+Crystal is tailored towards using https://crystal-lang.org/api/master/JSON/Serializable.html
<SamantazFox> We have no other choice xD
<FromGitter> <Blacksmoke16> how so?
<SamantazFox> Youtube gives huge JSON files.
<FromGitter> <Blacksmoke16> so?
<SamantazFox> they evolve all the time
<FromGitter> <Blacksmoke16> fair, if the structure is fluid then not much you can do in that regard i guess then :/
<FromGitter> <oprypin:matrix.org> SamantazFox, you dont literally have to use `try`... ⏎ there's also this option ⏎ ⏎ ```if (u = x["foo"]?) ⏎ u.as_a.find(&.as_s.== "u") ⏎ end``` [https://gitter.im/crystal-lang/crystal?at=619d78e6c6149e534947abca]
<FromGitter> <oprypin:matrix.org> also... `my_json.dig?("foo").try &.dig?("bar")` - that's better written as what I'm pretty sure is equivalent: `my_json.dig?("foo", "bar")`
<FromGitter> <Blacksmoke16> ^
<SamantazFox> oprypin: yeah, I alternate, but when you have stuff like that, well, that's not that easy
<SamantazFox> (I can't even paste the file in a pastebin, it's too large xD)
<SamantazFox> https://bpa.st/A4PQ
<SamantazFox> Just to give you an idea, here's 2% of it, as bpaste doesn't accept more that 256kB
<FromGitter> <Blacksmoke16> couldnt you have some sort of transformation layer that handles the brunt of that, while still allowing you to work with objects
<SamantazFox> What do you mean?
<FromGitter> <Blacksmoke16> i.e. consume input JSON and output standardized objects. I imagine the end data is stable/common and will just need to keep the input transformation logic up to date which i imagine would be a lot easier than using `JSON::Any` throughout the codebase
<SamantazFox> Oh, yeah, that's what we do, mostly
<FromGitter> <Blacksmoke16> gotcha
<SamantazFox> like, 80% of it is just dumped after we got the reply, parsed it, and extracted the data we needed.
<SamantazFox> but the major problem is at "getting the data we need"
<FromGitter> <Blacksmoke16> cons of working with a private API i suppose :P
<SamantazFox> yeah xD
<SamantazFox> but sadly, this is what it needs to provide a better platform to watch youtube videos, while excaping the grabby hands of big G