<FromGitter>
<mattrberry> Totally a niche thought, but has there been any consideration on adding support for functions like `r[]`, eg `def r[](idx : Int)` or `def r[]=(idx : Int, val : UInt32)`
<FromGitter>
<mattrberry> I would think that it wouldn't be a huge addition in the compiler due to the way I understand existing [] and []= methods are implemented, but I could be totally wrong
<FromGitter>
<3n-k1> @mattrberry how would that be used? if it's something like `obj.r[idx]`, you could just have a property `r` that implements the `#[]` method
<FromGitter>
<mattrberry> I'll state again that it's very niche and not even necessary in those niche use cases, but here's my current use case: ⏎ I have a CPU class that has a list of registers on it, eg ⏎ ⏎ ```class CPU ⏎ getter r : Slice(UInt32) = Slice.new 16``` ⏎ ... [https://gitter.im/crystal-lang/crystal?at=5fbb483fabf6a739a6aceac3]
<FromGitter>
<3n-k1> core maintainers give us hygienic macros challenge ⏎ /s
<FromGitter>
<3n-k1> allow us to create our *own* syntax uwu
<FromGitter>
<3n-k1> ~~what'
<FromGitter>
<3n-k1> ~~what's the worst that could happen~~
_whitelogger has joined #crystal-lang
<jhass>
@mattrberry: just return a thin wrapper struct for r? `record @cpu : CPU do def []=(...) @cpu.@r[..] = ...; end end`
<jhass>
in your proposal `a.b[c] = d` would be ambigous to be either `a.b.[]=(c, d)` or `a.b[]=(c, d)`
<oprypin>
yes it'd basically turn Crystal into an undecidable grammar (communicating state from semantic back to the parser is a must) so probably not good, and the struct solution is indeed just how it should be done. if you check, maybe it has 0 performance effect. or maybe not. then just use r_get() 😅
incognitorobito has joined #crystal-lang
incognitorobito has quit [Ping timeout: 245 seconds]
psydroid has quit [Quit: Bridge terminating on SIGTERM]
return0e[m] has quit [Quit: Bridge terminating on SIGTERM]
erdnaxeli has quit [Quit: Bridge terminating on SIGTERM]
ryanprior has quit [Quit: Bridge terminating on SIGTERM]
<yxhuvud>
no, it is perfectly normal that typeof(x/y) == typeof(x) forall x.
<FromGitter>
<j8r> I don't know
<yxhuvud>
hmm. no, that is not correct. but it seems to always propagate to float and never to int, at least
<FromGitter>
<j8r> the definition of floor division is ⏎ ⏎ > the floor function is the function that takes as input a real number x {\displaystyle x} x, and gives as output the greatest integer less than or equal to x {\displaystyle x} x
[gnubie] has joined #crystal-lang
* [gnubie]
waves
<FromGitter>
<j8r> so, the greatest integer, logically should be of a `Int` type, not Float
<[gnubie]>
on how can i add "data" with key=value pair when the CLIENT.post is executed
<[gnubie]>
in the html form, it only asked for username and password.. now, i want to add data automatically added when CLIENT.post
<[gnubie]>
if i translate the code snippet into a curl command, it will look like this: curl -k -d username=${username} -d password=${password} -X POST https://apidomain.com/api/path
<[gnubie]>
now, i want to add another "data" automatically
<[gnubie]>
the html form will still retain asking the user with username and password. but once the user clicks the button, additional data will be added in the POST part
<[gnubie]>
i've been searching similar objectives but i can't find one. i am trying to understand the crystal api reference but i still can't figure out
<[gnubie]>
i'm getting an error: undefined local variable or method 'username' for top-level
<FromGitter>
<Daniel-Worrall> where are they coming from?
<FromGitter>
<Daniel-Worrall> `params = HTTP::Params.parse(context.request.body.not_nil!.gets_to_end)` and then `params["username"]`
<[gnubie]>
i'm sorry Daniel-Worrall. yes, i'm seeing that line but what do you mean?
<FromGitter>
<Dan-Do> if the username comes from CLI, then use `"#{ARGV[1]}"`
<FromGitter>
<Dan-Do> > *<[gnubie]>* i'm sorry Daniel-Worrall. yes, i'm seeing that line but what do you mean? ⏎ ⏎ ```CLIENT.post("/api/path", form: {"username" => "#{params["username"]}, "password" => "#{params["password"]}", "client" => "curl", "scope" => "all"})```
avane has quit [Ping timeout: 240 seconds]
<[gnubie]>
thanks Dan-Do! it worked!
<[gnubie]>
i'm getting excited here. i'm getting a json response. now, i want to parse the output in such a way that i will only get 2 k/v results out of 9 because only 2 k/v is what i'm interested to know.
<[gnubie]>
currently in my shell script using curl, i use jq to pipe out the output
<[gnubie]>
basically my jq command is: | jq -r '.token,.address'
avane has joined #crystal-lang
<FromGitter>
<Daniel-Worrall> well how are you outputting it?
<[gnubie]>
the output is json
<FromGitter>
<Daniel-Worrall> Oh yeah, you're just serving it in a html body
<[gnubie]>
i want to filter the json output in such a way that only 2 x k/v will be presented on the web browser
<FromGitter>
<Daniel-Worrall> You can `json = JSON.parse(obj); "#{json["first_key"]}, #{json["second_key"]}"`
<FromGitter>
<Daniel-Worrall> or you could define the json struct for serialising and just not include the other keys so it ignores them in parsing
<[gnubie]>
is it on the block of the CLIENT.post or on the when "/submit" block?
<FromGitter>
<Daniel-Worrall> you want to parse your response from your post
<[gnubie]>
yes, i guess it should be outside
<[gnubie]>
hi Daniel-Worrall and Dan-Do. thank you for the help. but i need to go now. i will be back later. thanks again! :)
* [gnubie]
wishes there will be a book on crystal for newbies
[gnubie] has quit [Quit: Leaving]
<FromGitter>
<Daniel-Worrall> Have you read the Reference?
<kreyren>
Referencing https://github.com/iv-org/invidious/issues/1421 brainstorm appreciated.. i am trying to figure out how to handle scenario where crystal is deployed as a website that expects variable storing value based on deployed environment
<kreyren>
namely this is using `HOST_URI` that is expected to change based on website deployed either on clearweb or as hidden service
<kreyren>
where my current fix would be figure out what domain is being used to deploy the page and then interpret that in if statement to return HOST_URI?
<kreyren>
the other option would be looking for onion-specific value somewhere around the page which is janky >.>
<kreyren>
i was ideally thinking about something like `if domain is '.*\.onion' then return #{hidden_service} to store in HOST_URI
<FromGitter>
<Blacksmoke16> And env vars wouldn't help?
<jhass>
just ditch that in favour of the HTTP Host header and make sure your reverse proxy is always setting it correctly (so overriding any client supplied value)
<jhass>
alternatively any other (custom) HTTP header set by the reverse proxy, but why if there's already one exactly for this purpose
<kreyren>
Blacksmoke16, i don't know how would env var help assuming that this is not a constant
<kreyren>
jhass, HTTP Host header? That is clear on hidden services i think
<kreyren>
lemmeh check
<FromGitter>
<Blacksmoke16> Ah I was under the assumption that one running instance would be one or the other. But it sounds like it should be able to handle both yea?
<kreyren>
jhass, the HTTP header doesn't seem to be a viable option during the website initiation
<kreyren>
Blacksmoke16, this is instance running on port e.g. 6912 that is providing the website to the clearweb, but the port is also mirrored to provide hidden_service
<kreyren>
where the issue is that hidden_service refers to clearweb when it shoudn't e.g. login is not possible
<jhass>
looking at https://manpages.debian.org/stretch/tor/torrc.5.en.html you could configure your hidden service to a different local port, have the reverse proxy listen to that and override the Host header or whatever for this case only
<jhass>
Well, if you want your application be flexible in how it generates URL based on how it's accessed, you obviously cannot make this static global configuration
<jhass>
it has to be per request
<kreyren>
jhass, How could i do that in crystal then?
<kreyren>
or what is your proposal in this case?
<jhass>
to move this setting onto a per request basis
<kreyren>
there is no request though.. the hidden service is available as a website that routes the POST/GET to the server or what do you mean?
<jhass>
"flexible value per request" and "global constant value" are obivously contradictionary requirements
<jhass>
It's a webapp, of course there's a HTTP request/response cycle
<kreyren>
Like i think i understand what do you propose, but i don't see how that would be possible through crystal since it seems to need those hardcodded to expand for the HOST_URL that expects domain.tld
<kreyren>
so basically if i can get domain.tld and dngjkasdng.onion in HOST_URI based on where the website is deployed that would solve the issue
<jhass>
you're confusing invidious application structure with "Crystal"
<jhass>
also just scroll down in the file you linked and notice the "before_all" hook which obviously runs for every request, wherever it is coming from
<kreyren>
checking
<jhass>
(I guess before_all is a kemal thing)
<kreyren>
How would that help in determining where the website is deployed to store value in HOST_URI though?
<kreyren>
or do you propose grabbing the HTTP Host header during that phase?
<jhass>
You cannot use HOST_URI
<jhass>
you have to replace it with a mechanism that works per request
<straight-shoota>
what's HOST_URI anyway? AFAIK it's neither a kemal nor invidious thing
<kreyren>
That shoudn't be a problem for me, but i don't understand the solution that you are proposing
<jhass>
a standard Crystal constant
<jhass>
straight-shoota: ^
<kreyren>
HOST_URI is being used in the webapp to replace the domain.tld for links e.g. the generated RSS feed needs full name
<straight-shoota>
Which webapp? I understood the discussion is about invidious? Can't find it there.
<kreyren>
that is used all over the file to define `href` paths for example
<straight-shoota>
Okay, so individous has a constant for configuring its external hostname
<kreyren>
yep it has `domain` property in config that sets the HOST_URI variable
<jhass>
which obviously is a flawed approach when there's more than one valid hostname
<straight-shoota>
yepp
<kreyren>
FWIW i was told that upstream didn't expect invidious being popular choice for hidden service so they set it up as hardcoded
<jhass>
when upstream isn't interested to support this usecase it might become a quite hard to maintain patchset over time. It might be easier to just run two instances configured to the same DB (if any) locally
<kreyren>
jhass, upstream is interested in supporting this configuration
<straight-shoota>
IMO it's always a bad idea to make external host a static config value. For most web applications it shouldn't be necessary at all.
<kreyren>
jhass, running two instances is not an option for management reasons
<kreyren>
straight-shoota, true, but it supports running with `domain` property set as blank so i think i could expand login in that regard.. I don't know how to make the expression though
<kreyren>
e.g. it should expand in invidious.snopyta.org on clearweb and in djdangjkangk.onion as hidden_service
<kreyren>
(hidden url scrambled)
<straight-shoota>
As jhass already mentioned, best solution is just to use the host header
<straight-shoota>
no need for global configuration
<straight-shoota>
Assuming reverse proxy is properly configured, you can trust that the value in the host header is what's available to the client
<straight-shoota>
But I assume your issue is about cross-referencing between different hosts, right?
<straight-shoota>
Or maybe not...
<kreyren>
depends the webapp expects HOST_URI to be set depending on environment before it loads So i guess host header might be an option in before_all hook?
<kreyren>
trying atm
<straight-shoota>
I'm not sure what you need a before hook for to use that header
<straight-shoota>
just use it in the request handler
<straight-shoota>
instead of HOST_URL
<straight-shoota>
HOST_URL needs to go. You can't have dynamic configuration with a constant. Replace every use of HOST_URL with a dynamic resolution based on host header
<kreyren>
straight-shoota, it can't go because the webapp has features like RSS feed that require the value of HOST_URL to expand in valid feed
<jhass>
an RSS feed is still fetched within a HTTP request/response cycle
<kreyren>
or like how do you make sure that it generates correct paths assuming that things like RSS feed can't work with relative paths
<jhass>
use the host header used to fetch the feed
<kreyren>
The RSS feed has things like ` <link rel="self" href="/feed/channel/UCVls1GmFKf6WlTraIb_IaJg"/>` that needs to have domain in front of the `/feed` though
<kreyren>
else the RSS feed is broken
<straight-shoota>
So just use the host header: `<link rel="self" href="#{request.host}/feed/channel/UCVls1GmFKf6WlTraIb_IaJg"/>`
<kreyren>
is #{request.host} set by default?
* kreyren
wants to see if that fixes it in snopyta's environment
<straight-shoota>
it's set to Host header
<straight-shoota>
just note the code I posted was shortened for illustration
<jhass>
which comes without a scheme so you need to prepend http:// or https://, likely dependent on the X-Forwarded-Proto header or such
<jhass>
probably worth to add a little helper to determine the URI prefix from the Host and proto headers
<jhass>
straight-shoota: idk, the more "sophisticated" the stdlib solution the easier somebody can fool it because the reverse proxy doesn't filter all
<kreyren>
straight-shoota, using curl -v the header for host is set
<jhass>
maybe the reverse proxy or tor map thingy or whatever is in the pipeline filters it?
<kreyren>
possible i guess
<kreyren>
dunno how to verify as that is snopyta's config that i can request info from on demand
<kreyren>
quotting: """nginx should forward the host `proxy_set_header Host $http_host;`"""
<jhass>
probably worth to get a full dev setup locally for this tbh. So you can also locally curl each part of the pipeline explicitly
<jhass>
and hack debug prints into stdlib, kemal and what not
<straight-shoota>
or write specs where you can exactly control the request data
<kreyren>
jhass, i am working on dev setup atm but i want to make sure that my solution works on snopyta
<straight-shoota>
make sure that it runs in dev first
<straight-shoota>
The production environment needs to provide you the requested host name somehow. If it doesn't it must be reconfigured.
deimos_ has joined #crystal-lang
<kreyren>
How can i get shards on debian?
<kreyren>
or are they part of crystal now?
<straight-shoota>
shards is typically distributed with the crystal package
<FromGitter>
<Blacksmoke16> i really like the idea of keeping the docs with the code. I feel its easy for dedicated external docs to get outdated
<oprypin>
the thing that it does very nicely is that the textual docs are the king, and you can freely intersperse API definitions into it
<oprypin>
well u can keep it in the same repo. and you really should; i think it's a mistake whenever people split it
<FromGitter>
<Blacksmoke16> even if you have to go to another dir to update docs on a method i would think thats easy to forget
<FromGitter>
<Blacksmoke16> i did/have plans to, do more with the `wiki` page
<FromGitter>
<Blacksmoke16> like use that for general documentation, like how things are architected and such that don't really need updated too often. then keep API docs for more of the usage of things
<FromGitter>
<Blacksmoke16> :shrug:
<FromGitter>
<Blacksmoke16> but im happy to see you're checking it out 😉 got a project in mind or something?
<oprypin>
Blacksmoke16, with that attitude you're gonna end up writing an entire book with its source file having to be prefixed with `# ` in its entirety ಠ_ಠ
<oprypin>
yea i have a project. i'll publish it if it works out
<FromGitter>
<Blacksmoke16> 🙈 ha
<FromGitter>
<Blacksmoke16> nice, any questions/issues etc feel free to ping me
<oprypin>
ah but the api docs themselves seem stellar
<FromGitter>
<Blacksmoke16> ❤️
<FromGitter>
<j8r> I'm incrementing a x value to a given step float, until y. ⏎ I have ideas, but you, how would you do to know when y is reached or exceeded?
<FromGitter>
<j8r> all values can be negative or positive, and mixed
<oprypin>
j8r, ?? `0.0.step(by: 0.2, to: 100.0) do |x|`
<oprypin>
i dont get it, how do i parse if a http request returns body `error=bad_verification_code&error_description=The+code+passed+is+incorrect+or+expired.&error_uri=blabla` ? seems like there's only the block version of https://crystal-lang.org/api/0.35.1/HTTP/FormData.html#build(response:HTTP::Server::Response,boundary=MIME::Multipart.generate_boundary,&)-class-method ?
<oprypin>
straight-shoota, thanks :>
<oprypin>
i have so much pressure to finish all my various projects xD
<straight-shoota>
seems familiar...
<FromGitter>
<j8r> question: in your opinion, what's the sign of `Float32::NAN`?
<straight-shoota>
oprypin, you want to *parse* a body, so you should be using FormData.parse ?
<FromGitter>
<j8r> `Float32::NAN.sign #=> 1`- a bug?
<straight-shoota>
hm, how can something have a sign when it's not even a number...?
<oprypin>
j8r, what should it be, an exception?
<oprypin>
straight-shoota, yea i want to parse but just get a Hash
<FromGitter>
<j8r> at least, `0` maybe
<straight-shoota>
IMO exception would be best
<straight-shoota>
any value other than `1` or `-1` would be a total screw up
<FromGitter>
<j8r> #<=> and #sign are quite close
<_ht>
If I overload "initialize" how can I call another initialize method in my overloaded one?
<FromGitter>
<j8r> and `#<=>` returns nil, 0, 1, or -1
<straight-shoota>
`number.sign * some_value` is a typical use case for `#sign`
<straight-shoota>
it's not related to `#<=>` in any way
<FromGitter>
<j8r> it can
<FromGitter>
<j8r> because it checks nan values, and sign is basically `self <=> 0` no?
<straight-shoota>
_ht, you shouldn't
<_ht>
Why not? I'd like to have one initialize with a block parameter and one without.
<_ht>
That is not possible / common in Crystal?
<oprypin>
j8r, fyi this is why it's positive
<oprypin>
>> [1f32, -1f32, Float32::NAN].each { |y| x = y; p "%032b %f" % {pointerof(x).as(UInt32*).value, x} }
<FromGitter>
<Blacksmoke16> and going back to the db thing, i suppose a more robust solution would be to have a like `DBFactory` or `DBProvider` or something that you can inject as needed. It could be a singleton (since DB handles checking out connections), and provide an API to yield a connection.
<FromGitter>
<Blacksmoke16> but :shrug: depends on how you want to go about it
<FromGitter>
<christopherzimmerman> Is there a way to use the `FILE` type from C in bindings? Thought it might be exposed via LibC, but guess not.
<oprypin>
chachasmooth, Void* if you really need
<oprypin>
ideally dont do that tho
<FromGitter>
<christopherzimmerman> I guess that could work if I needed to receive a file handler, I think I need to be able to pass a file handler to a library. Not sure if that's possible right now.
<oprypin>
Blacksmoke16, (lazy question), so i see that athena controller methods can return an object. is there any way to add a wrapper around all of them? to accept that object and further respond