crossplane-contrib / function-patch-and-transform

A patch & transform composition function
https://crossplane.io
Apache License 2.0
23 stars 24 forks source link

Allow simple string replace #124

Closed applike-ss closed 1 month ago

applike-ss commented 3 months ago

As per this comment: https://github.com/crossplane/crossplane/issues/3025#issuecomment-1937911181 i assume that this is the right place to post this request.

I would like to see a simple string replacement implementation, that could be used like this:

  - type: CombineFromComposite
    combine:
      variables:
        - fromFieldPath: status.availability
        - fromFieldPath: spec.claimRef.namespace
        - fromFieldPath: spec.parameters.name
      strategy: string
      string:
        fmt: "%s.%s.%s"
        replace:
          search: 'public'
          replace: 'pub'
    toFieldPath: spec.forProvider.topic.name 

Originally taken from: https://github.com/crossplane/crossplane/issues/3026 but slightly adjusted.

The code snippet is just a rough example and may not be perfectly matching how inputs for the function should be specified.

gschei commented 1 month ago

I think the right way to solve this would be to add a string-transform of type "replace". It would follow the P&T concept of

  - type: CombineFromComposite
    combine:
      variables:
        - fromFieldPath: status.availability
        - fromFieldPath: spec.claimRef.namespace
        - fromFieldPath: spec.parameters.name
      strategy: string
      string:
        fmt: "%s.%s.%s"
    toFieldPath: spec.forProvider.topic.name 
    transforms:
      - type: string
        string:
          type: Replace
          replace:
            search: "public"
            replace: "pub"

So the replace would happen after the combine (the original ticket would also support replace to the "fromField" before the combine).

If this makes sense I would implement it.

applike-ss commented 1 month ago

Sounds logical 👍 Thanks for the feedback!

gschei commented 1 month ago

It PR was merged and this should be available in the next released version. @negz @jeanduplessis I also added documentation for it, but don't know if it makes sense, as this is now only available with the P&T composition function: https://github.com/crossplane/docs/pull/796

jeanduplessis commented 1 month ago

I think it makes sense to land that PR once the docs have been updated to be "function-first": https://github.com/crossplane/docs/issues/791

gschei commented 1 month ago

this was implemented and released with v0.7.0, issue can be closed