generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathparams.go
69 lines (61 loc) · 2.35 KB
/
params.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package datastore
import (
"encoding/json"
"github.com/99designs/gqlgen/graphql"
)
// QueryParams object to use when limiting and sorting database query results
type QueryParams struct {
Page int `json:"page,omitempty"`
PageSize int `json:"page_size,omitempty"`
OrderByField string `json:"order_by_field,omitempty"`
SortDirection string `json:"sort_direction,omitempty"`
}
// MarshalQueryParams will marshal the QueryParams struct into a GraphQL marshaler.
// If all fields of the QueryParams struct are empty or zero, it returns a GraphQL null value.
//
// Parameters:
// - m: The QueryParams struct to be marshaled.
//
// Returns:
// - A graphql.Marshaler that represents the marshaled QueryParams struct.
//
// The function performs the following steps:
// 1. Checks if all fields of the QueryParams struct are empty or zero.
// 2. If all fields are empty or zero, returns graphql.Null.
// 3. Otherwise, marshals the QueryParams struct into a generic GraphQL marshaler using graphql.MarshalAny.
func MarshalQueryParams(m QueryParams) graphql.Marshaler {
if m.Page == 0 && m.PageSize == 0 && m.OrderByField == "" && m.SortDirection == "" {
return graphql.Null
}
return graphql.MarshalAny(m)
}
// UnmarshalQueryParams will unmarshal the provided interface into a QueryParams struct.
// It handles the conversion from a generic interface to the specific QueryParams type,
// ensuring that the data is correctly parsed and assigned to the struct fields.
//
// Parameters:
// - v: The interface{} to be unmarshaled. It is expected to be a map or a JSON-like structure.
//
// Returns:
// - QueryParams: The unmarshaled QueryParams struct with the parsed data.
// - error: An error if the unmarshaling process fails.
//
// The function performs the following steps:
// 1. Checks if the provided interface is nil, returning an empty QueryParams struct if true.
// 2. Marshals the interface into a JSON byte slice.
// 3. Unmarshals the JSON byte slice into a QueryParams struct.
// 4. Returns the populated QueryParams struct and any error encountered during the process.
func UnmarshalQueryParams(v interface{}) (QueryParams, error) {
if v == nil {
return QueryParams{}, nil
}
data, err := json.Marshal(v)
if err != nil {
return QueryParams{}, err
}
var q QueryParams
if err = json.Unmarshal(data, &q); err != nil {
return QueryParams{}, err
}
return q, nil
}