Get class property position in storage $lb
I have a classname and a property name. I want to know, when I get object value directly from a global, which property corresponds to which position in the $lb structure.
Here's what I got so far:
Class Utils.Storage { /// write ##class(Utils.Storage).GetPosition() ClassMethod GetPosition(class As %Dictionary.CacheClassname = "Sample.Address", property As %Dictionary.CacheIdentifier = "Zip") As %Integer { set strategy = $$$comClassKeyGet(class, $$$cCLASSstoragestrategy) set strategyId = class _ "||" _ strategy &sql(SELECT Name INTO :position FROM %Dictionary.StorageDataValueDefinition WHERE parent = (SELECT ID FROM %Dictionary.StorageDataDefinition WHERE parent = :strategyId AND Structure = 'listnode') AND Value = :property) quit position } /// do ##class(Utils.Storage).Test() ClassMethod Test() { set list = $lb("Street", "City", "State", "Zip") set obj = ##class(Sample.Address).%New() Do obj.%SetSerial(list) set list2 = obj.%GetSerial() zw obj, list2 } }
Work example:
write ##class(Utils.Storage).GetPosition("Sample.Address","City") >2
It should work for everything, except:
- Array Of properties in persistent classes with default array STORAGEDEFAULT
- List Of properties in persistent classes with non-default array STORAGEDEFAULT
- Custom storage
- SQL storage
- Relationships
And that's okay, I only really need it for serial classes. But is there maybe a better way to get it?
UPD: Should probably check how it works with inheritance.