stampit-org/stamp-specification

Allow metadata (descriptors) merging override

koresar opened this issue · 6 comments

My mental model of stamps is following:

  • The compose() (and stamp.compose()) function is just the metadata collection functions. It does nothing else but collecting metadata.
  • Using the collected metadata user can create objects by calling the stamp().

Thus, the metadata collection is the corner stone of the stamps philosophy. We should not limit people on how they merge metadata.

I'm thinking of allowing people to overwrite the mergeComposable similar to the initializers way. In other words - if user specified a merging callback function and it returned non-undefined then use the returned value as a new descriptor. (Will provide code examples later.)

Be careful not to over-complicate things...

Make sure you answer "is this really needed?" =)

I'm yet to need this in JS. Although, it might be useful for other languages.

This idea needs a deep thought though.

I cannot think of a use case that could not be solved by init

yes. Either init or infected compose.

I'm just thinking of other languages. It could be useful there. Could not. :)

Code example as promised:

const WrappedMergeStamp = compose({
  mergeDescriptor(dstDescriptor, srcDescriptor) {
    // my own algorithm to merge metadata.
    console.log(src);
    // Can wrap original compose.
    return Object.assign({}, compose(dst, src).compose);
  }
});

WrappedMergeStamp.compose({ properties: { a: 1 } }).compose({ methods: { b() {} } });

Will print:

{ properties: { a: 1 } }
{ methods: { b: [Function: b] } }

Again. I don't see JS need that given that we have infected compose.
But other languages are not as flexible (e.g. Java or C#).

This idea was continued with the #109 proposal.
Closing.