Skip to content

Commit

Permalink
🔇 silent changes: add base functions #4
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Dec 7, 2024
1 parent 41ad69a commit 3f3ebcc
Showing 1 changed file with 120 additions and 1 deletion.
121 changes: 120 additions & 1 deletion init.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,31 @@ func (m *meta) JsonPretty() string {
return unify4g.JsonPrettyN(m.Respond())
}

// Json serializes the `header` instance into a compact JSON string.
//
// This function uses the `unify4g.JsonN` utility to create a compact JSON representation
// of the `header` instance. The resulting string contains only the key information, formatted
// with minimal whitespace, making it suitable for compact storage or transmission of header data.
//
// Returns:
// - A compact JSON string representation of the `header` instance.
func (h *header) Json() string {
return unify4g.JsonN(h.Respond())
}

// JsonPretty serializes the `header` instance into a prettified JSON string.
//
// This function uses the `unify4g.JsonPrettyN` utility to produce a formatted, human-readable
// JSON string representation of the `header` instance. The output is structured with indentation
// and newlines, making it ideal for inspecting header data in a clear, easy-to-read format, especially
// during debugging or development.
//
// Returns:
// - A prettified JSON string representation of the `header` instance, formatted for improved readability.
func (h *header) JsonPretty() string {
return unify4g.JsonPrettyN(h.Respond())
}

// WithPage sets the page number for the `pagination` instance.
//
// This function updates the `page` field of the `pagination` and
Expand Down Expand Up @@ -296,6 +321,66 @@ func (m *meta) WithCustomFieldKV(key string, value interface{}) *meta {
return m
}

// WithCode sets the `code` field of the `header` instance.
//
// This function assigns the provided integer value to the `code` field of the `header`
// and returns the updated `header` instance, allowing for method chaining.
//
// Parameters:
// - `v`: The integer value to set as the HTTP status code.
//
// Returns:
// - The updated `header` instance with the `code` field set to the provided value.
func (h *header) WithCode(v int) *header {
h.code = v
return h
}

// WithText sets the `text` field of the `header` instance.
//
// This function assigns the provided string value to the `text` field of the `header`
// and returns the updated `header` instance, allowing for method chaining.
//
// Parameters:
// - `v`: The string value to set as the text message.
//
// Returns:
// - The updated `header` instance with the `text` field set to the provided value.
func (h *header) WithText(v string) *header {
h.text = v
return h
}

// WithType sets the `Type` field of the `header` instance.
//
// This function assigns the provided string value to the `Type` field of the `header`
// and returns the updated `header` instance, allowing for method chaining.
//
// Parameters:
// - `v`: The string value to set as the type of the header.
//
// Returns:
// - The updated `header` instance with the `Type` field set to the provided value.
func (h *header) WithType(v string) *header {
h.Type = v
return h
}

// WithDescription sets the `description` field of the `header` instance.
//
// This function assigns the provided string value to the `description` field of the `header`
// and returns the updated `header` instance, allowing for method chaining.
//
// Parameters:
// - `v`: The string value to set as the description of the header.
//
// Returns:
// - The updated `header` instance with the `description` field set to the provided value.
func (h *header) WithDescription(v string) *header {
h.description = v
return h
}

// WithStatusCode sets the HTTP status code for the `wrapper` instance.
//
// This function updates the `statusCode` field of the `wrapper` and
Expand Down Expand Up @@ -605,7 +690,7 @@ func (w *wrapper) Respond() map[string]interface{} {
m["data"] = w.data
}
if w.IsHeaderPresent() {
m["headers"] = w.header
m["headers"] = w.header.Respond()
}
if w.IsMetaPresent() {
m["meta"] = w.meta.Respond()
Expand Down Expand Up @@ -696,3 +781,37 @@ func (m *meta) Respond() map[string]interface{} {
}
return mk
}

// Respond generates a map representation of the `header` instance.
//
// This function checks if the `header` instance is available (non-nil) and includes the
// values of its fields in the returned map. Only the fields that are present (i.e., non-empty)
// are added to the map, ensuring a clean and concise response.
//
// Fields included in the response:
// - `code`: The HTTP status code, if present and greater than 0.
// - `text`: The associated text message, if present and not empty.
// - `type`: The type of the header, if present and not empty.
// - `description`: A description related to the header, if present and not empty.
//
// Returns:
// - A `map[string]interface{}` containing the fields of the `header` instance that are present.
func (h *header) Respond() map[string]interface{} {
m := make(map[string]interface{})
if !h.Available() {
return m
}
if h.IsCodePresent() {
m["code"] = h.code
}
if h.IsTextPresent() {
m["text"] = h.text
}
if h.IsTypePresent() {
m["type"] = h.Type
}
if h.IsDescriptionPresent() {
m["description"] = h.description
}
return m
}

0 comments on commit 3f3ebcc

Please sign in to comment.