-
Notifications
You must be signed in to change notification settings - Fork 409
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Better customization of Leshan server #1348
Comments
I totally agree that currently is is not so easy to tweak The remaining question is how to do that ? Adding a new Interface for Base64 Encoder/Decoder make sense to me (more details at #1325 (comment)). About create 2 new builders, I'm not sure.
When you have lot of parameter and complicate configuration, it's generally easy to favor the builder pattern. WDYT ? |
Yes it’s true that the builder pattern might be a bit cumbersome for those two classes, since they have very few dependencies and probably won't evolve too much in the future. In this case I guess constructor injection might be enough to meet the need. Something like: private static LwM2mEncoder buildPatchedEncoder() {
Map<ContentFormat, NodeEncoder> nodeEncoders = DefaultLwM2mEncoder.getDefaultNodeEncoders(false);
nodeEncoders.put(ContentFormat.SENML_JSON, new LwM2mNodeSenMLEncoder(new SenMLJsonJacksonEncoderDecoder(
new SenMLJsonRecordSerDes(new CustomBase64Encoder())
)));
return new DefaultLwM2mEncoder(nodeEncoders, DefaultLwM2mEncoder.getDefaultPathEncoder(), new LwM2mValueChecker());
} Adding a few constructors seems light enough, for now. The only problem is that this pattern does not scale very well. For instance, regarding new SenMLJsonJacksonEncoderDecoder(
new SenMLJsonRecordSerDes(new CustomBase64Encoder()),
SenMLJsonJacksonEncoderDecoder.DEFAULT_OBJECT_MAPPER,
SenMLJsonJacksonEncoderDecoder.DEFAULT_NO_VALUE_OPTION
))); We could use Lombok for generic constructors, but I guess it's too late to integrate it in the code since we have many other builders, including some custom ones. |
I didn't know lombol could you share a link ? My first thought : I feel 3 constructor is maybe ok. The 2 existing one + one with all parameters. |
|
I prefer to not add a dependency just for this. (And as soon as you want to customize your builder, I guess this will be complicated) |
Done with : #1372 |
#1372 integrated it in |
Hi, I wanted to discuss a better way we could structure Leshan code in order to make it easier to swap certain components for their custom implementations. As an example, I'll describe adding a custom Base64 encoder/decoder to
SenMLJsonRecordSerDes
class - this is a throwback to #1325 where we discussed new API for encoding and decoding.Let's say that on our side we want to inject a custom
Base64Encoder
andBase64Decoder
implementations. Customization starts with theLeshanServerBuilder
, which has the required setter for new encoders:Then we have to rebuild our own
SenMLJsonJacksonEncoderDecoder
:Note that there is almost no change in
SenMLJsonJacksonEncoderDecoder
code with respect to the Leshan version, except we inject a customSenMLJsonRecordSerDes
during construction:And finally in our custom version of
SenMLJsonRecordSerDes
, we basically have 2 lines of code to change with respect to the Leshan version (related to OPAQUE fields encoding and decoding):and
This is a lot of code duplication in both
SenMLJsonJacksonEncoderDecoder
andSenMLJsonRecordSerDes
, the only reason being that the Base64 decoder is not easily “reachable” during server config. That’s why it would be helpful to extend more widely the builder pattern that helps constructing the Leshan server.As you know, the
LeshanServerBuilder
exposes setters for a number of internal components, and if no custom implementation is provided for a component, switches to default implementation whenbuild()
is called. Unfortunately this builder pattern does not exist for other components. For instance, it would be convenient to be able to do this in order to inject new encoders/decoders:With a
SenMLJsonJacksonEncoderDecoderBuilder
and aSenMLJsonRecordSerDesBuilder
, there is no code duplication, and we are able to inject directly our own Base64 encoder/decoder. What do you think about this idea?The text was updated successfully, but these errors were encountered: