dbriemann
2020-6-4 15:32:56

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?


badkins
2020-6-4 15:45:40

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.


dbriemann
2020-6-4 15:59:55

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?


dbriemann
2020-6-4 16:04:06

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.


samth
2020-6-4 16:12:06

The idiomatic way to handle errors is with exceptions


soegaard2
2020-6-4 16:12:09

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.


dbriemann
2020-6-4 16:12:52

Thanks. I will read a bit more about racket exceptions then next


badkins
2020-6-4 16:59:24

@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.


badkins
2020-6-4 17:01:30

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.


badkins
2020-6-4 17:04:35

@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")))


dbriemann
2020-6-4 18:46:42

@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 :))