I'm working on an IoT service that needs to be able to send messages via some kind of protocol. My goal is to make it easy for me to swap between these protocol implementations.
In IoT two common protocols that are used for messaging are MQTT and WAMP. Both popular Go libraries for these protocols have similar interfaces, however different parameters for their respective function implementations.
Example:
MQTT
Publish(topic string, qos byte, retained bool, payload interface{}) Token
WAMP
Publish(topic string, options wamp.Dict, args wamp.List, kwargs wamp.Dict) error
My initial idea was to define a generic interface where I filter out all the parameters I really need:
Publish(topic string, payload map[string]interface{}, options map[string]interface{}) map[string]interface{}
However since for both libraries, the signatures are quite different, I am be forced to use the interface{}
type. Which in turn would force me to do a lot of type assertion magic and extra work to make this work properly.
Am I completely missing something obvious? What would be a better solution to this problem? Or should I tackle this problem completely differently.
question from:
https://stackoverflow.com/questions/65834524/generic-interface-for-api-with-common-functionality-but-different-function-param 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…