1
0
mirror of https://github.com/haiwen/seafile-server.git synced 2025-09-15 22:59:43 +00:00

Don't omit commit id when marshal commit to json (#491)

This commit is contained in:
Xiangyue Cai
2021-09-23 14:12:53 +08:00
committed by GitHub
parent 88e917c58e
commit c31dcc79ce
7 changed files with 127 additions and 13 deletions

View File

@@ -22,8 +22,8 @@ type Commit struct {
CreatorID string `json:"creator"`
Desc string `json:"description"`
Ctime int64 `json:"ctime"`
ParentID string `json:"parent_id,omitempty"`
SecondParentID string `json:"second_parent_id,omitempty"`
ParentID String `json:"parent_id"`
SecondParentID String `json:"second_parent_id"`
RepoName string `json:"repo_name"`
RepoDesc string `json:"repo_desc"`
RepoCategory string `json:"repo_category"`
@@ -57,7 +57,7 @@ func NewCommit(repoID, parentID, newRoot, user, desc string) *Commit {
commit.CreatorID = "0000000000000000000000000000000000000000"
commit.Ctime = time.Now().Unix()
commit.CommitID = computeCommitID(commit)
commit.ParentID = parentID
commit.ParentID.SetValid(parentID)
return commit
}

View File

@@ -48,7 +48,7 @@ func TestCommit(t *testing.T) {
newCommit.CreatorID = commitID
newCommit.Desc = "This is a commit"
newCommit.Ctime = time.Now().Unix()
newCommit.ParentID = commitID
newCommit.ParentID.SetValid(commitID)
newCommit.DeviceName = "Linux"
err := Save(newCommit)
if err != nil {

View File

@@ -0,0 +1,114 @@
package commitmgr
import (
"bytes"
"database/sql"
"encoding/json"
"fmt"
)
// nullBytes is a JSON null literal
var nullBytes = []byte("null")
// String is a nullable string. It supports SQL and JSON serialization.
// It will marshal to null if null. Blank string input will be considered null.
type String struct {
sql.NullString
}
// StringFrom creates a new String that will never be blank.
func StringFrom(s string) String {
return NewString(s, true)
}
// StringFromPtr creates a new String that be null if s is nil.
func StringFromPtr(s *string) String {
if s == nil {
return NewString("", false)
}
return NewString(*s, true)
}
// ValueOrZero returns the inner value if valid, otherwise zero.
func (s String) ValueOrZero() string {
if !s.Valid {
return ""
}
return s.String
}
// NewString creates a new String
func NewString(s string, valid bool) String {
return String{
NullString: sql.NullString{
String: s,
Valid: valid,
},
}
}
// UnmarshalJSON implements json.Unmarshaler.
// It supports string and null input. Blank string input does not produce a null String.
func (s *String) UnmarshalJSON(data []byte) error {
if bytes.Equal(data, nullBytes) {
s.Valid = false
return nil
}
if err := json.Unmarshal(data, &s.String); err != nil {
return fmt.Errorf("null: couldn't unmarshal JSON: %w", err)
}
s.Valid = true
return nil
}
// MarshalJSON implements json.Marshaler.
// It will encode null if this String is null.
func (s String) MarshalJSON() ([]byte, error) {
if !s.Valid {
return []byte("null"), nil
}
return json.Marshal(s.String)
}
// MarshalText implements encoding.TextMarshaler.
// It will encode a blank string when this String is null.
func (s String) MarshalText() ([]byte, error) {
if !s.Valid {
return []byte{}, nil
}
return []byte(s.String), nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
// It will unmarshal to a null String if the input is a blank string.
func (s *String) UnmarshalText(text []byte) error {
s.String = string(text)
s.Valid = s.String != ""
return nil
}
// SetValid changes this String's value and also sets it to be non-null.
func (s *String) SetValid(v string) {
s.String = v
s.Valid = true
}
// Ptr returns a pointer to this String's value, or a nil pointer if this String is null.
func (s String) Ptr() *string {
if !s.Valid {
return nil
}
return &s.String
}
// IsZero returns true for null strings, for potential future omitempty support.
func (s String) IsZero() bool {
return !s.Valid
}
// Equal returns true if both strings have the same value or are both null.
func (s String) Equal(other String) bool {
return s.Valid == other.Valid && (!s.Valid || s.String == other.String)
}