Skip to content

Commit

Permalink
Better C# property validation
Browse files Browse the repository at this point in the history
  • Loading branch information
externl committed Oct 7, 2024
1 parent 30420e2 commit 83d8372
Show file tree
Hide file tree
Showing 8 changed files with 355 additions and 644 deletions.
28 changes: 16 additions & 12 deletions config/makeprops.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def parseProperty(self, propertyPrefix, attrs):
propertyClass = attrs.get("class", None)
prefixOnly = attrs.get("prefix-only", "false").lower() == "true"

if self.language == Language.CPP:
if self.language == Language.CPP or self.language == Language.CSHARP:
propertyName = name
else:
propertyName = (
Expand Down Expand Up @@ -345,7 +345,7 @@ def startElement(self, name, attrs):

# TODO: temporary until all languages support class props

if self.language != Language.CPP:
if self.language != Language.CPP and self.language != Language.CSHARP:
if self.parentNodeType == "class":
return

Expand Down Expand Up @@ -574,18 +574,18 @@ def openFiles(self):
self.srcFile.write(csPreamble)

def closeFiles(self):
self.srcFile.write(" public static Property[][] validProps =\n")
self.srcFile.write(" public static PropertyArray[] validProps =\n")

self.srcFile.write(" {\n")
self.srcFile.write(" [\n")
for s in self.generatedSections():
self.srcFile.write(" %sProps,\n" % s)
self.srcFile.write(" };\n\n")
self.srcFile.write(" ];\n\n")

self.srcFile.write(" public static string[] clPropNames =\n")
self.srcFile.write(" {\n")
self.srcFile.write(" [\n")
for s in self.sectionNames:
self.srcFile.write(' "%s",\n' % s)
self.srcFile.write(" };\n")
self.srcFile.write(" ];\n")
self.srcFile.write("}\n")
self.srcFile.close()

Expand All @@ -594,7 +594,7 @@ def fix(self, propertyName):

@override
def writeProperty(self, property):
self.srcFile.write(" %s,\n" % property)
self.srcFile.write(" %s,\n" % property)

@override
def createProperty(
Expand All @@ -606,20 +606,24 @@ def createProperty(
propertyClass,
prefixOnly,
):
line = 'new(@"{pattern}", {usesRegex}, {defaultValue}, {deprecated})'.format(
line = 'new(@"{pattern}", {usesRegex}, {defaultValue}, {deprecated}, {propertyClass}, {prefixOnly})'.format(
pattern=f"^{self.fix(propertyName)}$" if usesRegex else propertyName,
usesRegex="true" if usesRegex else "false",
defaultValue=f'"{defaultValue}"',
deprecated="true" if deprecated else "false",
propertyClass=f"{propertyClass}Props" if propertyClass else "null",
prefixOnly="true" if prefixOnly else "false",
)
return line

def openSection(self, sectionName):
self.srcFile.write(" public static Property[] %sProps =\n" % sectionName)
self.srcFile.write(" {\n")
self.srcFile.write(
f' public static PropertyArray {sectionName}Props = new(\n "{sectionName}",\n'
)
self.srcFile.write(" [\n")

def closeSection(self, sectionName):
self.srcFile.write(" };\n")
self.srcFile.write(" ]);\n")
self.srcFile.write("\n")

def moveFiles(self, location):
Expand Down
3 changes: 1 addition & 2 deletions cpp/src/Ice/PropertyUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ IceInternal::validatePropertiesWithPrefix(
const PropertiesPtr& properties,
const PropertyArray* propertyArray)
{
vector<string> unknownProps;

// Do not check for unknown properties if Ice prefix, ie Ice, Glacier2, etc
for (const char** i = IceInternal::PropertyNames::clPropNames; *i; ++i)
{
Expand All @@ -75,6 +73,7 @@ IceInternal::validatePropertiesWithPrefix(
}
}

vector<string> unknownProps;
PropertyDict props = properties->getPropertiesForPrefix(string{prefix} + ".");
for (const auto& p : props)
{
Expand Down
4 changes: 2 additions & 2 deletions cpp/src/Ice/PropertyUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ namespace IceInternal
/// @return The property if found, nullopt otherwise.
std::optional<Property> findProperty(std::string_view key, const PropertyArray* propertyArray);

/// Finds all unknown properties for a given prefix.
/// @param prefix The prefix to search for.
/// Validates the properties for a given prefix.
/// @param prefix The prefix to validate.
/// @param properties The properties to consider.
/// @param propertyArray The property array to search against.
/// @throws UnknownPropertyException if unknown properties are found.
Expand Down
14 changes: 13 additions & 1 deletion csharp/src/Ice/Internal/Property.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
// Copyright (c) ZeroC, Inc.

#nullable enable

namespace Ice.Internal;

public sealed record class Property(string pattern, bool usesRegex, string defaultValue, bool deprecated);
public sealed record class PropertyArray(
string name,
Property[] properties);

public sealed record class Property(
string pattern,
bool usesRegex,
string defaultValue,
bool deprecated,
PropertyArray? propertyClass,
bool prefixOnly);
Loading

0 comments on commit 83d8372

Please sign in to comment.