Support function object types in Ocl #132
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Similar to HCL support for Functions and Function Calls
Usage
OCL Syntax
In OCL itself it is represented in the same way that a function would be called in most programing languages.
All functions are uniquely named and support none or many arguments which can themselves be of any parseable ocl expression type.
When building a custom function, be mindful that unlike how HCL may be used in places like Terraform, functions in OCL should where possible be reversable. That is, once an object is deserialized with a function, it should ideally be serialized back into a format with the same value. This may require only supporting being mapped to certain object types, or by relying on additional context from the serialization process. From a technical standpoint you may not want or need to support this, but it should be kept in mind.
Defining a Custom Function
By default, only the
Base64DecodeFunctionCall
is included out-of-the box however .Implement the
IFunctionCall
interface.This interface defines two methods:
object? ToValue(IEnumerable<object?> arguments)
- Called during deserialization when converting from the OCL representation to a single property value. The function may contain none or many arguments.IEnumerable<object?> ToOclFunctionCall(object propertyValue)
- Called during serialization and allows for a single object value to be represented by the function call as being defined with non or many arguments.Pass an instance of this custom function class into the
OclSerializer
via theOclSerializerOptions
(e.g).As an example see the Base64DecodeFunctionCall class
OclFunctionAttribute
To allow for two-way conversion of properties, a new
OclFunction
attribute exists that allows for annotating properties that, when serialized, should take place through a function.An example from the included tests
results in a serialized OCL document with two properties