Skip to content

Commit

Permalink
♻️ DataResult,ErrorResult and I18nMessage #48
Browse files Browse the repository at this point in the history
  • Loading branch information
trydofor committed Jan 24, 2025
1 parent 9603e66 commit 33ecb3a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 31 deletions.
15 changes: 7 additions & 8 deletions src/main/java/pro/fessional/mirana/data/ActionResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@
public interface ActionResult extends Serializable {

/**
* the message to the user,
* should be null if empty
*/
@Nullable
String getMessage();

/**
* whether success, default false.
* whether the result is success, default false.
*/
default boolean isSuccess() {
return false;
}

/**
* biz-code/err-code to the caller, should be null if empty
*/
@Nullable
String getCode();
}
8 changes: 1 addition & 7 deletions src/main/java/pro/fessional/mirana/data/DataResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@
public interface DataResult<T> extends ActionResult {

/**
* the biz code to caller
*/
@Nullable
String getCode();

/**
* the biz data to caller
* the biz-data to caller
*/
@Nullable
T getData();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/pro/fessional/mirana/data/ErrorResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/**
* <pre>
* - success is false, not complete because of errors
* - message ars '\n'-joined of errors.message
* - code the err-code more than errors
* - errors should null if error is empty
* </pre>
*
Expand Down
38 changes: 23 additions & 15 deletions src/main/java/pro/fessional/mirana/data/R.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@
*
* 1. if have errors - the service is abnormal and incomplete, returns ErrorResult.
* 2. if no errors - the service is normal and complete, returns DataResult.
* 3. success - the success or failure of the service result.
* 3. success - the success or failure of the result.
* 5. if has code - the biz/err is more clear and detailed.
* 4. if has data - biz-data, regardless of success or failure.
* 3. if has message - should tell to the user or show detailed errors.
* 4. if has data - business data, regardless of success or failure.
* 5. if has code, business logic should be clear and detailed.
*
* ## Basic result container.
*
* - success - whether the result succeeded, default false.
* - code - biz/err code. CodeEnum auto set the code.
* - data - business data in the result.
* - code - business code. CodeEnum auto set the code.
* - errors - errors thrown, no result.
* - errors - errors thrown, success must be false.
* - message - default i18n message or template
* - i18nCode - i18n template code
* - i18nArgs - i18n template args
Expand Down Expand Up @@ -77,11 +77,11 @@ public R() {
}

public R(boolean success) {
this(success, null, (String)null);
this(success, null, (String) null);
}

public R(boolean success, T data) {
this(success, data, (String) null);
this(success, data, (String) null);
}

// ref to this
Expand All @@ -93,27 +93,27 @@ public R(boolean success, T data, String code) {

public R(boolean success, T data, String code, String message) {
this(success, data, code);
super.setMessage(message);
setMessage(message);
}

public R(boolean success, T data, String code, String message, String i18nCode) {
this(success, data, code);
super.setMessageBy(message,i18nCode);
setMessageBy(message, i18nCode);
}

public R(boolean success, T data, String code, String message, String i18nCode, Object... i18nArgs) {
this(success, data, code);
super.setMessageBy(message,i18nCode, i18nArgs);
setMessageBy(message, i18nCode, i18nArgs);
}

public R(boolean success, T data, String code, I18nAware message) {
this(success, data, code);
super.setMessageBy(message);
setMessageBy(message);
}

public R(boolean success, T data, I18nAware message) {
this(success, data);
super.setMessageBy(message);
setMessageBy(message);
}

public R(boolean success, T data, @NotNull CodeEnum code) {
Expand Down Expand Up @@ -205,7 +205,7 @@ public R<T> setCause(Object cause) {
@Transient
@Contract("_->this")
public R<T> setDataIfOk(T data) {
return success ? setData(data): this;
return success ? setData(data) : this;
}

@Transient
Expand Down Expand Up @@ -281,8 +281,16 @@ public R<T> setMessageIfNg(@NotNull Supplier<String> message) {
@Transient
@Contract("_->this")
public R<T> setCodeMessage(@NotNull CodeEnum code) {
this.code = code.getCode();
super.setMessageBy(code);
setCode(code.getCode());
setMessageBy(code);
return this;
}

@Transient
@Contract("_,_->this")
public R<T> setCodeMessage(String code, String message) {
setCode(code);
setMessage(message);
return this;
}

Expand Down

0 comments on commit 33ecb3a

Please sign in to comment.