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
<FromGitter> <Blacksmoke16> idk, prob more of a question for that editor's highlighting support
Sankalp has quit [Ping timeout: 252 seconds]
Sankalp has joined #crystal-lang
<FromGitter> <lebogan> @jrei:matrix.org Sorry for the late response. I just learned how to use YAML::Serializable and you are right. It is way cleaner. Still learning...
Sankalp has quit [Ping timeout: 264 seconds]
Sankalp has joined #crystal-lang
sagax has joined #crystal-lang
notzmv has quit [Ping timeout: 264 seconds]
notzmv has joined #crystal-lang
ur5us has quit [Ping timeout: 252 seconds]
notzmv has quit [Ping timeout: 255 seconds]
Sankalp has quit [Ping timeout: 265 seconds]
Sankalp has joined #crystal-lang
ur5us has joined #crystal-lang
ur5us has quit [Ping timeout: 255 seconds]
alexherbo2 has joined #crystal-lang
alexherbo2 has quit [Remote host closed the connection]
alexherbo2 has joined #crystal-lang
alexherbo2 has quit [Remote host closed the connection]
alexherbo2 has joined #crystal-lang
jmdaemon has quit [Ping timeout: 265 seconds]
<FromGitter> <f1reflyylmao:matrix.org> When I try to use the stdlib `HTTP::Client` initialised with `"https", "api.scryfall.com", path: "cards/search", query: HTTP::Params.encode {"q" => "foo"}` cloudflare returns a `400 bad request`, but when I use the very same URI object but convert it to string before giving it to the HTTP::Client, it works like a charm. Any idea why it won't work when not converting it to string? Are there extra
<FromGitter> ... steps to it?
<FromGitter> <Blacksmoke16> prob need to add a `/` prefix to your `path`
<FromGitter> <f1reflyylmao:matrix.org> Yeah, that was the problem. Works now, thank you for the pointer!
greenbigfrog has quit [Ping timeout: 260 seconds]
greenbigfrog has joined #crystal-lang
brw has quit [Remote host closed the connection]
<wwalker> I have a string, which is valid json. I want to parse it (into `v`) and then add a field to it like this: v["custom_id"] = "#{v["uu"]}-#{v["qt"]}-#{v["ts"]}-#{v["tsns"]}" I parsed it as: v = JSON.parse(line).raw.as(Hash) but when I try to assign to a new hash key (custom_id), I get this error `Error: expected argument #2 to 'Hash(String, JSON::Any)#[]=' to be JSON::Any, not String` How can I
<wwalker> parse JSON into a datastructure of Hashes and strings and arrays with crystal?
alexherbo2 has quit [Remote host closed the connection]
alexherbo2 has joined #crystal-lang
renich has joined #crystal-lang
_ht has joined #crystal-lang
<FromGitter> <Blacksmoke16> i think you'd have to do like ` = JSON::Any.new "some_string"`
<FromGitter> <Blacksmoke16> other ways ofc include parsing it as an actual `Hash(String, String)` (assuming its a simple structure like that). Or leverage `JSON::Serializable` to deserialize it into a class that you could then call a setter on
<wwalker> I looked into Serialize, but then I would have to have a class that knew what the JSON would look like. I don't and I can't know that . I just want a structure
<FromGitter> <Blacksmoke16> okay yea, if you dont know the structure of the JSON a head of time, would have to use `JSON.parse` then
<FromGitter> <Blacksmoke16> is the data just a simple key/value obj? or includes nested stuff and such as well?
<wwalker> deeply nested, strings and numbers for keys; hashes, arrays, strings and numbers for value
<FromGitter> <Blacksmoke16> `JSON.parse` it is then 😅
<wwalker> I did that, but I need to add a value to the json, and I can't figure out how.
<FromGitter> <ober> But json.parse gives a JSON.any which is not easily cast into a malable hash
<FromGitter> <Blacksmoke16> ☝️ December 12, 2022 2:32 PM (https://gitter.im/crystal-lang/crystal?at=639781d63daaa326ba7ff8c0)
<FromGitter> <Blacksmoke16> its a bit verbose, but still totally usable
<FromGitter> <Blacksmoke16> could see about trying to have normal objects to represent it, even if you cant go directly from JSON to those objects
<FromGitter> <wwalker> given: {"a": 13, "b": "apple"} how could I modify it to be {"derived_from_a":13,"a":13, "b":"apple","derived_from_b": "apple"}
<FromGitter> <wwalker> what should I use? (I will be referencing values in sub hashes and creating values in the main hash and in sub hashes)
<FromGitter> <Blacksmoke16> https://play.crystal-lang.org/#/r/e8d5
<FromGitter> <Blacksmoke16> sounds like an overall pita
<FromGitter> <Blacksmoke16> idk what the context is, but could see about trying to have normal objects to represent it, even if you cant go directly from JSON to those objects
<FromGitter> <ober> equivalency to the ruby example above :p
<FromGitter> <Blacksmoke16> `JSON::Any` isnt really meant to be used as a working data model
<FromGitter> <ober> Sure, but .raw.as(Hash) helps no?
<FromGitter> <Blacksmoke16> working with deep nested hashes with many diff value types is also not real fun
<FromGitter> <Blacksmoke16> thats basically the same as `.as_h`
<FromGitter> <ober> in this lang :P
<FromGitter> <Blacksmoke16> but the catch is the value of the hash is expected to be `JSON::Any` so have to manually create one of those with the actual value you want to assign
<FromGitter> <wwalker> what is the 7_i32 at the end of line 5 @Blacksmoke16 ?
<FromGitter> <Blacksmoke16> can just remove the `_i32` leftover from a copy paste
<FromGitter> <wwalker> Nrever mind, makes sense
ur5us has joined #crystal-lang
<FromGitter> <wwalker> Thanks! I think I can wrap the "noise" in a method or 3 and be golden
<FromGitter> <Blacksmoke16> 👍
<wwalker> Thank you! Now I see what I have to do and it is simple.
alexherbo2 has quit [Remote host closed the connection]
alexherbo2 has joined #crystal-lang
jmdaemon has joined #crystal-lang
<FromGitter> <moe:busyloop.net> @wwalker: https://github.com/Sija/any_hash.cr may also be worth a look
<FromGitter> <moe:busyloop.net> has worked well for me for dynamically building / modifying hashes
<FromGitter> <moe:busyloop.net> oh noes, just now I see CI is failing on that repo tho, so not sure if it still works in latest crystal 🙈
<wwalker> Thank you moe. I'll look into that.
alexherbo2 has quit [Ping timeout: 260 seconds]
_ht has quit [Quit: _ht]
<Ober> can you limit how many threads are spawned? such as with a thread pool?
<FromGitter> <Blacksmoke16> you mean fibers?
<Ober> yes. items spawned via spawn. to avoid spawning one for each task, I'd like to have a fixed amount closer to ncpus.
<FromGitter> <Blacksmoke16> fibers are not the same thing as threads, so pretty sure there isnt really any harm in spawning many
<FromGitter> <Blacksmoke16> esp if the task is IO bound
<FromGitter> <Blacksmoke16> related: https://github.com/crystal-lang/crystal/issues/6468
<Ober> other than heap exhaustion. think 525k spawnings
<FromGitter> <Blacksmoke16> https://crystal-lang.org/reference/1.6/guides/concurrency.html#a-fiber seems to suggest you can spawn millions of them
<FromGitter> <moe:busyloop.net> i don't think you can limit them on a global level, but you can of course use a fiberpool at least for your own fibers
<FromGitter> <Blacksmoke16> ^ of which, there isnt anything built in to handle that. but are some shards that add some higher level concurrency features
<FromGitter> <moe:busyloop.net> it's also very easy and fun to build (if you're into that kind of thing). but yes, there are also multiple shards that do it if you just want something that works.
<Ober> thanks
<FromGitter> <moe:busyloop.net> hmmm. my little embedded node sidecar works a little bit too well. tempted to start using it for more than i should.
* FromGitter * moe:busyloop.net concerned frankenstein noises
<FromGitter> <moe:busyloop.net> https://carc.in/#/r/e8e5 - spawns a node process and let's you call anything in it from crystal
<FromGitter> <moe:busyloop.net> first only used it for that one node library that has no crystal equivalent, yet. now somewhat tempted to build me a `SlowButVeryFlexibleHash` 🥴
<FromGitter> <ober> nice
taupiqueur has quit [Ping timeout: 256 seconds]
<FromGitter> <wwalker> How do I prevent the program from exiting while there are still fibers running?
<FromGitter> <Blacksmoke16> Probably keep track of the fibers you spawned and use a channel to communicate they finished