Skip to content

Commit 7dac7cc

Browse files
committed
Correctly name key mappers - rename "Snake"CaseKeyMapper
* The "snake case" key mapper was really kebab case. * Add a real snake case key mapper, but clarify it as LowerSnakeCaseKeyMapper to prevent silently breaking API users.
1 parent 738d8ac commit 7dac7cc

12 files changed

Lines changed: 176 additions & 53 deletions

File tree

backend-hocon/src/main/java/space/arim/dazzleconf/backend/hocon/HoconBackend.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
import space.arim.dazzleconf.backend.DataTree;
4141
import space.arim.dazzleconf.backend.KeyMapper;
4242
import space.arim.dazzleconf.backend.ReadableRoot;
43-
import space.arim.dazzleconf.backend.SnakeCaseKeyMapper;
43+
import space.arim.dazzleconf.backend.KebabCaseKeyMapper;
4444
import space.arim.dazzleconf.engine.CommentLocation;
4545
import space.arim.dazzleconf.internals.lang.LibraryLang;
4646

@@ -247,7 +247,7 @@ private ConfigValue entryToHocon(DataEntry entry) {
247247

248248
@Override
249249
public @NonNull KeyMapper recommendKeyMapper() {
250-
return new SnakeCaseKeyMapper();
250+
return new KebabCaseKeyMapper();
251251
}
252252

253253
@Override

backend-toml/src/main/java/space/arim/dazzleconf/backend/toml/TomlBackend.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
import space.arim.dazzleconf.backend.KeyMapper;
4848
import space.arim.dazzleconf.backend.KeyPath;
4949
import space.arim.dazzleconf.backend.ReadableRoot;
50-
import space.arim.dazzleconf.backend.SnakeCaseKeyMapper;
50+
import space.arim.dazzleconf.backend.KebabCaseKeyMapper;
5151
import space.arim.dazzleconf.engine.CommentLocation;
5252
import space.arim.dazzleconf.internals.lang.LibraryLang;
5353

@@ -324,7 +324,7 @@ private static CommentLocation commentLocationFrom(CommentPosition position) {
324324

325325
@Override
326326
public @NonNull KeyMapper recommendKeyMapper() {
327-
return new SnakeCaseKeyMapper();
327+
return new KebabCaseKeyMapper();
328328
}
329329

330330
@Override

backend-yaml/src/main/java/space/arim/dazzleconf/backend/yaml/ReadEvents.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
import space.arim.dazzleconf.backend.DataEntry;
6060
import space.arim.dazzleconf.backend.DataList;
6161
import space.arim.dazzleconf.backend.DataTree;
62-
import space.arim.dazzleconf.backend.SnakeCaseKeyMapper;
62+
import space.arim.dazzleconf.backend.KebabCaseKeyMapper;
6363
import space.arim.dazzleconf.internals.lang.LibraryLang;
6464

6565
import java.util.ArrayList;
@@ -147,7 +147,7 @@ private void consumeEventId(Event.ID eventId) {
147147
if (!parser.checkEvent(eventId)) {
148148
// We can re-use SnakeCaseKeyMapper to build readable event names
149149
// For example, StreamStart -> stream-start -> stream start
150-
String displayName = new SnakeCaseKeyMapper().labelToKey(eventId.name()).toString();
150+
String displayName = new KebabCaseKeyMapper().labelToKey(eventId.name()).toString();
151151
String message = "Expected " + displayName.replace('-', ' ');
152152
ErrorContext error = context.errorSource.buildError(preBuilt(message));
153153
throw context.throwError(error, null);

backend-yaml/src/main/java/space/arim/dazzleconf/backend/yaml/YamlBackend.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
import space.arim.dazzleconf.backend.CommentData;
5050
import space.arim.dazzleconf.backend.KeyMapper;
5151
import space.arim.dazzleconf.backend.ReadableRoot;
52-
import space.arim.dazzleconf.backend.SnakeCaseKeyMapper;
52+
import space.arim.dazzleconf.backend.KebabCaseKeyMapper;
5353
import space.arim.dazzleconf.engine.CommentLocation;
5454
import space.arim.dazzleconf.internals.lang.LibraryLang;
5555

@@ -328,7 +328,7 @@ private static void writeComments(Writer writer, List<String> lines) throws IOEx
328328

329329
@Override
330330
public @NonNull KeyMapper recommendKeyMapper() {
331-
return new SnakeCaseKeyMapper();
331+
return new KebabCaseKeyMapper();
332332
}
333333

334334
@Override

core/src/main/java/space/arim/dazzleconf/backend/SnakeCaseKeyMapper.java renamed to core/src/main/java/space/arim/dazzleconf/backend/CamelCaseMap.java

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,11 @@
1919

2020
package space.arim.dazzleconf.backend;
2121

22-
import org.checkerframework.checker.nullness.qual.NonNull;
22+
final class CamelCaseMap {
2323

24-
/**
25-
* A key mapper which converts lower camel case patterns to lower snake case.
26-
* <p>
27-
* This is intended for use with YAML and TOML and other formats that use lower snake case. For example, a method
28-
* named "myConfOption" will become "my-conf-option" using this key mapper.
29-
*
30-
*/
31-
public final class SnakeCaseKeyMapper implements KeyMapper {
32-
33-
/**
34-
* Creates
35-
*
36-
*/
37-
public SnakeCaseKeyMapper() {}
38-
39-
@Override
40-
public @NonNull CharSequence labelToKey(@NonNull CharSequence label) {
24+
private CamelCaseMap() {}
4125

26+
static CharSequence toCasedLower(CharSequence label, char separator) {
4227
StringBuilder builder = new StringBuilder();
4328
int startAppend = 0;
4429

@@ -48,7 +33,7 @@ public SnakeCaseKeyMapper() {}
4833
// Found a segment: append everything before us
4934
builder.append(label, startAppend, n);
5035
if (n != 0) {
51-
builder.append('-');
36+
builder.append(separator);
5237
}
5338
builder.append(Character.toLowerCase(currentChar));
5439
startAppend = n + 1;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* DazzleConf
3+
* Copyright © 2025 Anand Beh
4+
*
5+
* DazzleConf is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* DazzleConf is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with DazzleConf. If not, see <https://www.gnu.org/licenses/>
17+
* and navigate to version 3 of the GNU Lesser General Public License.
18+
*/
19+
20+
package space.arim.dazzleconf.backend;
21+
22+
import org.checkerframework.checker.nullness.qual.NonNull;
23+
24+
/**
25+
* A key mapper which converts lower camel case patterns to kebab case.
26+
* <p>
27+
* This is intended for use with YAML and TOML and other formats that use kebab case. For example, a method
28+
* named "myConfOption" will become "my-conf-option" using this key mapper.
29+
*
30+
*/
31+
public final class KebabCaseKeyMapper implements KeyMapper {
32+
33+
/**
34+
* Creates
35+
*/
36+
public KebabCaseKeyMapper() {}
37+
38+
@Override
39+
public @NonNull CharSequence labelToKey(@NonNull CharSequence label) {
40+
return CamelCaseMap.toCasedLower(label, '-');
41+
}
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* DazzleConf
3+
* Copyright © 2025 Anand Beh
4+
*
5+
* DazzleConf is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* DazzleConf is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with DazzleConf. If not, see <https://www.gnu.org/licenses/>
17+
* and navigate to version 3 of the GNU Lesser General Public License.
18+
*/
19+
20+
package space.arim.dazzleconf.backend;
21+
22+
import org.checkerframework.checker.nullness.qual.NonNull;
23+
24+
/**
25+
* A key mapper which converts lower camel case patterns to snake case.
26+
* <p>
27+
* For example, a method named "myConfOption" will become "my_conf_option" using this key mapper. This is the naming
28+
* scheme (by convention) for SQL columns, Python fields and functions, and Rust struct fields.
29+
*
30+
*/
31+
public final class LowerSnakeCaseKeyMapper implements KeyMapper {
32+
33+
/**
34+
* Creates
35+
*/
36+
public LowerSnakeCaseKeyMapper() {}
37+
38+
@Override
39+
public @NonNull CharSequence labelToKey(@NonNull CharSequence label) {
40+
return CamelCaseMap.toCasedLower(label, '_');
41+
}
42+
}

core/src/test/java/space/arim/dazzleconf/ConfigurationMechanicsTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import space.arim.dazzleconf.backend.DataTree;
3434
import space.arim.dazzleconf.backend.KeyMapper;
3535
import space.arim.dazzleconf.backend.KeyPath;
36-
import space.arim.dazzleconf.backend.SnakeCaseKeyMapper;
36+
import space.arim.dazzleconf.backend.KebabCaseKeyMapper;
3737
import space.arim.dazzleconf.engine.CommentLocation;
3838
import space.arim.dazzleconf.engine.Comments;
3939
import space.arim.dazzleconf.engine.UpdateListener;
@@ -61,7 +61,7 @@ public class ConfigurationMechanicsTest {
6161

6262
private Configuration<Config> configuration;
6363
private final UpdateListener updateListener;
64-
private final KeyMapper keyMapper = new SnakeCaseKeyMapper();
64+
private final KeyMapper keyMapper = new KebabCaseKeyMapper();
6565

6666
public ConfigurationMechanicsTest(@Mock UpdateListener updateListener) {
6767
this.updateListener = updateListener;

core/src/test/java/space/arim/dazzleconf/ErrorPrintingTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import space.arim.dazzleconf.backend.KeyMapper;
2828
import space.arim.dazzleconf.backend.KeyPath;
2929
import space.arim.dazzleconf.backend.Printable;
30-
import space.arim.dazzleconf.backend.SnakeCaseKeyMapper;
30+
import space.arim.dazzleconf.backend.KebabCaseKeyMapper;
3131
import space.arim.dazzleconf.engine.UpdateReason;
3232
import space.arim.dazzleconf.engine.liaison.SubSection;
3333
import space.arim.dazzleconf.reflect.DefaultReflectionService;
@@ -89,7 +89,7 @@ public void notifyUpdate(@NonNull KeyPath entryPath, @NonNull UpdateReason updat
8989

9090
@Override
9191
public @NonNull KeyMapper keyMapper() {
92-
return new SnakeCaseKeyMapper();
92+
return new KebabCaseKeyMapper();
9393
}
9494

9595
@Override

core/src/test/java/space/arim/dazzleconf/backend/SnakeCaseKeyMapperTest.java renamed to core/src/test/java/space/arim/dazzleconf/backend/KebabCaseKeyMapperTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323

2424
import static org.junit.jupiter.api.Assertions.assertEquals;
2525

26-
public class SnakeCaseKeyMapperTest {
26+
public class KebabCaseKeyMapperTest {
2727

28-
private final KeyMapper keyMapper = new SnakeCaseKeyMapper();
28+
private final KeyMapper keyMapper = new KebabCaseKeyMapper();
2929

3030
@Test
3131
public void simple() {

0 commit comments

Comments
 (0)