You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Being able to access this would've allowed me to easily log the name of the payload being processed, as I am using oneof to enumerate all possible IPC payloads. I think this is a more than acceptable use case.
IMO, this data has been sitting in the library for 5 years and counting. It clearly works well enough, even if it is "not quite correct" or a "special-case". If the alternative is reflection, which is not performant, why should this remain an internal API? More importantly, why is the library the only one with easy access to this information?
My workaround uses reflection, building atop of the workaround here:
varnameMapCache:[String:[Int:String]]=[:]extension_NameMap{func protoNameFor(rawValue:Int, cacheKey:String)->String?{
if let cache =nameMapCache[cacheKey]{returncache[rawValue]}letselfMirror=Mirror(reflecting:self)
guard let numberToNameMapChild = selfMirror.children.first(where:{(name, _)->Boolinreturn name =="numberToNameMap"}),let numberToNameMap = numberToNameMapChild.value as?Dictionary<Int,Any>else{returnnil}nameMapCache[cacheKey]= numberToNameMap.compactMapValues{ value inletvalueMirror=Mirror(reflecting: value)
guard let protoChild = valueMirror.children.first(where:{(name, _)->Boolinreturn name =="proto"}),let stringConvertible = protoChild.value as?CustomStringConvertibleelse{returnnil}return stringConvertible.description
}returnprotoNameFor(rawValue: rawValue, cacheKey: cacheKey)}}structPBPayloadCommandNameReader:SwiftProtobuf.Visitor{letstart:Intmutatingfunc visitSingularDoubleField(value:Double, fieldNumber:Int)throws{trycheck(fieldNumber: fieldNumber)}mutatingfunc visitSingularInt64Field(value:Int64, fieldNumber:Int)throws{trycheck(fieldNumber: fieldNumber)}mutatingfunc visitSingularUInt64Field(value:UInt64, fieldNumber:Int)throws{trycheck(fieldNumber: fieldNumber)}mutatingfunc visitSingularBoolField(value:Bool, fieldNumber:Int)throws{trycheck(fieldNumber: fieldNumber)}mutatingfunc visitSingularStringField(value:String, fieldNumber:Int)throws{trycheck(fieldNumber: fieldNumber)}mutatingfunc visitSingularBytesField(value:Data, fieldNumber:Int)throws{trycheck(fieldNumber: fieldNumber)}mutatingfunc visitSingularEnumField<E>(value:E, fieldNumber:Int)throwswhere E :Enum{trycheck(fieldNumber: fieldNumber)}mutatingfunc visitMapField<KeyType, ValueType>(fieldType:_ProtobufMap<KeyType,ValueType>.Type, value:_ProtobufMap<KeyType,ValueType>.BaseType, fieldNumber:Int)throwswhere KeyType :MapKeyType, ValueType :MapValueType{trycheck(fieldNumber: fieldNumber)}mutatingfunc visitMapField<KeyType, ValueType>(fieldType:_ProtobufEnumMap<KeyType,ValueType>.Type, value:_ProtobufEnumMap<KeyType,ValueType>.BaseType, fieldNumber:Int)throwswhere KeyType :MapKeyType, ValueType :Enum, ValueType.RawValue ==Int{trycheck(fieldNumber: fieldNumber)}mutatingfunc visitMapField<KeyType, ValueType>(fieldType:_ProtobufMessageMap<KeyType,ValueType>.Type, value:_ProtobufMessageMap<KeyType,ValueType>.BaseType, fieldNumber:Int)throwswhere KeyType :MapKeyType, ValueType :Hashable, ValueType :SwiftProtobuf.Message{trycheck(fieldNumber: fieldNumber)}mutatingfunc visitUnknown(bytes:Data)throws{}enumFound:Error{case found(String?)}mutatingfunc visitSingularMessageField<M>(value:M, fieldNumber:Int)throwswhere M :SwiftProtobuf.Message{trycheck(fieldNumber: fieldNumber)}func check(fieldNumber:Int)throws{
guard fieldNumber >= start else{return}throwFound.found(PBPayload._protobuf_nameMap.protoNameFor(rawValue: fieldNumber, cacheKey:"PBPayloadCommandReflection"))}}
/// PBPayload { int64 ID = 1; bool IsResponse = 2; oneof Command { ... }; }
extensionPBPayload{varcommandName:String?{varreader=PBPayloadCommandNameReader(start:3)do{trytraverse(visitor:&reader)}catch{
if case PBPayloadCommandNameReader.Found.found(let name)= error {return name
}fatalError("Error while traversing: \(error)")}returnnil}}
Having this functionality provided by the library would be incredibly valuable, and would cut out all of this boilerplate code.
The text was updated successfully, but these errors were encountered:
Being able to access this would've allowed me to easily log the name of the payload being processed, as I am using oneof to enumerate all possible IPC payloads. I think this is a more than acceptable use case.
IMO, this data has been sitting in the library for 5 years and counting. It clearly works well enough, even if it is "not quite correct" or a "special-case". If the alternative is reflection, which is not performant, why should this remain an internal API? More importantly, why is the library the only one with easy access to this information?
My workaround uses reflection, building atop of the workaround here:
Having this functionality provided by the library would be incredibly valuable, and would cut out all of this boilerplate code.
The text was updated successfully, but these errors were encountered: