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

Improved Camel Case linter to automatically detect if upper or lower … #1217

Merged
merged 5 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -18,11 +18,17 @@
import static java.lang.String.format;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.neighbor.Walker;
import software.amazon.smithy.model.node.NodeMapper;
import software.amazon.smithy.model.shapes.MemberShape;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.traits.AuthDefinitionTrait;
import software.amazon.smithy.model.traits.ProtocolDefinitionTrait;
Expand All @@ -42,12 +48,12 @@ public final class CamelCaseValidator extends AbstractValidator {
* CamelCase configuration settings.
*/
public static final class Config {
private MemberNameHandling memberNames = MemberNameHandling.LOWER;
private MemberNameHandling memberNames = MemberNameHandling.AUTO;

/**
* Gets how member names are to be cased.
*
* <p>One of "upper" or "lower" (default).
* <p>One of "upper", "lower", or "auto" (default).
*
* @return returns member name casing.
*/
Expand Down Expand Up @@ -87,6 +93,21 @@ Pattern getRegex() {
public String toString() {
return "lower";
}
},

/**
* Member names are serialized using either all lower camel or all upper camel, but not a mixture.
*/
AUTO {
Pattern getRegex() {
// No Pattern associated with auto setting
return null;
}

@Override
public String toString() {
return "auto";
}
};

abstract Pattern getRegex();
Expand Down Expand Up @@ -135,14 +156,76 @@ public List<ValidationEvent> validate(Model model) {
shape.getType(), shape.getId().getName())))
.forEach(events::add);

Pattern isValidMemberName = config.getMemberNames().getRegex();
model.shapes(MemberShape.class)
.filter(shape -> !isValidMemberName.matcher(shape.getMemberName()).find())
.map(shape -> danger(shape, format(
"Member shape member name, `%s`, is not %s camel case",
shape.getMemberName(), config.getMemberNames())))
.forEach(events::add);
// First validate each service's closure's member shape member names
Set<MemberShape> seenShapes = new HashSet<>();
for (ServiceShape serviceShape : model.getServiceShapes()) {
Set<Shape> serviceClosure = new HashSet<>();
Walker walker = new Walker(model);
walker.iterateShapes(serviceShape).forEachRemaining(serviceClosure::add);
rchache marked this conversation as resolved.
Show resolved Hide resolved
List<MemberShape> memberShapes = serviceClosure.stream()
.filter(Shape::isMemberShape)
.map(shape -> (MemberShape) shape)
.collect(Collectors.toList());
events.addAll(validateCamelCasing(memberShapes, serviceShape.getId().getName()));
seenShapes.addAll(memberShapes);
}

// Next get all other member shapes (ex. trait shape members) and validate per namespace grouping
Map<String, List<MemberShape>> memberShapesByNamespace = model.toSet(MemberShape.class).stream()
.filter(memberShape -> !seenShapes.contains(memberShape))
.collect(Collectors.groupingBy(
memberShape -> memberShape.getContainer().getNamespace()));

for (Map.Entry<String, List<MemberShape>> memberShapeGrouping : memberShapesByNamespace.entrySet()) {
events.addAll(validateCamelCasing(memberShapeGrouping.getValue(),
memberShapeGrouping.getKey() + " namespace"));
}

return events;
}

