yakworks.message.MsgKeyTrait.groovy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of api-messages Show documentation
Show all versions of api-messages Show documentation
Models and SPI for message service, api results and problems.
/*
* Copyright 2019 original authors
* SPDX-License-Identifier: Apache-2.0
*/
package yakworks.message
import groovy.transform.CompileStatic
/**
* Trait implementation for MsgKey to allow easy ability to make anything in groovy a MsgKey
*/
@CompileStatic
trait MsgKeyTrait implements MsgKey {
String code
MsgArgs args
String fallbackMessage
E code(String code){ this.code = code; return (E)this;}
E args(Object args){
this.args = MsgArgs.of(args)
return (E)this
}
E args(MsgArgs args){
this.args = args
return (E)this
}
/**
* If one is set then return it,
* if not it looks at args and if its a map then returns the defaultMessage key if it exists
*/
@Override
String getFallbackMessage(){
return (fallbackMessage != null) ? fallbackMessage : getArgs().getFallbackMessage();
}
E fallbackMessage(String defMsg){ fallbackMessage = defMsg; return (E)this;}
}