From 5be14d36e7e18f0d94de338734029803ce74ebab Mon Sep 17 00:00:00 2001 From: arisnguyenit97 Date: Mon, 27 May 2024 21:34:21 +0700 Subject: [PATCH] :recycle: refactor: refactor HTTPWrapBuuilder add function custom fields #3 --- .../model/builder/HttpWrapBuilder.java | 17 ++++++++++++++ .../unify4j/model/builder/MetaBuilder.java | 22 ++++++++++++++----- .../org/unify4j/HttpWrapBuilderTest.java | 14 ++++++++++++ 3 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 plugin/src/test/groovy/org/unify4j/HttpWrapBuilderTest.java diff --git a/plugin/src/main/groovy/org/unify4j/model/builder/HttpWrapBuilder.java b/plugin/src/main/groovy/org/unify4j/model/builder/HttpWrapBuilder.java index 1752d5f..f014d5f 100644 --- a/plugin/src/main/groovy/org/unify4j/model/builder/HttpWrapBuilder.java +++ b/plugin/src/main/groovy/org/unify4j/model/builder/HttpWrapBuilder.java @@ -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; @@ -57,6 +59,21 @@ public HttpWrapBuilder customFields(Map fields) { return this; } + @SuppressWarnings({"unchecked"}) + public HttpWrapBuilder customFields(String key, Object value) { + Map customFields = (Map) this.build().getMeta().getCustomFields(); + if (Collection4j.isEmptyMap(customFields)) { + customFields = new HashMap<>(); + } + customFields.put(key, value); + return this.customFields(customFields); + } + + public HttpWrapBuilder meta(MetaBuilder builder) { + this.builder().setMeta(builder.build()); + return this; + } + public HttpWrapBuilder statusCode(HttpResponse status) { this.builder().setStatusCode(status.getCode()); this.builder().setHeaders(status); diff --git a/plugin/src/main/groovy/org/unify4j/model/builder/MetaBuilder.java b/plugin/src/main/groovy/org/unify4j/model/builder/MetaBuilder.java index fb18ae0..ef79757 100644 --- a/plugin/src/main/groovy/org/unify4j/model/builder/MetaBuilder.java +++ b/plugin/src/main/groovy/org/unify4j/model/builder/MetaBuilder.java @@ -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; @@ -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 customFields; + private Map 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 customFields) { + public MetaBuilder customFields(Map 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); diff --git a/plugin/src/test/groovy/org/unify4j/HttpWrapBuilderTest.java b/plugin/src/test/groovy/org/unify4j/HttpWrapBuilderTest.java new file mode 100644 index 0000000..308ff4a --- /dev/null +++ b/plugin/src/test/groovy/org/unify4j/HttpWrapBuilderTest.java @@ -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 builder = new HttpWrapBuilder<>(); + builder.customFields("name", 112).customFields("age", 28); + System.out.println(builder.build()); + } +}