Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Enhance Kamelet data type implementations #1489

Merged
merged 1 commit into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/yaks-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ jobs:
yaks run test/aws-ddb-sink $YAKS_RUN_OPTIONS
yaks run test/aws-s3 $YAKS_RUN_OPTIONS

yaks run test/avro-data-type $YAKS_RUN_OPTIONS
yaks run test/avro-serdes-action $YAKS_RUN_OPTIONS

yaks run test/extract-field-action $YAKS_RUN_OPTIONS
yaks run test/insert-field-action $YAKS_RUN_OPTIONS

Expand Down
22 changes: 9 additions & 13 deletions kamelets/avro-deserialize-action.kamelet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ spec:
title: "Avro Deserialize Action"
description: "Deserialize payload to Avro"
type: object
required:
- schema
properties:
schema:
title: Schema
Expand All @@ -54,23 +52,21 @@ spec:
- "camel:core"
- "camel:jackson-avro"
template:
beans:
- name: schemaResolver
type: "#class:org.apache.camel.kamelets.utils.format.converter.avro.AvroSchemaResolver"
property:
- key: validate
value: '{{validate}}'
- key: schema
value: '{{schema:}}'
from:
uri: kamelet:source
steps:
- set-property:
name: schema
constant: "{{schema}}"
- set-property:
name: validate
constant: "{{validate}}"
- unmarshal:
avro:
library: Jackson
unmarshalType: com.fasterxml.jackson.databind.JsonNode
schemaResolver: "#class:org.apache.camel.kamelets.utils.serialization.InflightAvroSchemaResolver"
- remove-property:
name: schema
- remove-property:
name: validate
schemaResolver: "#bean:{{schemaResolver}}"
- remove-header:
name: "Content-Type"
22 changes: 9 additions & 13 deletions kamelets/avro-serialize-action.kamelet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ spec:
title: "Avro Serialize Action"
description: "Serialize payload to Avro"
type: object
required:
- schema
properties:
schema:
title: Schema
Expand All @@ -54,24 +52,22 @@ spec:
- "camel:core"
- "camel:jackson-avro"
template:
beans:
- name: schemaResolver
type: "#class:org.apache.camel.kamelets.utils.format.converter.avro.AvroSchemaResolver"
property:
- key: validate
value: '{{validate}}'
- key: schema
value: '{{schema:}}'
from:
uri: kamelet:source
steps:
- set-property:
name: schema
constant: "{{schema}}"
- set-property:
name: validate
constant: "{{validate}}"
- marshal:
avro:
library: Jackson
unmarshalType: com.fasterxml.jackson.databind.JsonNode
schemaResolver: "#class:org.apache.camel.kamelets.utils.serialization.InflightAvroSchemaResolver"
- remove-property:
name: schema
- remove-property:
name: validate
schemaResolver: "#bean:{{schemaResolver}}"
- set-header:
name: "Content-Type"
constant: "application/avro"
15 changes: 7 additions & 8 deletions kamelets/protobuf-deserialize-action.kamelet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ spec:
title: "Protobuf Deserialize Action"
description: "Deserialize payload to Protobuf"
type: object
required:
- schema
properties:
schema:
title: Schema
Expand All @@ -46,18 +44,19 @@ spec:
- "camel:core"
- "camel:jackson-protobuf"
template:
beans:
- name: schemaResolver
type: "#class:org.apache.camel.kamelets.utils.format.converter.protobuf.ProtobufSchemaResolver"
property:
- key: schema
value: '{{schema:}}'
from:
uri: kamelet:source
steps:
- set-property:
name: schema
constant: "{{schema}}"
- unmarshal:
protobuf:
library: Jackson
unmarshalType: com.fasterxml.jackson.databind.JsonNode
schemaResolver: "#class:org.apache.camel.kamelets.utils.serialization.InflightProtobufSchemaResolver"
- remove-property:
name: schema
schemaResolver: "#bean:{{schemaResolver}}"
- remove-header:
name: "Content-Type"
15 changes: 7 additions & 8 deletions kamelets/protobuf-serialize-action.kamelet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ spec:
title: "Protobuf Serialize Action"
description: "Serialize payload to Protobuf"
type: object
required:
- schema
properties:
schema:
title: Schema
Expand All @@ -46,19 +44,20 @@ spec:
- "camel:core"
- "camel:jackson-protobuf"
template:
beans:
- name: schemaResolver
type: "#class:org.apache.camel.kamelets.utils.format.converter.protobuf.ProtobufSchemaResolver"
property:
- key: schema
value: '{{schema:}}'
from:
uri: kamelet:source
steps:
- set-property:
name: schema
constant: "{{schema}}"
- marshal:
protobuf:
library: Jackson
unmarshalType: com.fasterxml.jackson.databind.JsonNode
schemaResolver: "#class:org.apache.camel.kamelets.utils.serialization.InflightProtobufSchemaResolver"
- remove-property:
name: schema
schemaResolver: "#bean:{{schemaResolver}}"
- set-header:
name: "Content-Type"
constant: "application/protobuf"
76 changes: 76 additions & 0 deletions kamelets/resolve-pojo-schema-action.kamelet.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# ---------------------------------------------------------------------------
# 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.
# ---------------------------------------------------------------------------

