lib/api: Unify JSON marshalling of file infos (#6087)

This commit is contained in:
Simon Frei 2019-10-15 11:25:12 +02:00 committed by Jakob Borg
parent aa4b918224
commit a0c9db1d09
5 changed files with 60 additions and 40 deletions

1
go.mod
View File

@ -45,7 +45,6 @@ require (
github.com/vitrun/qart v0.0.0-20160531060029-bf64b92db6b0 github.com/vitrun/qart v0.0.0-20160531060029-bf64b92db6b0
golang.org/x/crypto v0.0.0-20190829043050-9756ffdc2472 golang.org/x/crypto v0.0.0-20190829043050-9756ffdc2472
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297 golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297
golang.org/x/sys v0.0.0-20190904154756-749cb33beabd // indirect
golang.org/x/text v0.3.2 golang.org/x/text v0.3.2
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 golang.org/x/time v0.0.0-20190308202827-9d24e82272b4
gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d // indirect gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d // indirect

View File

@ -1573,10 +1573,10 @@ func (s *service) getHeapProf(w http.ResponseWriter, r *http.Request) {
pprof.WriteHeapProfile(w) pprof.WriteHeapProfile(w)
} }
func toJsonFileInfoSlice(fs []db.FileInfoTruncated) []jsonDBFileInfo { func toJsonFileInfoSlice(fs []db.FileInfoTruncated) []jsonFileInfoTrunc {
res := make([]jsonDBFileInfo, len(fs)) res := make([]jsonFileInfoTrunc, len(fs))
for i, f := range fs { for i, f := range fs {
res[i] = jsonDBFileInfo(f) res[i] = jsonFileInfoTrunc(f)
} }
return res return res
} }
@ -1586,45 +1586,39 @@ func toJsonFileInfoSlice(fs []db.FileInfoTruncated) []jsonDBFileInfo {
type jsonFileInfo protocol.FileInfo type jsonFileInfo protocol.FileInfo
func (f jsonFileInfo) MarshalJSON() ([]byte, error) { func (f jsonFileInfo) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{ m := fileIntfJSONMap(protocol.FileInfo(f))
"name": f.Name, m["numBlocks"] = len(f.Blocks)
"type": f.Type, return json.Marshal(m)
"size": f.Size,
"permissions": fmt.Sprintf("%#o", f.Permissions),
"deleted": f.Deleted,
"invalid": protocol.FileInfo(f).IsInvalid(),
"ignored": protocol.FileInfo(f).IsIgnored(),
"mustRescan": protocol.FileInfo(f).MustRescan(),
"noPermissions": f.NoPermissions,
"modified": protocol.FileInfo(f).ModTime(),
"modifiedBy": f.ModifiedBy.String(),
"sequence": f.Sequence,
"numBlocks": len(f.Blocks),
"version": jsonVersionVector(f.Version),
"localFlags": f.LocalFlags,
})
} }
type jsonDBFileInfo db.FileInfoTruncated type jsonFileInfoTrunc db.FileInfoTruncated
func (f jsonDBFileInfo) MarshalJSON() ([]byte, error) { func (f jsonFileInfoTrunc) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{ m := fileIntfJSONMap(db.FileInfoTruncated(f))
"name": f.Name, m["numBlocks"] = nil // explicitly unknown
"type": f.Type.String(), return json.Marshal(m)
"size": f.Size, }
"permissions": fmt.Sprintf("%#o", f.Permissions),
"deleted": f.Deleted, func fileIntfJSONMap(f db.FileIntf) map[string]interface{} {
"invalid": db.FileInfoTruncated(f).IsInvalid(), out := map[string]interface{}{
"ignored": db.FileInfoTruncated(f).IsIgnored(), "name": f.FileName(),
"mustRescan": db.FileInfoTruncated(f).MustRescan(), "type": f.FileType().String(),
"noPermissions": f.NoPermissions, "size": f.FileSize(),
"modified": db.FileInfoTruncated(f).ModTime(), "deleted": f.IsDeleted(),
"modifiedBy": f.ModifiedBy.String(), "invalid": f.IsInvalid(),
"sequence": f.Sequence, "ignored": f.IsIgnored(),
"numBlocks": nil, // explicitly unknown "mustRescan": f.MustRescan(),
"version": jsonVersionVector(f.Version), "noPermissions": !f.HasPermissionBits(),
"localFlags": f.LocalFlags, "modified": f.ModTime(),
}) "modifiedBy": f.FileModifiedBy().String(),
"sequence": f.SequenceNo(),
"version": jsonVersionVector(f.FileVersion()),
"localFlags": f.FileLocalFlags(),
}
if f.HasPermissionBits() {
out["permissions"] = fmt.Sprintf("%#o", f.FilePermissions())
}
return out
} }
type jsonVersionVector protocol.Vector type jsonVersionVector protocol.Vector

View File

@ -51,6 +51,10 @@ type FileIntf interface {
SequenceNo() int64 SequenceNo() int64
BlockSize() int BlockSize() int
FileVersion() protocol.Vector FileVersion() protocol.Vector
FileType() protocol.FileInfoType
FilePermissions() uint32
FileModifiedBy() protocol.ShortID
ModTime() time.Time
} }
// The Iterator is called with either a protocol.FileInfo or a // The Iterator is called with either a protocol.FileInfo or a

View File

@ -116,6 +116,17 @@ func (f FileInfoTruncated) FileVersion() protocol.Vector {
return f.Version return f.Version
} }
func (f FileInfoTruncated) FileType() protocol.FileInfoType {
return f.Type
}
func (f FileInfoTruncated) FilePermissions() uint32 {
return f.Permissions
}
func (f FileInfoTruncated) FileModifiedBy() protocol.ShortID {
return f.ModifiedBy
}
func (f FileInfoTruncated) ConvertToIgnoredFileInfo(by protocol.ShortID) protocol.FileInfo { func (f FileInfoTruncated) ConvertToIgnoredFileInfo(by protocol.ShortID) protocol.FileInfo {
return protocol.FileInfo{ return protocol.FileInfo{
Name: f.Name, Name: f.Name,

View File

@ -124,6 +124,18 @@ func (f FileInfo) FileVersion() Vector {
return f.Version return f.Version
} }
func (f FileInfo) FileType() FileInfoType {
return f.Type
}
func (f FileInfo) FilePermissions() uint32 {
return f.Permissions
}
func (f FileInfo) FileModifiedBy() ShortID {
return f.ModifiedBy
}
// WinsConflict returns true if "f" is the one to choose when it is in // WinsConflict returns true if "f" is the one to choose when it is in
// conflict with "other". // conflict with "other".
func (f FileInfo) WinsConflict(other FileInfo) bool { func (f FileInfo) WinsConflict(other FileInfo) bool {