Accessor
Turn any object into a statically typed facade.
Source code is hosted on GitHub
This library allows you to create a statically typed object accessor based on the Proxy API. The main purpose of this package is to allow dynamic object access in a static manner, without having to rely on dynamic, string based keys. Code won't compile if you try to access a path that does not exist. You keep control over what is returned from the getter, by providing a custom access handler.
createAccessor()
Create an accessor instance of the type ObjectAccessor
.
Now you can statically access every object field:
You can provide a custom access handler to customise the access behaviour. Below is an example of how one could statically access translations, proxied by a translation library, using the accessor.
Now you can indirectly access translations through the accessor, without using any dynamic keys.
Trying to access a missing key will result in a compilation error. 💥
This library is used internally by this translation library, to power its static translations access:
ObjectAccessor.key()
Get absolute field key:
ObjectAccessor.keyAt()
Get absolute field key, bypassing the static typing:
ObjectAccessor.get()
Get field value coming from the access handler:
ObjectAccessor.getAt()
Get field value coming from the access handler, bypassing the static typing:
ObjectAccessor.has()
Check if a field exists:
ObjectAccessor.hasAt()
Check if a field exists, bypassing the static typing:
Last updated