Moved v3 code from NginxProxyManager/nginx-proxy-manager-3 to NginxProxyManager/nginx-proxy-manager
This commit is contained in:
39
backend/internal/types/db_date.go
Normal file
39
backend/internal/types/db_date.go
Normal file
@ -0,0 +1,39 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
// DBDate is a date time
|
||||
// type DBDate time.Time
|
||||
type DBDate struct {
|
||||
Time time.Time
|
||||
}
|
||||
|
||||
// Value encodes the type ready for the database
|
||||
func (d DBDate) Value() (driver.Value, error) {
|
||||
return driver.Value(d.Time.Unix()), nil
|
||||
}
|
||||
|
||||
// Scan takes data from the database and modifies it for Go Types
|
||||
func (d *DBDate) Scan(src interface{}) error {
|
||||
d.Time = time.Unix(src.(int64), 0)
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalJSON will unmarshal both database and post given values
|
||||
func (d *DBDate) UnmarshalJSON(data []byte) error {
|
||||
var u int64
|
||||
if err := json.Unmarshal(data, &u); err != nil {
|
||||
return err
|
||||
}
|
||||
d.Time = time.Unix(u, 0)
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalJSON will marshal for output in api responses
|
||||
func (d DBDate) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(d.Time.Unix())
|
||||
}
|
71
backend/internal/types/jsonb.go
Normal file
71
backend/internal/types/jsonb.go
Normal file
@ -0,0 +1,71 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// JSONB can be anything
|
||||
type JSONB struct {
|
||||
Encoded string `json:"decoded"`
|
||||
Decoded interface{} `json:"encoded"`
|
||||
}
|
||||
|
||||
// Value encodes the type ready for the database
|
||||
func (j JSONB) Value() (driver.Value, error) {
|
||||
json, err := json.Marshal(j.Decoded)
|
||||
return driver.Value(string(json)), err
|
||||
}
|
||||
|
||||
// Scan takes data from the database and modifies it for Go Types
|
||||
func (j *JSONB) Scan(src interface{}) error {
|
||||
var jsonb JSONB
|
||||
var srcString string
|
||||
switch v := src.(type) {
|
||||
case string:
|
||||
srcString = src.(string)
|
||||
case []uint8:
|
||||
srcString = string(src.([]uint8))
|
||||
default:
|
||||
return fmt.Errorf("Incompatible type for JSONB: %v", v)
|
||||
}
|
||||
|
||||
jsonb.Encoded = srcString
|
||||
|
||||
if err := json.Unmarshal([]byte(srcString), &jsonb.Decoded); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*j = jsonb
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalJSON will unmarshal both database and post given values
|
||||
func (j *JSONB) UnmarshalJSON(data []byte) error {
|
||||
var jsonb JSONB
|
||||
jsonb.Encoded = string(data)
|
||||
if err := json.Unmarshal(data, &jsonb.Decoded); err != nil {
|
||||
return err
|
||||
}
|
||||
*j = jsonb
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalJSON will marshal for output in api responses
|
||||
func (j JSONB) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(j.Decoded)
|
||||
}
|
||||
|
||||
// AsStringArray will attempt to return as []string
|
||||
func (j JSONB) AsStringArray() ([]string, error) {
|
||||
var strs []string
|
||||
|
||||
// Encode then Decode onto this type
|
||||
b, _ := j.MarshalJSON()
|
||||
if err := json.Unmarshal(b, &strs); err != nil {
|
||||
return strs, err
|
||||
}
|
||||
|
||||
return strs, nil
|
||||
}
|
54
backend/internal/types/nullable_db_date.go
Normal file
54
backend/internal/types/nullable_db_date.go
Normal file
@ -0,0 +1,54 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
// NullableDBDate is a date time that can be null in the db
|
||||
// type DBDate time.Time
|
||||
type NullableDBDate struct {
|
||||
Time *time.Time
|
||||
}
|
||||
|
||||
// Value encodes the type ready for the database
|
||||
func (d NullableDBDate) Value() (driver.Value, error) {
|
||||
if d.Time == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return driver.Value(d.Time.Unix()), nil
|
||||
}
|
||||
|
||||
// Scan takes data from the database and modifies it for Go Types
|
||||
func (d *NullableDBDate) Scan(src interface{}) error {
|
||||
var tme time.Time
|
||||
if src != nil {
|
||||
tme = time.Unix(src.(int64), 0)
|
||||
}
|
||||
|
||||
d.Time = &tme
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalJSON will unmarshal both database and post given values
|
||||
func (d *NullableDBDate) UnmarshalJSON(data []byte) error {
|
||||
var t time.Time
|
||||
var u int64
|
||||
if err := json.Unmarshal(data, &u); err != nil {
|
||||
d.Time = &t
|
||||
return nil
|
||||
}
|
||||
t = time.Unix(u, 0)
|
||||
d.Time = &t
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalJSON will marshal for output in api responses
|
||||
func (d NullableDBDate) MarshalJSON() ([]byte, error) {
|
||||
if d.Time == nil || d.Time.IsZero() {
|
||||
return json.Marshal(nil)
|
||||
}
|
||||
|
||||
return json.Marshal(d.Time.Unix())
|
||||
}
|
Reference in New Issue
Block a user