private List<ValidationEvent> validateCamelCasing(List<MemberShape> memberShapes, String scope) {
int upperCamelMemberNamesCount = 0;
int lowerCamelMemberNamesCount = 0;
Set<MemberShape> nonUpperCamelMemberShapes = new HashSet<>();
Set<MemberShape> nonLowerCamelMemberShapes = new HashSet<>();

for (MemberShape memberShape : memberShapes) {
if (MemberNameHandling.UPPER.getRegex().matcher(memberShape.getMemberName()).find()) {
upperCamelMemberNamesCount++;
} else {
nonUpperCamelMemberShapes.add(memberShape);
}
if (MemberNameHandling.LOWER.getRegex().matcher(memberShape.getMemberName()).find()) {
lowerCamelMemberNamesCount++;
} else {
nonLowerCamelMemberShapes.add(memberShape);
}
}

// Member shapes are expected to be either upper or lower, depending on the config (and in AUTO mode, the model)
Set<MemberShape> violatingMemberShapes = new HashSet<>();
String memberNameHandling = config.getMemberNames().toString();
if (MemberNameHandling.AUTO.equals(config.getMemberNames())) {
if (upperCamelMemberNamesCount > lowerCamelMemberNamesCount) {
violatingMemberShapes = nonUpperCamelMemberShapes;
memberNameHandling = MemberNameHandling.UPPER.toString();
} else {
violatingMemberShapes = nonLowerCamelMemberShapes;
memberNameHandling = MemberNameHandling.LOWER.toString();
}
} else if (MemberNameHandling.UPPER.equals(config.getMemberNames())) {
violatingMemberShapes = nonUpperCamelMemberShapes;
} else if (MemberNameHandling.LOWER.equals(config.getMemberNames())) {
violatingMemberShapes = nonLowerCamelMemberShapes;
}

String finalMemberNameHandling = memberNameHandling;
return violatingMemberShapes.stream()
.map(shape -> danger(shape, format(
"Member shape member name, `%s`, is not %s camel case;"
+ " members in the %s must all use %s camel case.",
shape.getMemberName(), finalMemberNameHandling, scope, finalMemberNameHandling)))
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[DANGER] smithy.example#UpperOperationResponse$lowerCamel: Member shape member name, `lowerCamel`, is not upper camel case; members in the UpperService must all use upper camel case. | DefaultCamelCase
[DANGER] smithy.example#LowerOperationResponse$UpperCamel: Member shape member name, `UpperCamel`, is not lower camel case; members in the LowerService must all use lower camel case. | DefaultCamelCase
[DANGER] smithy.example#MyStructure$snake_case: Member shape member name, `snake_case`, is not lower camel case; members in the LowerService must all use lower camel case. | DefaultCamelCase
[DANGER] smithy.example#MyStructure$snake_case: Member shape member name, `snake_case`, is not upper camel case; members in the UpperService must all use upper camel case. | DefaultCamelCase
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
$version: "1.0"

metadata validators = [
{name: "CamelCase",
id: "DefaultCamelCase"}
]

namespace smithy.example

service UpperService {
version: "2020-09-21",
operations: [UpperOperation],
}

operation UpperOperation {
input: UpperOperationRequest,
output: UpperOperationResponse,
errors: [],
}

structure UpperOperationRequest {
UpperCamel: MyString,
SecondUpperCamel: MyString,
ThirdUpperCamel: MyString,
FourthUpperCamel: MyStructure
}

structure UpperOperationResponse {
lowerCamel: MyString
}

service LowerService {
version: "2020-09-21",
operations: [LowerOperation],
}

operation LowerOperation {
input: LowerOperationRequest,
output: LowerOperationResponse,
errors: [],
}

structure LowerOperationRequest {
lowerCamel: MyString,
secondLowerCamel: MyString,
thirdLowerCamel: MyString,
fourthLowerCamel: MyStructure
}

structure LowerOperationResponse {
UpperCamel: MyString
}

structure MyStructure {
snake_case: MyString
}

string MyString
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
[DANGER] ns.foo#Structure$UpperCamelCase: Member shape member name, `UpperCamelCase`, is not lower camel case | DefaultCamelCase
[DANGER] ns.foo#Structure$lowerCamelCase: Member shape member name, `lowerCamelCase`, is not upper camel case | OppositeOfDefaults
[DANGER] ns.foo#Structure$snake_case: Member shape member name, `snake_case`, is not lower camel case | DefaultCamelCase
[DANGER] ns.foo#Structure$snake_case: Member shape member name, `snake_case`, is not upper camel case | OppositeOfDefaults
[DANGER] ns.foo#bam: string shape name, `bam`, is not upper camel case | LowerCamelCase
[DANGER] ns.foo#snake_case: string shape name, `snake_case`, is not upper camel case | LowerCamelCase
[DANGER] ns.foo#InvalidTrait: string trait definition, `InvalidTrait`, is not lower camel case | LowerCamelCase
[DANGER] ns.foo#upperStructureTrait$UpperCamelCase: Member shape member name, `UpperCamelCase`, is not lower camel case; members in the ns.foo namespace must all use lower camel case. | LowerCamelCase
[DANGER] ns.bar#upperStructureTrait$UpperCamelCase: Member shape member name, `UpperCamelCase`, is not lower camel case; members in the ns.bar namespace must all use lower camel case. | LowerCamelCase
[DANGER] ns.bar#upperStructureTrait$SecondUpperCamelCase: Member shape member name, `SecondUpperCamelCase`, is not lower camel case; members in the ns.bar namespace must all use lower camel case. | LowerCamelCase
[DANGER] ns.foo#upperStructureTrait$snake_case: Member shape member name, `snake_case`, is not lower camel case; members in the ns.foo namespace must all use lower camel case. | LowerCamelCase
[DANGER] ns.baz#lowerStructureTrait$UpperCamelCase: Member shape member name, `UpperCamelCase`, is not lower camel case; members in the ns.baz namespace must all use lower camel case. | LowerCamelCase
[DANGER] ns.foo#Structure$UpperCamelCase: Member shape member name, `UpperCamelCase`, is not lower camel case; members in the MyService must all use lower camel case. | LowerCamelCase
[DANGER] ns.foo#Structure$SecondUpperCamelCase: Member shape member name, `SecondUpperCamelCase`, is not lower camel case; members in the MyService must all use lower camel case. | LowerCamelCase
[DANGER] ns.foo#Structure$ThirdUpperCamelCase: Member shape member name, `ThirdUpperCamelCase`, is not lower camel case; members in the MyService must all use lower camel case. | LowerCamelCase
[DANGER] ns.foo#Structure$FourthUpperCamelCase: Member shape member name, `FourthUpperCamelCase`, is not lower camel case; members in the MyService must all use lower camel case. | LowerCamelCase
[DANGER] ns.foo#Structure$snake_case: Member shape member name, `snake_case`, is not lower camel case; members in the MyService must all use lower camel case. | LowerCamelCase


[DANGER] ns.foo#InvalidTrait: string trait definition, `InvalidTrait`, is not lower camel case | UpperCamelCase
[DANGER] ns.foo#bam: string shape name, `bam`, is not upper camel case | UpperCamelCase
[DANGER] ns.foo#snake_case: string shape name, `snake_case`, is not upper camel case | UpperCamelCase
[DANGER] ns.foo#lowerStructureTrait$lowerCamelCase: Member shape member name, `lowerCamelCase`, is not upper camel case; members in the ns.foo namespace must all use upper camel case. | UpperCamelCase
[DANGER] ns.foo#upperStructureTrait$snake_case: Member shape member name, `snake_case`, is not upper camel case; members in the ns.foo namespace must all use upper camel case. | UpperCamelCase
[DANGER] ns.bar#upperStructureTrait$lowerCamelCase: Member shape member name, `lowerCamelCase`, is not upper camel case; members in the ns.bar namespace must all use upper camel case. | UpperCamelCase
[DANGER] ns.baz#lowerStructureTrait$lowerCamelCase: Member shape member name, `lowerCamelCase`, is not upper camel case; members in the ns.baz namespace must all use upper camel case. | UpperCamelCase
[DANGER] ns.baz#lowerStructureTrait$secondLowerCamelCase: Member shape member name, `secondLowerCamelCase`, is not upper camel case; members in the ns.baz namespace must all use upper camel case. | UpperCamelCase
[DANGER] ns.foo#Structure$snake_case: Member shape member name, `snake_case`, is not upper camel case; members in the MyService must all use upper camel case. | UpperCamelCase
[DANGER] foo.protocols#fooJson1_1$foo: Member shape member name, `foo`, is not upper camel case; members in the foo.protocols namespace must all use upper camel case. | UpperCamelCase
[DANGER] foo.auth#v1_1$name: Member shape member name, `name`, is not upper camel case; members in the foo.auth namespace must all use upper camel case. | UpperCamelCase
[DANGER] ns.foo#Structure$lowerCamelCase: Member shape member name, `lowerCamelCase`, is not upper camel case; members in the MyService must all use upper camel case. | UpperCamelCase


[DANGER] ns.foo#InvalidTrait: string trait definition, `InvalidTrait`, is not lower camel case | DefaultCamelCase
[DANGER] ns.foo#bam: string shape name, `bam`, is not upper camel case | DefaultCamelCase
[DANGER] ns.foo#bam: string shape name, `bam`, is not upper camel case | OppositeOfDefaults
[DANGER] ns.foo#snake_case: string shape name, `snake_case`, is not upper camel case | DefaultCamelCase
[DANGER] ns.foo#snake_case: string shape name, `snake_case`, is not upper camel case | OppositeOfDefaults
[DANGER] ns.foo#InvalidTrait: string trait definition, `InvalidTrait`, is not lower camel case | DefaultCamelCase
[DANGER] ns.foo#InvalidTrait: string trait definition, `InvalidTrait`, is not lower camel case | OppositeOfDefaults
[DANGER] ns.foo#upperStructureTrait$UpperCamelCase: Member shape member name, `UpperCamelCase`, is not lower camel case | DefaultCamelCase
[DANGER] ns.foo#upperStructureTrait$snake_case: Member shape member name, `snake_case`, is not lower camel case | DefaultCamelCase
[DANGER] ns.foo#upperStructureTrait$snake_case: Member shape member name, `snake_case`, is not upper camel case | OppositeOfDefaults
[DANGER] ns.foo#lowerStructureTrait$lowerCamelCase: Member shape member name, `lowerCamelCase`, is not upper camel case | OppositeOfDefaults
[DANGER] foo.protocols#fooJson1_1$foo: Member shape member name, `foo`, is not upper camel case | OppositeOfDefaults
[DANGER] foo.auth#v1_1$name: Member shape member name, `name`, is not upper camel case | OppositeOfDefaults
[DANGER] ns.foo#upperStructureTrait$snake_case: Member shape member name, `snake_case`, is not lower camel case; members in the ns.foo namespace must all use lower camel case. | DefaultCamelCase
[DANGER] ns.foo#upperStructureTrait$UpperCamelCase: Member shape member name, `UpperCamelCase`, is not lower camel case; members in the ns.foo namespace must all use lower camel case. | DefaultCamelCase
[DANGER] ns.bar#upperStructureTrait$lowerCamelCase: Member shape member name, `lowerCamelCase`, is not upper camel case; members in the ns.bar namespace must all use upper camel case. | DefaultCamelCase
[DANGER] ns.baz#lowerStructureTrait$UpperCamelCase: Member shape member name, `UpperCamelCase`, is not lower camel case; members in the ns.baz namespace must all use lower camel case. | DefaultCamelCase
[DANGER] ns.foo#Structure$lowerCamelCase: Member shape member name, `lowerCamelCase`, is not upper camel case; members in the MyService must all use upper camel case. | DefaultCamelCase
[DANGER] ns.foo#Structure$snake_case: Member shape member name, `snake_case`, is not upper camel case; members in the MyService must all use upper camel case. | DefaultCamelCase
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,40 @@
"smithy.api#trait": {}
}
},
"ns.bar#upperStructureTrait": {
"type": "structure",
"members": {
"UpperCamelCase": {
"target": "ns.foo#Foo"
},
"SecondUpperCamelCase": {
"target": "ns.foo#Foo"
},
"lowerCamelCase": {
"target": "ns.foo#Foo"
}
},
"traits": {
"smithy.api#trait": {}
}
},
"ns.baz#lowerStructureTrait": {
"type": "structure",
"members": {
"lowerCamelCase": {
"target": "ns.foo#Foo"
},
"secondLowerCamelCase": {
"target": "ns.foo#Foo"
},
"UpperCamelCase": {
"target": "ns.foo#Foo"
}
},
"traits": {
"smithy.api#trait": {}
}
},
"ns.foo#Foo": {
"type": "string"
},
Expand All @@ -59,8 +93,17 @@
"UpperCamelCase": {
"target": "ns.foo#Foo"
},
"snake_case": {
"SecondUpperCamelCase": {
"target": "ns.foo#Foo"
},
"ThirdUpperCamelCase": {
"target": "ns.foo#BazBar"
},
"FourthUpperCamelCase": {
"target": "ns.foo#bam"
},
"snake_case": {
"target": "ns.foo#snake_case"
}
}
},
Expand Down Expand Up @@ -95,20 +138,44 @@
},
"smithy.api#authDefinition": {}
}
},
"ns.foo#MyService": {
"type": "service",
"operations": [
{
"target": "ns.foo#MyOperation"
}
]
},
"ns.foo#MyOperation": {
"type": "operation",
"input": {
"target": "ns.foo#Structure"
},
"output": {
"target": "ns.foo#Structure"
}
}
},
"metadata": {
"validators": [
{
"name": "CamelCase",
"id": "DefaultCamelCase"
"id": "LowerCamelCase",
"configuration": {
"memberNames": "lower"
}
},
{
"name": "CamelCase",
"id": "OppositeOfDefaults",
"id": "UpperCamelCase",
"configuration": {
"memberNames": "upper"
}
},
{
"name": "CamelCase",
"id": "DefaultCamelCase"
}
]
}
Expand Down