Skip to content

Commit

Permalink
Make getIcePropertiesXxx no throw in Swift (#2949)
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardnormier authored Oct 22, 2024
1 parent 12de22e commit 99080a8
Show file tree
Hide file tree
Showing 18 changed files with 71 additions and 98 deletions.
6 changes: 3 additions & 3 deletions swift/src/Ice/CommunicatorI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ class CommunicatorI: LocalObject<ICECommunicator>, Communicator {
init(handle: ICECommunicator, initData: InitializationData) {
defaultsAndOverrides = DefaultsAndOverrides(handle: handle)
self.initData = initData
let num = try! initData.properties!.getIcePropertyAsInt("Ice.ClassGraphDepthMax")
let num = initData.properties!.getIcePropertyAsInt("Ice.ClassGraphDepthMax")
if num < 1 || num > 0x7FFF_FFFF {
classGraphDepthMax = 0x7FFF_FFFF
} else {
classGraphDepthMax = num
}
traceSlicing = try! initData.properties!.getIcePropertyAsInt("Ice.Trace.Slicing") > 0
acceptClassCycles = try! initData.properties!.getIcePropertyAsInt("Ice.AcceptClassCycles") > 0
traceSlicing = initData.properties!.getIcePropertyAsInt("Ice.Trace.Slicing") > 0
acceptClassCycles = initData.properties!.getIcePropertyAsInt("Ice.AcceptClassCycles") > 0

super.init(handle: handle)
}
Expand Down
12 changes: 3 additions & 9 deletions swift/src/Ice/Properties.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ public protocol Properties: AnyObject {
/// - parameter key: `String` The property key.
///
/// - returns: `String` - The property value or the default value.
///
/// - throws: `NSException` if the key is not a valid Ice property
func getIceProperty(_ key: String) throws -> String
func getIceProperty(_ key: String) -> String

/// Get a property by key. If the property is not set, the given default value is returned.
///
Expand All @@ -43,9 +41,7 @@ public protocol Properties: AnyObject {
/// - parameter key: `String` The property key.
///
/// - returns: `Int32` - The property value interpreted as an integer, or the default value.
///
/// - throws: `NSException` if the key is not a valid Ice property
func getIcePropertyAsInt(_ key: String) throws -> Int32
func getIcePropertyAsInt(_ key: String) -> Int32

/// Get a property as an integer. If the property is not set, the given default value is returned.
///
Expand Down Expand Up @@ -76,9 +72,7 @@ public protocol Properties: AnyObject {
/// - parameter key: `String` The property key.
///
/// - returns: `StringSeq` - The property value interpreted as list of strings, or the default value.
///
/// - throws: `NSException` if the key is not a valid Ice property
func getIcePropertyAsList(_ key: String) throws -> StringSeq
func getIcePropertyAsList(_ key: String) -> StringSeq

/// Get a property as a list of strings. The strings must be separated by whitespace or comma. If the property is
/// not set, the default list is returned. The strings in the list can contain whitespace and commas if they are
Expand Down
36 changes: 18 additions & 18 deletions swift/src/Ice/PropertiesI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,43 @@ import IceImpl

class PropertiesI: LocalObject<ICEProperties>, Properties {
public func getProperty(_ key: String) -> String {
return handle.getProperty(key)
handle.getProperty(key)
}

public func getIceProperty(_ key: String) throws -> String {
try handle.getIceProperty(key)
public func getIceProperty(_ key: String) -> String {
handle.getIceProperty(key)
}

public func getPropertyWithDefault(key: String, value: String) -> String {
return handle.getPropertyWithDefault(key, value: value)
handle.getPropertyWithDefault(key, value: value)
}

public func getPropertyAsInt(_ key: String) -> Int32 {
return handle.getPropertyAsInt(key)
handle.getPropertyAsInt(key)
}

public func getIcePropertyAsInt(_ key: String) throws -> Int32 {
try handle.getIcePropertyAsInt(key) as! Int32
public func getIcePropertyAsInt(_ key: String) -> Int32 {
handle.getIcePropertyAsInt(key)
}

public func getPropertyAsIntWithDefault(key: String, value: Int32) -> Int32 {
return handle.getPropertyAsIntWithDefault(key: key, value: value)
handle.getPropertyAsIntWithDefault(key: key, value: value)
}

public func getPropertyAsList(_ key: String) -> StringSeq {
return handle.getPropertyAsList(key)
handle.getPropertyAsList(key)
}

public func getIcePropertyAsList(_ key: String) throws -> StringSeq {
try handle.getIcePropertyAsList(key)
public func getIcePropertyAsList(_ key: String) -> StringSeq {
handle.getIcePropertyAsList(key)
}

public func getPropertyAsListWithDefault(key: String, value: StringSeq) -> StringSeq {
return handle.getPropertyAsListWithDefault(key: key, value: value)
handle.getPropertyAsListWithDefault(key: key, value: value)
}

public func getPropertiesForPrefix(_ prefix: String) -> PropertyDict {
return handle.getPropertiesForPrefix(prefix)
handle.getPropertiesForPrefix(prefix)
}

public func setProperty(key: String, value: String) {
Expand All @@ -55,28 +55,28 @@ class PropertiesI: LocalObject<ICEProperties>, Properties {
}

public func getCommandLineOptions() -> StringSeq {
return handle.getCommandLineOptions()
handle.getCommandLineOptions()
}

public func parseCommandLineOptions(prefix: String, options: StringSeq) throws -> StringSeq {
return try autoreleasepool {
try autoreleasepool {
try handle.parseCommandLineOptions(prefix, options: options)
}
}

public func parseIceCommandLineOptions(_ options: StringSeq) throws -> StringSeq {
return try autoreleasepool {
try autoreleasepool {
try handle.parseIceCommandLineOptions(options)
}
}

public func load(_ file: String) throws {
return try autoreleasepool {
try autoreleasepool {
try handle.load(file)
}
}

public func clone() -> Properties {
return PropertiesI(handle: handle.clone())
PropertiesI(handle: handle.clone())
}
}
39 changes: 8 additions & 31 deletions swift/src/IceImpl/Properties.mm
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,11 @@ - (NSString*)getProperty:(NSString*)key
return toNSString(self.properties->getProperty(fromNSString(key)));
}

- (NSString*)getIceProperty:(NSString*)key error:(NSError**)error;
- (NSString*)getIceProperty:(NSString*)key;
{
try
{
return toNSString(self.properties->getIceProperty(fromNSString(key)));
}
catch (...)
{
*error = convertException(std::current_exception());
return nil;
}
// We don't catch exceptions on purpose; in particular, we want Ice::UnknownPropertyException to terminate
// the application.
return toNSString(self.properties->getIceProperty(fromNSString(key)));
}

- (NSString*)getPropertyWithDefault:(NSString*)key value:(NSString*)value
Expand All @@ -38,18 +32,9 @@ - (int32_t)getPropertyAsInt:(NSString*)key
return self.properties->getPropertyAsInt(fromNSString(key));
}

- (NSNumber*)getIcePropertyAsInt:(NSString*)key error:(NSError**)error
- (int32_t)getIcePropertyAsInt:(NSString*)key
{
try
{
int32_t value = self.properties->getIcePropertyAsInt(fromNSString(key));
return [NSNumber numberWithInt:value];
}
catch (...)
{
*error = convertException(std::current_exception());
return nil;
}
return self.properties->getIcePropertyAsInt(fromNSString(key));
}

- (int32_t)getPropertyAsIntWithDefault:(NSString*)key value:(int32_t)value
Expand All @@ -62,17 +47,9 @@ - (int32_t)getPropertyAsIntWithDefault:(NSString*)key value:(int32_t)value
return toNSArray(self.properties->getPropertyAsList(fromNSString(key)));
}

- (NSArray<NSString*>*)getIcePropertyAsList:(NSString*)key error:(NSError**)error
- (NSArray<NSString*>*)getIcePropertyAsList:(NSString*)key
{
try
{
return toNSArray(self.properties->getIcePropertyAsList(fromNSString(key)));
}
catch (...)
{
*error = convertException(std::current_exception());
return nil;
}
return toNSArray(self.properties->getIcePropertyAsList(fromNSString(key)));
}

- (NSArray<NSString*>*)getPropertyAsListWithDefault:(NSString*)key value:(NSArray<NSString*>*)value
Expand Down
23 changes: 11 additions & 12 deletions swift/src/IceImpl/include/Properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,28 @@
NS_ASSUME_NONNULL_BEGIN

ICEIMPL_API @interface ICEProperties : ICELocalObject
- (nonnull NSString*)getProperty:(NSString*)key;
- (nullable NSString*)getIceProperty:(NSString*)key error:(NSError**)error NS_SWIFT_NAME(getIceProperty(_:));
- (nonnull NSString*)getPropertyWithDefault:(NSString*)key value:(NSString*)value;
- (NSString*)getProperty:(NSString*)key;
- (NSString*)getIceProperty:(NSString*)key;
- (NSString*)getPropertyWithDefault:(NSString*)key value:(NSString*)value;
- (int32_t)getPropertyAsInt:(NSString*)key;
- (nullable NSNumber*)getIcePropertyAsInt:(NSString*)key error:(NSError**)error NS_SWIFT_NAME(getIcePropertyAsInt(_:));
- (int32_t)getIcePropertyAsInt:(NSString*)key NS_SWIFT_NAME(getIcePropertyAsInt(_:));
- (int32_t)getPropertyAsIntWithDefault:(NSString*)key
value:(int32_t)value NS_SWIFT_NAME(getPropertyAsIntWithDefault(key:value:));
- (nonnull NSArray<NSString*>*)getPropertyAsList:(NSString* _Nonnull)key;
- (nullable NSArray<NSString*>*)getIcePropertyAsList:(NSString* _Nonnull)key
error:(NSError**)error NS_SWIFT_NAME(getIcePropertyAsList(_:));
- (nonnull NSArray<NSString*>*)getPropertyAsListWithDefault:(NSString* _Nonnull)key
value:(NSArray<NSString*>* _Nonnull)value
- (NSArray<NSString*>*)getPropertyAsList:(NSString* _Nonnull)key;
- (NSArray<NSString*>*)getIcePropertyAsList:(NSString* _Nonnull)key NS_SWIFT_NAME(getIcePropertyAsList(_:));
- (NSArray<NSString*>*)getPropertyAsListWithDefault:(NSString* _Nonnull)key
value:(NSArray<NSString*>* _Nonnull)value
NS_SWIFT_NAME(getPropertyAsListWithDefault(key:value:));
- (nonnull NSDictionary<NSString*, NSString*>*)getPropertiesForPrefix:(NSString* _Nonnull)prefix
- (NSDictionary<NSString*, NSString*>*)getPropertiesForPrefix:(NSString* _Nonnull)prefix
NS_SWIFT_NAME(getPropertiesForPrefix(_:));
- (BOOL)setProperty:(NSString*)key value:(NSString*)value error:(NSError**)error;
- (nonnull NSArray<NSString*>*)getCommandLineOptions;
- (NSArray<NSString*>*)getCommandLineOptions;
- (nullable NSArray<NSString*>*)parseCommandLineOptions:(NSString*)prefix
options:(NSArray<NSString*>*)options
error:(NSError* _Nullable* _Nullable)error;
- (nullable NSArray<NSString*>*)parseIceCommandLineOptions:(NSArray<NSString*>*)options error:(NSError**)error;
- (BOOL)load:(NSString*)file error:(NSError* _Nullable* _Nullable)error;
- (nonnull ICEProperties*)clone;
- (ICEProperties*)clone;
@end

#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion swift/test/Ice/binding/AllTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ func testSecureEndpoints(helper: TestHelper, com: RemoteCommunicatorPrx) async t

let output = helper.getWriter()

if try helper.communicator().getProperties().getIceProperty("Ice.Default.Protocol") == "ssl" {
if helper.communicator().getProperties().getIceProperty("Ice.Default.Protocol") == "ssl" {
output.write("testing unsecure vs. secure endpoints... ")
do {
let adapters = try await [
Expand Down
2 changes: 1 addition & 1 deletion swift/test/Ice/enums/AllTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func allTests(_ helper: TestHelper) async throws -> TestIntfPrx {
output.write("testing enum streaming... ")

let encoding_1_0 =
try communicator.getProperties().getIceProperty("Ice.Default.EncodingVersion") == "1.0"
communicator.getProperties().getIceProperty("Ice.Default.EncodingVersion") == "1.0"

var ostr = Ice.OutputStream(communicator: communicator)
ostr.write(ByteEnum.benum11)
Expand Down
8 changes: 4 additions & 4 deletions swift/test/Ice/facets/AllTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ func allTests(_ helper: TestHelper) async throws -> GPrx {
output.write("testing Ice.Admin.Facets property... ")
try test(communicator.getProperties().getIcePropertyAsList("Ice.Admin.Facets").count == 0)
communicator.getProperties().setProperty(key: "Ice.Admin.Facets", value: "foobar")
var facetFilter = try communicator.getProperties().getIcePropertyAsList("Ice.Admin.Facets")
var facetFilter = communicator.getProperties().getIcePropertyAsList("Ice.Admin.Facets")
try test(facetFilter == ["foobar"])
communicator.getProperties().setProperty(key: "Ice.Admin.Facets", value: "foo\\'bar")
facetFilter = try communicator.getProperties().getIcePropertyAsList("Ice.Admin.Facets")
facetFilter = communicator.getProperties().getIcePropertyAsList("Ice.Admin.Facets")
try test(facetFilter == ["foo'bar"])
communicator.getProperties().setProperty(key: "Ice.Admin.Facets", value: "'foo bar' toto 'titi'")
facetFilter = try communicator.getProperties().getIcePropertyAsList("Ice.Admin.Facets")
facetFilter = communicator.getProperties().getIcePropertyAsList("Ice.Admin.Facets")
try test(facetFilter == ["foo bar", "toto", "titi"])
communicator.getProperties().setProperty(
key: "Ice.Admin.Facets", value: "'foo bar\\' toto' 'titi'")
facetFilter = try communicator.getProperties().getIcePropertyAsList("Ice.Admin.Facets")
facetFilter = communicator.getProperties().getIcePropertyAsList("Ice.Admin.Facets")
try test(facetFilter == ["foo bar' toto", "titi"])
// communicator.getProperties().setProperty("Ice.Admin.Facets", "'foo bar' 'toto titi");
// facetFilter = try communicator.getProperties().getIcePropertyAsList("Ice.Admin.Facets");
Expand Down
4 changes: 2 additions & 2 deletions swift/test/Ice/info/AllTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func allTests(_ helper: TestHelper) async throws {

output.write("test object adapter endpoint information... ")
do {
let host = try communicator.getProperties().getIcePropertyAsInt("Ice.IPv6") != 0 ? "::1" : "127.0.0.1"
let host = communicator.getProperties().getIcePropertyAsInt("Ice.IPv6") != 0 ? "::1" : "127.0.0.1"
communicator.getProperties().setProperty(
key: "TestAdapter.Endpoints",
value: "tcp -h \"\(host)\" -t 15000:udp -h \"\(host)\"")
Expand Down Expand Up @@ -149,7 +149,7 @@ func allTests(_ helper: TestHelper) async throws {

let testIntf = try await checkedCast(prx: base, type: TestIntfPrx.self)!

let defaultHost = try communicator.getProperties().getIceProperty("Ice.Default.Host")
let defaultHost = communicator.getProperties().getIceProperty("Ice.Default.Host")

output.write("test connection endpoint information... ")
do {
Expand Down
2 changes: 1 addition & 1 deletion swift/test/Ice/objects/TestI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class InitialI: Initial {

func acceptsClassCycles(current: Ice.Current) async throws -> Bool {
let properties = current.adapter.getCommunicator().getProperties()
return try properties.getIcePropertyAsInt("Ice.AcceptClassCycles") > 0
return properties.getIcePropertyAsInt("Ice.AcceptClassCycles") > 0
}

func getD1(d1: D1?, current _: Ice.Current) async throws -> D1? {
Expand Down
2 changes: 1 addition & 1 deletion swift/test/Ice/operations/BatchOneways.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func batchOneways(_ helper: TestHelper, _ p: MyClassPrx) async throws {
conn = try await p.ice_getConnection()
if supportsCompress,
conn != nil,
try! p.ice_getCommunicator().getProperties().getIceProperty("Ice.Override.Compress") == ""
p.ice_getCommunicator().getProperties().getIceProperty("Ice.Override.Compress") == ""
{
let prx = try await p.ice_getConnection()!.createProxy(p.ice_getIdentity()).ice_batchOneway()

Expand Down
2 changes: 1 addition & 1 deletion swift/test/Ice/optional/AllTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ func allTests(_ helper: TestHelper) async throws -> InitialPrx {
}
output.writeLine("ok")

if communicator.getProperties().getPropertyAsInt("Ice.Default.SlicedFormat") > 0 {
if communicator.getProperties().getIcePropertyAsInt("Ice.Default.SlicedFormat") > 0 {
output.write("testing marshaling with unknown class slices... ")
do {
let c = C()
Expand Down
17 changes: 10 additions & 7 deletions swift/test/Ice/properties/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ public class Client: TestHelperI {
output.write("testing ice properties with set default values...")
let properties = Ice.createProperties()

let toStringMode = try properties.getIceProperty("Ice.ToStringMode")
let toStringMode = properties.getIceProperty("Ice.ToStringMode")
try test(toStringMode == "Unicode")

let closeTimeout = try properties.getIcePropertyAsInt("Ice.Connection.Client.CloseTimeout")
let closeTimeout = properties.getIcePropertyAsInt("Ice.Connection.Client.CloseTimeout")
try test(closeTimeout == 10)

let retryIntervals = try properties.getIcePropertyAsList("Ice.RetryIntervals")
let retryIntervals = properties.getIcePropertyAsList("Ice.RetryIntervals")
try test(retryIntervals == ["0"])

output.writeLine("ok")
Expand All @@ -117,24 +117,26 @@ public class Client: TestHelperI {
output.write("testing ice properties with unset default values...")
let properties = Ice.createProperties()

let stringValue = try properties.getIceProperty("Ice.Admin.Router")
let stringValue = properties.getIceProperty("Ice.Admin.Router")
try test(stringValue == "")

let intValue = try properties.getIcePropertyAsInt("Ice.Admin.Router")
let intValue = properties.getIcePropertyAsInt("Ice.Admin.Router")
try test(intValue == 0)

let listValue = try properties.getIcePropertyAsList("Ice.Admin.Router")
let listValue = properties.getIcePropertyAsList("Ice.Admin.Router")
try test(listValue == [])

output.writeLine("ok")
}

/*
do {
output.write("testing that getting an unknown ice property throws an exception...")
let properties = Ice.createProperties()
do {
_ = try properties.getIceProperty("Ice.UnknownProperty")
// Cannot test in Swift since this generates a fatal error.
_ = properties.getIceProperty("Ice.UnknownProperty")
try test(false)
} catch let error as UnknownPropertyException {
try test(error.ice_id() == "::Ice::UnknownPropertyException")
Expand All @@ -143,5 +145,6 @@ public class Client: TestHelperI {
output.writeLine("ok")
}
*/
}
}
2 changes: 1 addition & 1 deletion swift/test/Ice/proxy/AllTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ public func allTests(_ helper: TestHelper) async throws -> MyClassPrx {
"test -e 1.1:opaque -e 1.1 -t 1 -v CTEyNy4wLjAuMeouAAAQJwAAAA==")!
try test(communicator.proxyToString(p2) == "test -t -e 1.1:tcp -h 127.0.0.1 -p 12010 -t 10000")

if try communicator.getProperties().getIcePropertyAsInt("Ice.IPv6") == 0 {
if communicator.getProperties().getIcePropertyAsInt("Ice.IPv6") == 0 {
// Two legal TCP endpoints expressed as opaque endpoints
p1 = try communicator.stringToProxy(
"test -e 1.0:" + "opaque -e 1.0 -t 1 -v CTEyNy4wLjAuMeouAAAQJwAAAA==:"
Expand Down
Loading

0 comments on commit 99080a8

Please sign in to comment.