feat: establish Orbit models, migrations and development baseline
This commit is contained in:
58
model/json.go
Normal file
58
model/json.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// JSON 是经过合法性检查的通用文档。输入内容应使用版本化结构或业务侧引用,
|
||||
// Orbit 不解释行业专用字段。
|
||||
type JSON json.RawMessage
|
||||
|
||||
func (j JSON) Value() (driver.Value, error) {
|
||||
if j == nil {
|
||||
return nil, nil
|
||||
}
|
||||
if !json.Valid(j) {
|
||||
return nil, errors.New("invalid JSON document")
|
||||
}
|
||||
return []byte(j), nil
|
||||
}
|
||||
func (j *JSON) Scan(src any) error {
|
||||
if src == nil {
|
||||
*j = nil
|
||||
return nil
|
||||
}
|
||||
var b []byte
|
||||
switch v := src.(type) {
|
||||
case []byte:
|
||||
b = v
|
||||
case string:
|
||||
b = []byte(v)
|
||||
default:
|
||||
return fmt.Errorf("JSON: unsupported source %T", src)
|
||||
}
|
||||
if !json.Valid(b) {
|
||||
return errors.New("invalid JSON document")
|
||||
}
|
||||
*j = append((*j)[:0], b...)
|
||||
return nil
|
||||
}
|
||||
func (j JSON) MarshalJSON() ([]byte, error) {
|
||||
if j == nil {
|
||||
return []byte("null"), nil
|
||||
}
|
||||
if !json.Valid(j) {
|
||||
return nil, errors.New("invalid JSON document")
|
||||
}
|
||||
return []byte(j), nil
|
||||
}
|
||||
func (j *JSON) UnmarshalJSON(b []byte) error {
|
||||
if !json.Valid(b) {
|
||||
return errors.New("invalid JSON document")
|
||||
}
|
||||
*j = append((*j)[:0], b...)
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user