jam01 commented on a change in pull request #4561:
URL: https://github.com/apache/camel/pull/4561#discussion_r535331423



##########
File path: 
core/camel-core-model/src/main/java/org/apache/camel/builder/DatasonnetBuilder.java
##########
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.builder;
+
+import org.apache.camel.Expression;
+import org.apache.camel.model.language.DatasonnetExpression;
+
+public class DatasonnetBuilder extends ValueBuilder {

Review comment:
       I think we based this on one of those builders. The reason comes from 
bodyMediaType and outputMediaType being both individually optional, so we could 
have any of these:
   
   ```
   // here we don't use the body, and the default output is fine
   .transform(datasonnet("hello, world", String.class))
   ```
   ```
   // here we need to be explicit about the input, maybe the content-type 
header does not apply, for example
   .transform(datasonnet("payload", 
String.class).bodyMediaType("application/json")) 
   ```
   ```
   // here we want the component to look in other locations for the input (eg. 
content-type header)
   // but we need a different output than default
   .transform(datasonnet("payload", 
String.class).outputMediaType("application/xml")))
   ```
   ```
   // here both are needed
   .transform(datasonnet("some-expression", String.class)
       .outputMediaType("application/json")
       .bodyMediaType("application/xml")))
   ```
   
   So the value is being able to be selectively explicit with the input and 
output types. Otherwise we'd have to do 
   
   ```
   // we could have a 'all defaults' type of constructor
   .transform(datasonnet("hello, world", String.class))
   ```
   ```
   .transform(datasonnet("payload", String.class, "application/json", null)) 
   ```
   ```
   .transform(datasonnet("payload", String.class, null, "application/json")) 
   ```
   ```
   .transform(datasonnet("some-expression", String.class, "application/json", 
"application/xml"))
   ```
   
   So I guess ultimately it's about readability since both options are 
functionally the same. I think a predicate is one particular scenario that 
might be confusing
   
   ```
   // if we needed to be explicit on the body, do we leave the output as null 
so the default is used?
   // or do we make it Java as it should be, either way the component forces 
Java for predicates
   // but the extra null could be confusing to devs, or misleading if they 
wrote something other than null or Java
   .when(datasonnet("payload.predicate == 'yes'", "application/json", null))
      ...
   // the output type would be ignored
   .when(datasonnet("payload.predicate == 'yes'", "application/json", 
"application/json"))
   ```
   
   I hope that expresses the value we see in the Builder. 
   
   This wasn't actually a widely discussed decision internally, I just saw 
those other Builders you mentioned and seemed like the right way to achieve 
what I described. So we're all ears :)
   
   If there's a more "camel way" to achieve this we'd be happy to refactor. Or 
if the Builder class should be moved to the datasonnet component (I think the 
XML one is) in order to move maintenance there, we can do that. Or if 
ultimately this doesn't fit with the Builder's design we can remove it.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to