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

[Bugfix]Fix the problem of "," being divided in [] #5401

Merged
merged 5 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public abstract class AbstractCommandArgs extends CommandArgs {
/** user-defined parameters */
@Parameter(
names = {"-i", "--variable"},
splitter = ParameterSplitter.class,
description = "Variable substitution, such as -i city=beijing, or -i date=20190318")
protected List<String> variables = Collections.emptyList();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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.seatunnel.core.starter.command;

import com.beust.jcommander.converters.IParameterSplitter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ParameterSplitter implements IParameterSplitter {

@Override
public List<String> split(String value) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add doc and UT for this method, and if the params contains ";" what will happen?
e.g. if the args looks like -i "map = ["par1=20230829;", "par2=20230829;"]"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add doc and UT for this method, and if the params contains ";" what will happen? e.g. if the args looks like -i "map = ["par1=20230829;", "par2=20230829;"]"

Yes, this code has a bug, I modified the implementation.PTAL

if (!value.contains(",")) {
return Collections.singletonList(value);
}

List<String> result = new ArrayList<>();
StringBuilder currentToken = new StringBuilder();
boolean insideBrackets = false;

for (char c : value.toCharArray()) {
if (c == '[') {
insideBrackets = true;
} else if (c == ']') {
insideBrackets = false;
}

if (c == ',' && !insideBrackets) {
result.add(currentToken.toString().trim());
currentToken = new StringBuilder();
} else {
currentToken.append(c);
}
}

if (currentToken.length() > 0) {
result.add(currentToken.toString().trim());
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public void testUserDefinedParamsCommand() throws URISyntaxException {
String password = "dsjr42=4wfskahdsd=w1chh";
String fakeSourceTable = "fake";
String fakeSinkTable = "sink";
String map = "[par1=20230829,par2=20230829]";
String[] args = {
"-c",
"/args/user_defined_params.conf",
Expand All @@ -54,7 +55,9 @@ public void testUserDefinedParamsCommand() throws URISyntaxException {
"-i",
"password=" + password,
"-i",
"username=" + username
"username=" + username,
"-i",
"map=" + map,
};
ClientCommandArgs clientCommandArgs =
CommandLineUtils.parse(args, new ClientCommandArgs(), "seatunnel-zeta", true);
Expand Down Expand Up @@ -88,6 +91,7 @@ public void testUserDefinedParamsCommand() throws URISyntaxException {

Assertions.assertEquals(sinkConfig.getString("username"), username);
Assertions.assertEquals(sinkConfig.getString("password"), password);
Assertions.assertEquals(sinkConfig.getString("map"), map);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@ sink {
result_table_name = ${fake_sink_table}
username = ${username}
password = ${password}
map = ${map}
}
}
}