From 2ec96d39ca9768e1d5c69295f15545d7e5e6c4a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gilles=20Gr=C3=A9goire?= <1478233+ggilles@users.noreply.github.com> Date: Thu, 27 Jan 2022 00:42:53 +0100 Subject: [PATCH] Allow usage of all caps identifiers in Djinni files (#58) * Correctly handle all caps identifiers * Add test around upper case identifiers with underscores --- src/source/generator.scala | 23 +++++++++++++++---- test-suite/djinni/constants.djinni | 6 +++++ .../generated-src/cpp/constants_interface.cpp | 3 +++ .../generated-src/cpp/constants_interface.hpp | 6 +++++ .../djinni/test/ConstantsInterface.java | 7 ++++++ .../generated-src/objc/DBConstantsInterface.h | 4 ++++ .../objc/DBConstantsInterface.mm | 4 ++++ test-suite/generated-src/ts/test.ts | 4 ++++ .../wasm/NativeConstantsInterface.cpp | 2 ++ 9 files changed, 55 insertions(+), 4 deletions(-) diff --git a/src/source/generator.scala b/src/source/generator.scala index 2907d6c4..5ae2c75b 100644 --- a/src/source/generator.scala +++ b/src/source/generator.scala @@ -111,7 +111,22 @@ package object generatorTools { if (s.isEmpty) s else ", " + s } def q(s: String) = '"' + s + '"' - def firstUpper(token: String) = if (token.isEmpty()) token else token.charAt(0).toUpper + token.substring(1) + + def leadingUpper(token: String) = { + if (token.isEmpty()) { + token + } else { + val head = token.charAt(0) + val tail = token.substring(1) + // Preserve mixed case identifiers like 'XXFoo': + // Convert tail to lowercase only when it is full uppercase. + if (tail.toUpperCase == tail) { + head.toUpper + tail.toLowerCase + } else { + head.toUpper + tail + } + } + } type IdentConverter = String => String @@ -132,13 +147,13 @@ package object generatorTools { enum: IdentConverter, const: IdentConverter) object IdentStyle { - val camelUpper = (s: String) => s.split("[-_]").map(firstUpper).mkString + val camelUpper = (s: String) => s.split("[-_]").map(leadingUpper).mkString val camelLower = (s: String) => { val parts = s.split('_') - parts.head + parts.tail.map(firstUpper).mkString + parts.head + parts.tail.map(leadingUpper).mkString } val underLower = (s: String) => s - val underUpper = (s: String) => s.split('_').map(firstUpper).mkString("_") + val underUpper = (s: String) => s.split('_').map(leadingUpper).mkString("_") val underCaps = (s: String) => s.toUpperCase val prefix = (prefix: String, suffix: IdentConverter) => (s: String) => prefix + suffix(s) diff --git a/test-suite/djinni/constants.djinni b/test-suite/djinni/constants.djinni index 3222e861..b1a514ce 100644 --- a/test-suite/djinni/constants.djinni +++ b/test-suite/djinni/constants.djinni @@ -84,4 +84,10 @@ constants_interface = interface +c { # No support for constant binary, list, set, map dummy(); + + # An upper-case constant should be parsed correctly + const UPPER_CASE_CONSTANT: string = "upper-case-constant"; + + # But we should preserve weirdness in casing + const XXXWeird_Case: i32 = 1; } diff --git a/test-suite/generated-src/cpp/constants_interface.cpp b/test-suite/generated-src/cpp/constants_interface.cpp index 9ab65b72..db767065 100644 --- a/test-suite/generated-src/cpp/constants_interface.cpp +++ b/test-suite/generated-src/cpp/constants_interface.cpp @@ -35,4 +35,7 @@ ConstantRecord const ConstantsInterface::OBJECT_CONSTANT = ConstantRecord( ConstantsInterface::I32_CONSTANT /* some_integer */ , ConstantsInterface::STRING_CONSTANT /* some_string */ ); +std::string const ConstantsInterface::UPPER_CASE_CONSTANT = {"upper-case-constant"}; + + } // namespace testsuite diff --git a/test-suite/generated-src/cpp/constants_interface.hpp b/test-suite/generated-src/cpp/constants_interface.hpp index a534c628..8075b8ad 100644 --- a/test-suite/generated-src/cpp/constants_interface.hpp +++ b/test-suite/generated-src/cpp/constants_interface.hpp @@ -62,6 +62,12 @@ class ConstantsInterface { static ConstantRecord const OBJECT_CONSTANT; + /** An upper-case constant should be parsed correctly */ + static std::string const UPPER_CASE_CONSTANT; + + /** But we should preserve weirdness in casing */ + static constexpr int32_t XXXWEIRD_CASE = 1; + /** * No support for null optional constants * No support for optional constant records diff --git a/test-suite/generated-src/java/com/dropbox/djinni/test/ConstantsInterface.java b/test-suite/generated-src/java/com/dropbox/djinni/test/ConstantsInterface.java index 2976d21a..79cac8a4 100644 --- a/test-suite/generated-src/java/com/dropbox/djinni/test/ConstantsInterface.java +++ b/test-suite/generated-src/java/com/dropbox/djinni/test/ConstantsInterface.java @@ -68,6 +68,13 @@ public abstract class ConstantsInterface { I32_CONSTANT /* mSomeInteger */ , STRING_CONSTANT /* mSomeString */ ); + /** An upper-case constant should be parsed correctly */ + @Nonnull + public static final String UPPER_CASE_CONSTANT = "upper-case-constant"; + + /** But we should preserve weirdness in casing */ + public static final int XXXWEIRD_CASE = 1; + /** * No support for null optional constants * No support for optional constant records diff --git a/test-suite/generated-src/objc/DBConstantsInterface.h b/test-suite/generated-src/objc/DBConstantsInterface.h index c8c86ca2..c7b3da64 100644 --- a/test-suite/generated-src/objc/DBConstantsInterface.h +++ b/test-suite/generated-src/objc/DBConstantsInterface.h @@ -19,6 +19,10 @@ extern float const DBConstantsInterfaceF32Constant; extern double const DBConstantsInterfaceF64Constant; extern NSString * __nonnull const DBConstantsInterfaceStringConstant; extern NSString * __nullable const DBConstantsInterfaceOptStringConstant; +/** An upper-case constant should be parsed correctly */ +extern NSString * __nonnull const DBConstantsInterfaceUpperCaseConstant; +/** But we should preserve weirdness in casing */ +extern int32_t const DBConstantsInterfaceXXXWeirdCase; /** Interface containing constants */ @interface DBConstantsInterface : NSObject diff --git a/test-suite/generated-src/objc/DBConstantsInterface.mm b/test-suite/generated-src/objc/DBConstantsInterface.mm index def90c46..dcdd0355 100644 --- a/test-suite/generated-src/objc/DBConstantsInterface.mm +++ b/test-suite/generated-src/objc/DBConstantsInterface.mm @@ -21,3 +21,7 @@ NSString * __nonnull const DBConstantsInterfaceStringConstant = @"string-constant"; NSString * __nullable const DBConstantsInterfaceOptStringConstant = @"string-constant"; + +NSString * __nonnull const DBConstantsInterfaceUpperCaseConstant = @"upper-case-constant"; + +int32_t const DBConstantsInterfaceXXXWeirdCase = 1; diff --git a/test-suite/generated-src/ts/test.ts b/test-suite/generated-src/ts/test.ts index 9a5a3180..17ba4d44 100644 --- a/test-suite/generated-src/ts/test.ts +++ b/test-suite/generated-src/ts/test.ts @@ -379,6 +379,10 @@ export namespace ConstantsInterface { someString: STRING_CONSTANT } ; + /** An upper-case constant should be parsed correctly */ + export const UPPER_CASE_CONSTANT = "upper-case-constant"; + /** But we should preserve weirdness in casing */ + export const XXXWEIRD_CASE = 1; } export interface /*record*/ AssortedPrimitives { diff --git a/test-suite/generated-src/wasm/NativeConstantsInterface.cpp b/test-suite/generated-src/wasm/NativeConstantsInterface.cpp index 57135885..89db5bf1 100644 --- a/test-suite/generated-src/wasm/NativeConstantsInterface.cpp +++ b/test-suite/generated-src/wasm/NativeConstantsInterface.cpp @@ -51,6 +51,8 @@ namespace { someString: Module.testsuite_ConstantsInterface.STRING_CONSTANT } ; + Module.testsuite_ConstantsInterface.UPPER_CASE_CONSTANT = "upper-case-constant"; + Module.testsuite_ConstantsInterface.XXXWEIRD_CASE = 1; }) } void NativeConstantsInterface::staticInitializeConstants() {