apiVersion: camel.apache.org/v1alpha1
kind: Kamelet
metadata:
name: resolve-pojo-schema-action
annotations:
camel.apache.org/kamelet.support.level: "Stable"
camel.apache.org/catalog.version: "4.0.0-SNAPSHOT"
camel.apache.org/kamelet.icon: "data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+CjxzdmcKICAgeG1sbnM6ZGM9Imh0dHA6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TU0LC03MC41IDI2LC01MCA0MSwtOTggbCAxODMsLTI4IHEgMTMsLTIgMjEsLTEyLjUgOCwtMTAuNSA4LC0yMy41IHoiCiAgICAgICBpZD0icGF0aDMwMjkiCiAgICAgICBpbmtzY2FwZTpjb25uZWN0b3ItY3VydmF0dXJlPSIwIgogICAgICAgc3R5bGU9ImZpbGw6Y3VycmVudENvbG9yIiAvPgogIDwvZz4KPC9zdmc+Cg=="
camel.apache.org/provider: "Apache Software Foundation"
camel.apache.org/kamelet.group: "Actions"
camel.apache.org/kamelet.namespace: "Transformation"
labels:
camel.apache.org/kamelet.type: "action"
spec:
definition:
title: "Resolve Schema Action"
description: "Resolves schema from given mime type and payload. Sets the resolved schema, the schema type and its content class as properties for later reference."
type: object
properties:
mimeType:
title: Mime Type
description: The mime type to determine the schema resolver implementation that should perform the operation.
type: string
default: "application/json"
example: "application/json"
schema:
title: Schema
description: Optional schema content (as single-line, using JSON format).
type: string
contentClass:
title: Content Class
description: Type information of the content object. Fully qualified class name.
type: string
example: "org.apache.camel.content.Foo"
targetMimeType:
title: Target Mime Type
description: Additional mime type information used to determine the schema resolver. Usually only used in combination with mime type "application/x-java-object"
type: string
example: "application/json"
dependencies:
- "mvn:org.apache.camel.kamelets:camel-kamelets-utils:4.0.0-SNAPSHOT"
- "camel:kamelet"
- "camel:core"
- "camel:jackson-avro"
- "camel:jackson-protobuf"
template:
beans:
- name: schemaResolver
type: "#class:org.apache.camel.kamelets.utils.format.schema.DelegatingSchemaResolver"
properties:
mimeType: '{{mimeType}}'
schema: '{{schema:}}'
contentClass: '{{contentClass:}}'
targetMimeType: '{{targetMimeType:}}'
from:
uri: "kamelet:source"
steps:
- process:
ref: "{{schemaResolver}}"
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@
import org.apache.camel.CamelContext;
import org.apache.camel.CamelContextAware;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.kamelets.utils.format.converter.standard.BinaryDataType;
import org.apache.camel.kamelets.utils.format.converter.standard.JsonModelDataType;
import org.apache.camel.kamelets.utils.format.converter.standard.StringDataType;
import org.apache.camel.kamelets.utils.format.converter.bytes.ByteArrayDataType;
import org.apache.camel.kamelets.utils.format.converter.text.StringDataType;
import org.apache.camel.kamelets.utils.format.spi.DataTypeConverter;
import org.apache.camel.kamelets.utils.format.spi.DataTypeConverterResolver;
import org.apache.camel.kamelets.utils.format.spi.DataTypeLoader;
Expand Down Expand Up @@ -105,9 +104,8 @@ protected void doInit() throws Exception {
if (classpathScan) {
dataTypeLoaders.add(new AnnotationDataTypeLoader());
} else if (useDefaultConverters) {
addDataTypeConverter(new BinaryDataType());
addDataTypeConverter(new ByteArrayDataType());
addDataTypeConverter(new StringDataType());
addDataTypeConverter(new JsonModelDataType());
}

for (DataTypeLoader loader : dataTypeLoaders) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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.kamelets.utils.format;

import java.util.Objects;

public enum MimeType {
JSON("application/json"),
PROTOBUF("application/protobuf"),
AVRO("application/avro"),
AVRO_BINARY("avro/binary"),
AVRO_STRUCT("avro/x-struct"),
BINARY("application/octet-stream"),
TEXT("text/plain"),
JAVA_OBJECT("application/x-java-object"),
STRUCT("application/x-struct");

private static final MimeType[] VALUES = values();
private final String type;

MimeType(String type) {
this.type = type;
}

public String type() {
return type;
}

public static MimeType of(String type) {
for (MimeType mt : VALUES) {
if (Objects.equals(type, mt.type)) {
return mt;
}
}

throw new IllegalArgumentException("Unsupported type: " + type);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.kamelets.utils.format;

import java.util.Arrays;
import java.util.Objects;

/**
* Supported schema type for Java object serialization/deserialization
*/
public enum SchemaType {
PROTOBUF("protobuf"),
AVRO("avsc"),
JSON("json");

private static final SchemaType[] VALUES = values();

private final String schemaType;

SchemaType(String type) {
this.schemaType = type;
}

public String type() {
return schemaType;
}

public static SchemaType of(String type) {
return Arrays.stream(VALUES)
.filter(s -> Objects.equals(s.schemaType, type))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException(String.format("Unsupported schema type '%s'", type)));
}
}
Loading
Loading