mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-05-13 05:00:50 +00:00
Add new uncamelize lambda (#18109)
Co-authored-by: Daniel Karlsson <daniel.karlsson@cinnober.com>
This commit is contained in:
parent
41dbe51575
commit
fbe81f0735
@ -822,6 +822,7 @@ Many generators (*those extending DefaultCodegen*) come with a small set of lamb
|
|||||||
- `uppercase` - Converts all of the characters in this fragment to upper case using the rules of the `ROOT` locale.
|
- `uppercase` - Converts all of the characters in this fragment to upper case using the rules of the `ROOT` locale.
|
||||||
- `titlecase` - Converts text in a fragment to title case. For example `once upon a time` to `Once Upon A Time`.
|
- `titlecase` - Converts text in a fragment to title case. For example `once upon a time` to `Once Upon A Time`.
|
||||||
- `camelcase` - Converts text in a fragment to camelCase. For example `Input-text` to `inputText`.
|
- `camelcase` - Converts text in a fragment to camelCase. For example `Input-text` to `inputText`.
|
||||||
|
- `uncamelize` - Converts text in a fragment from camelCase or PascalCase to a string of words separated by whitespaces. For example `inputText` to `Input Text`.
|
||||||
- `indented` - Prepends 4 spaces indention from second line of a fragment on. First line will be indented by Mustache.
|
- `indented` - Prepends 4 spaces indention from second line of a fragment on. First line will be indented by Mustache.
|
||||||
- `indented_8` - Prepends 8 spaces indention from second line of a fragment on. First line will be indented by Mustache.
|
- `indented_8` - Prepends 8 spaces indention from second line of a fragment on. First line will be indented by Mustache.
|
||||||
- `indented_12` - Prepends 12 spaces indention from second line of a fragment on. First line will be indented by Mustache.
|
- `indented_12` - Prepends 12 spaces indention from second line of a fragment on. First line will be indented by Mustache.
|
||||||
|
@ -465,6 +465,7 @@ public class DefaultCodegen implements CodegenConfig {
|
|||||||
.put("kebabcase", new KebabCaseLambda())
|
.put("kebabcase", new KebabCaseLambda())
|
||||||
.put("camelcase", new CamelCaseLambda(true).generator(this))
|
.put("camelcase", new CamelCaseLambda(true).generator(this))
|
||||||
.put("pascalcase", new CamelCaseLambda(false).generator(this))
|
.put("pascalcase", new CamelCaseLambda(false).generator(this))
|
||||||
|
.put("uncamelize", new UncamelizeLambda())
|
||||||
.put("forwardslash", new ForwardSlashLambda())
|
.put("forwardslash", new ForwardSlashLambda())
|
||||||
.put("backslash", new BackSlashLambda())
|
.put("backslash", new BackSlashLambda())
|
||||||
.put("doublequote", new DoubleQuoteLambda())
|
.put("doublequote", new DoubleQuoteLambda())
|
||||||
|
@ -0,0 +1,53 @@
|
|||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
|
||||||
|
* Copyright 2018 SmartBear Software
|
||||||
|
*
|
||||||
|
* Licensed 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
|
||||||
|
*
|
||||||
|
* https://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.openapitools.codegen.templating.mustache;
|
||||||
|
|
||||||
|
import com.samskivert.mustache.Mustache;
|
||||||
|
import com.samskivert.mustache.Template;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.Writer;
|
||||||
|
|
||||||
|
import static org.openapitools.codegen.utils.StringUtils.camelize;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts text in a fragment from camelCase or PascalCase to a space separated string
|
||||||
|
*
|
||||||
|
* Register:
|
||||||
|
* <pre>
|
||||||
|
* additionalProperties.put("uncamelize", new UncamelizeLambda());
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* Use:
|
||||||
|
* <pre>
|
||||||
|
* {{#uncamelize}}{{name}}{{/uncamelize}}
|
||||||
|
* </pre>
|
||||||
|
*/
|
||||||
|
public class UncamelizeLambda implements Mustache.Lambda {
|
||||||
|
|
||||||
|
public UncamelizeLambda() {}
|
||||||
|
@Override
|
||||||
|
public void execute(Template.Fragment fragment, Writer writer) throws IOException {
|
||||||
|
String input = fragment.execute();
|
||||||
|
String text = StringUtils.capitalize(StringUtils.join(StringUtils.splitByCharacterTypeCamelCase(input.trim()), StringUtils.SPACE));
|
||||||
|
writer.write(text.trim().replaceAll(" +", " "));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,50 @@
|
|||||||
|
package org.openapitools.codegen.templating.mustache;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
|
import org.testng.annotations.BeforeMethod;
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class UncamelizeLambdaTest extends LambdaTest {
|
||||||
|
@BeforeMethod
|
||||||
|
public void setup() {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void camelCaseTest() {
|
||||||
|
// Given
|
||||||
|
Map<String, Object> ctx = context("uncamelize", new UncamelizeLambda());
|
||||||
|
|
||||||
|
// When & Then
|
||||||
|
test("Input Text", "{{#uncamelize}}inputText{{/uncamelize}}", ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void pascalCaseTest() {
|
||||||
|
// Given
|
||||||
|
Map<String, Object> ctx = context("uncamelize", new UncamelizeLambda());
|
||||||
|
|
||||||
|
// When & Then
|
||||||
|
test("Input Text", "{{#uncamelize}}InputText{{/uncamelize}}", ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void emptyStringTest() {
|
||||||
|
// Given
|
||||||
|
Map<String, Object> ctx = context("uncamelize", new UncamelizeLambda());
|
||||||
|
|
||||||
|
// When & Then
|
||||||
|
test("", "{{#uncamelize}}{{/uncamelize}}", ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void nonCamelCaseStringTest() {
|
||||||
|
// Given
|
||||||
|
Map<String, Object> ctx = context("uncamelize", new UncamelizeLambda());
|
||||||
|
|
||||||
|
// When & Then
|
||||||
|
test("Input Text", "{{#uncamelize}}Input Text{{/uncamelize}}", ctx);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user