-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement user defined delimiter for value format (#3393)
This PR implements configurable delimiters for value formats.
- Loading branch information
Showing
38 changed files
with
717 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
ksql-common/src/main/java/io/confluent/ksql/serde/Delimiter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Copyright 2019 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (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.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.serde; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import com.google.errorprone.annotations.Immutable; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
@Immutable | ||
public final class Delimiter { | ||
|
||
private static final Map<String, Character> NAMED_DELIMITERS = ImmutableMap | ||
.<String, Character>builder() | ||
.put("TAB", '\t') | ||
.put("SPACE", ' ') | ||
.build(); | ||
|
||
private static final String NAMED_DELIMITERS_STRING = | ||
StringUtils.join(NAMED_DELIMITERS.keySet(), ","); | ||
|
||
private final char delimiter; | ||
|
||
public static Delimiter of(final char ch) { | ||
return new Delimiter(ch); | ||
} | ||
|
||
private Delimiter(final char delimiter) { | ||
this.delimiter = delimiter; | ||
} | ||
|
||
public static Delimiter of(final String str) { | ||
if (str == null) { | ||
throw new NullPointerException(); | ||
} | ||
if (str.trim().isEmpty()) { | ||
throw new IllegalArgumentException( | ||
"Delimiter cannot be empty, if you meant to have a tab or space for delimiter, please " | ||
+ "use the special values 'TAB' or 'SPACE'" | ||
+ System.lineSeparator() | ||
+ "Example valid value: ';'" | ||
); | ||
} | ||
if (str.length() == 1) { | ||
return new Delimiter(str.charAt(0)); | ||
} | ||
final Character delim = NAMED_DELIMITERS.get(str); | ||
if (delim != null) { | ||
return new Delimiter(delim); | ||
} | ||
throw new IllegalArgumentException( | ||
"Invalid delimiter value: '" + str | ||
+ "'. Delimiter must be a single character or " | ||
+ NAMED_DELIMITERS_STRING | ||
+ System.lineSeparator() | ||
+ "Example valid value: ';'" | ||
); | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
final Delimiter delimiter1 = (Delimiter) o; | ||
return delimiter == delimiter1.delimiter; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(delimiter); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.valueOf(delimiter); | ||
} | ||
|
||
public char getDelimiter() { | ||
return delimiter; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.