What would be the proper way to convert a hash
, fetched from a json rest api into a self defined struct? How would i best assert that the values to the corresponding keys in the hash are well-defined? E.g. I have a hash, which should contain an id field which is a number. Would I then just somehow test if the key id
exists and if so would check if it is a number? Or are there some more sophisticated methods of doing this?
I’m not sure what you’re referring to with “self defined struct”, but when I obtain a hash (either from an api or a form submission to a web app), I define a function that validates the hash (which makes use of a number of pre-defined standard validators such as requiring a field to exist, matching a regex, a date w/in a range, etc.). The validation function returns a list of errors w/ an empty list indicating success. If the hash is successfully validated I pass it to a constructor to create the struct. The constructor can then assume the hash has everything required to successfully create the struct.
OK thx that makes sense. What I meant by self defined struct was just a struct type I defined such as (struct item (id time type parent kids ....))
. If you say return errors this brings up another question. Do you return errors as values or do you use exceptions? Is there an idiomatic way in racket to handle errors?
Sorry if these questions sound strange but I learned some racket back at university about 10 years ago (i think it was called plt scheme).. and didn’t really use it since. I just decided to write a real gui application in racket and I am just working on getting into it again :slightly_smiling_face: It is kind of a challenge to overcome my programming habits from other (procedural/oop) languages and get back into functional programming.
The idiomatic way to handle errors is with exceptions
It depends on the context. In most cases errors are signaled using an exception. In the case where faulty data are expected (and not it isn’t considered a “critical error”) then it makes sense to something else - for example return a description of the errors.
Thanks. I will read a bit more about racket exceptions then next
@dbriemann I return errors as a list of strings because I’ll need to report those errors to either a human or a log, etc. I suppose an exception could be raised that contains a list of strings, but I don’t see the value of that.
Philosophically, I don’t consider expected error conditions to be exceptions. For example, a user forgetting to enter a field is not an exception to me.
@dbriemann since you mentioned “functional programming”, a pattern such as the following might be useful, and possibly instead of a simple if
you may have a more complicated function call: (filter identity (list
(if (non-empty-string? ftp-userid) #f "FTP Userid is required")
(if (non-empty-string? ftp-password) #f "FTP Password is required")))
@badkins thanks for the tips. Yes I remember filter.. I see the point about returning errors. In my case an exception will do fine. The errors I was talking about are not really expected. E.g. if the client queries the API but times out or gets some http error code. I was mainly asking because these days I professionally write go for over 4 years and there errors are handled very explicitly via return values (there are also no exceptions really :))