libcyaml icon indicating copy to clipboard operation
libcyaml copied to clipboard

Add support for default values for some of the CYAML types

Open dawoun opened this issue 2 years ago • 2 comments

  • Support default values (with new macros) for integer, float, boolean, enum and bitfield fields
  • Can work with code compiled without those updates without rebuilding said code
  • Existing code can be updated to allow default values support simply by including "cyaml_ex.h" instead of "cyaml.h"
  • Some new tests for the new functionality

dawoun avatar May 09 '22 10:05 dawoun

Hi @dawoun,

Thank you for your interest in LibCYAML.

I have a plan for default values where the default value will always be provided via a pointer. This will allow even structures and arrays to have default values.

The approach taken here is very different to what I want to do, however it is interesting to see.

tlsa avatar May 15 '22 14:05 tlsa

Hi @dawoun,

Thank you for your interest in LibCYAML.

I have a plan for default values where the default value will always be provided via a pointer. This will allow even structures and arrays to have default values.

The approach taken here is very different to what I want to do, however it is interesting to see.

I think my approach can be fairly easily expanded to use pointers, except it's not possible then to provide a value as the default value in a macro (i.e, CYAML_FIELD_INT_DEFAULT(7, ...) will not work because you can't take a pointer to the number. I considered it - it would have made copying the data into the value much easier - but seeing that there aren't any registration for whole structures, and that for strings and structures you can use NULL pointer as indicating they aren't in the yaml hence should be filled with defaults, I think the PR handles all the cases.

Furthermore, it doesn't change the API for any existing app, and can be easily applied by including cyaml_ex.h wherever cyaml.h was previously included.

dawoun avatar May 15 '22 17:05 dawoun

Hi @dawoun,

Thank you for this proposal. I have decided to use the alternative approach in https://github.com/tlsa/libcyaml/pull/212 instead, so I am closing this.

This a very clever approach, but I did not want to maintain the complexity, that would only increase as other optional features are added, such as validation callback functions.

tlsa avatar May 29 '23 13:05 tlsa