PDA

View Full Version : YAFR: Move a record buffer to a struct and vice-a-versa



Clive Richmond
1-Sep-2018, 12:34 AM
With the introduction of the JSON object (DataFlex 19.0) there are some nifty methods, JsontoDataType and DataTypetoJson, that transforms data from one format to another in a single command. For us personally the data type used most often is a struct or an array of struct.

I would like to see something similar for moving data between the physical, and local, record buffers into a struct and vice-a-versa. There are some implied caveats, such as a single record at a time, and perhaps not all record data types could be represented by a struct? Also, similar to the json object, this property pbRequireAllMembers should exist to skip missing columns or elements.

For anyone who has written web services you would have probably used Mike Peat’s excellent Struct Generator (https://support.dataaccess.com/Forums/showthread.php?967-Struct-Generator&p=4156#post4156) program. This was originally published back in 2006 and has certainly helped us avoid the drudgery of creating the initial struct and the 4 different methods required to move data back-n-forth. However, I can’t help thinking that 12 years on the data movement is probably something a new class and the runtime should be handling.

Nils G. Svedmyr
1-Sep-2018, 01:08 AM
I like it!

Chris Spencer
1-Sep-2018, 07:46 PM
That would be a nice addition

matthewd
3-Sep-2018, 01:11 PM
While I agree, it feels like there should be more to this. I've rolled my own solutions over time to handle the scenarios I've needed (one class does RB<->struct and DDO<->struct, one for RB->XML and DDO->XML, one for XML->DDO, one for SQL->struct) but an auto-generated generalized "mover" class to schlep data around maybe ought to handle the following:

Sources of data:


JSON
XML
Global record buffer
DDO record buffer
Struct
String array
SQL query results

Destinations:

JSON
XML
Global record buffer
DDO record buffer
Struct
String array


I've also (unfortunately) got derived structs,where I've got members for all of the fields in a regular struct that mirrors a record buffer, but added some additional members for processing/state storage while in memory. I really need to refactor these so they don't need to be updated every time a new field is added to the files...

When dealing with JSON and XML, having properties to map record buffer field names to JSON/XML names would be good. (maybe interface with a DD class that stores these field level properties?)

For bonus points, auto-generation of a struct from a record buffer should determine when I've got a series of fields with the same name but a _1, _2, _3, etc. added to the end, and create the struct with an array for those fields.

For extra bonus points, it should create a "sub struct" when you have a series of different fields that end in numbers as above, and create a sub struct array for those fields.

DaveR
3-Sep-2018, 02:37 PM
I like all of that

Clive Richmond
4-Sep-2018, 04:44 AM
Hi Matthew,

That is indeed a comprehensive solution you have created and built up.

Hopefully DAW can start with something and build on it.

starzen
4-Sep-2018, 05:25 AM
What we would need is anonymous types like pretty much all other languages have.

No need to declare structs. Just simply move a record buffer into an anonymous typed variable and use it

same works with json objects as well of course to pass these to a web service. Again no need to create any structs just simply pass the anon object and it converts into a json object



var myNewObject // anonymous typed variable
Move customer.recordbuffer to myNewObject // move record buffer to anon typed variable

showln myNewObject.Name

Michael Mullan
4-Sep-2018, 06:19 AM
Sean suggested part of this HERE (https://support.dataaccess.com/Forums/showthread.php?62730-YAFR-Option-Structs-in-fd-files&p=333809#post333809)

raveens
4-Sep-2018, 06:45 PM
+1

Clive Richmond
18-Sep-2018, 03:29 AM
What we would need is anonymous types like pretty much all other languages have.

No need to declare structs. Just simply move a record buffer into an anonymous typed variable and use it

same works with json objects as well of course to pass these to a web service. Again no need to create any structs just simply pass the anon object and it converts into a json object



var myNewObject // anonymous typed variable
Move customer.recordbuffer to myNewObject // move record buffer to anon typed variable

showln myNewObject.Name

+1