Skip to content

Commit

Permalink
Allow usage of all caps identifiers in Djinni files (#58)
Browse files Browse the repository at this point in the history
* Correctly handle all caps identifiers

* Add test around upper case identifiers with underscores
  • Loading branch information
ggilles authored Jan 26, 2022
1 parent d9aadf3 commit 2ec96d3
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/source/generator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)

Expand Down
6 changes: 6 additions & 0 deletions test-suite/djinni/constants.djinni
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
3 changes: 3 additions & 0 deletions test-suite/generated-src/cpp/constants_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions test-suite/generated-src/cpp/constants_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions test-suite/generated-src/objc/DBConstantsInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions test-suite/generated-src/objc/DBConstantsInterface.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
4 changes: 4 additions & 0 deletions test-suite/generated-src/ts/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions test-suite/generated-src/wasm/NativeConstantsInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 2ec96d3

Please sign in to comment.