Skip to content

Commit

Permalink
♻️ refactor: refactor HTTPWrapBuuilder add function custom fields #3
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed May 27, 2024
1 parent 8046ef7 commit 5be14d3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.unify4j.model.builder;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.unify4j.common.Collection4j;
import org.unify4j.model.response.HttpResponse;
import org.unify4j.model.response.MetaResponse;
import org.unify4j.model.response.PaginationResponse;
import org.unify4j.model.response.WrapResponse;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

Expand Down Expand Up @@ -57,6 +59,21 @@ public HttpWrapBuilder<T> customFields(Map<String, ?> fields) {
return this;
}

@SuppressWarnings({"unchecked"})
public HttpWrapBuilder<T> customFields(String key, Object value) {
Map<String, Object> customFields = (Map<String, Object>) this.build().getMeta().getCustomFields();
if (Collection4j.isEmptyMap(customFields)) {
customFields = new HashMap<>();
}
customFields.put(key, value);
return this.customFields(customFields);
}

public HttpWrapBuilder<T> meta(MetaBuilder builder) {
this.builder().setMeta(builder.build());
return this;
}

public HttpWrapBuilder<T> statusCode(HttpResponse status) {
this.builder().setStatusCode(status.getCode());
this.builder().setHeaders(status);
Expand Down
22 changes: 16 additions & 6 deletions plugin/src/main/groovy/org/unify4j/model/builder/MetaBuilder.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.unify4j.model.builder;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.unify4j.common.Collection4j;
import org.unify4j.common.Random4j;
import org.unify4j.model.response.MetaResponse;

import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

Expand All @@ -14,33 +16,41 @@ public class MetaBuilder {
private String requestId = Random4j.nextUUIDString();
private String locale = Locale.getDefault().getCountry();
private Date requestedTime = new Date();
private Map<String, ?> customFields;
private Map<String, Object> customFields;

public MetaBuilder setApiVersion(String apiVersion) {
public MetaBuilder apiVersion(String apiVersion) {
this.apiVersion = apiVersion;
return this;
}

public MetaBuilder setRequestId(String requestId) {
public MetaBuilder requestId(String requestId) {
this.requestId = requestId;
return this;
}

public MetaBuilder setLocale(String locale) {
public MetaBuilder locale(String locale) {
this.locale = locale;
return this;
}

public MetaBuilder setRequestedTime(Date requestedTime) {
public MetaBuilder requestedTime(Date requestedTime) {
this.requestedTime = requestedTime;
return this;
}

public MetaBuilder setCustomFields(Map<String, ?> customFields) {
public MetaBuilder customFields(Map<String, Object> customFields) {
this.customFields = customFields;
return this;
}

public MetaBuilder appendCustomField(String key, Object value) {
if (Collection4j.isEmptyMap(this.customFields)) {
this.customFields = new HashMap<>();
}
this.customFields.put(key, value);
return this;
}

public MetaResponse build() {
MetaResponse e = new MetaResponse();
e.setApiVersion(apiVersion);
Expand Down
14 changes: 14 additions & 0 deletions plugin/src/test/groovy/org/unify4j/HttpWrapBuilderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.unify4j;

import org.junit.Test;
import org.unify4j.model.builder.HttpWrapBuilder;

public class HttpWrapBuilderTest {

@Test
public void testBuilder() {
HttpWrapBuilder<String> builder = new HttpWrapBuilder<>();
builder.customFields("name", 112).customFields("age", 28);
System.out.println(builder.build());
}
}

0 comments on commit 5be14d3

Please sign in to comment.