fix: simplify property assignment

This commit is contained in:
Juan Pablo Villafáñez
2024-06-21 10:51:04 +02:00
parent b4e09e34ae
commit cfc39fac24
4 changed files with 265 additions and 234 deletions
@@ -82,45 +82,68 @@ type Collabora struct {
// SetProperties will set the file properties for the Collabora implementation. // SetProperties will set the file properties for the Collabora implementation.
func (cinfo *Collabora) SetProperties(props map[string]interface{}) { func (cinfo *Collabora) SetProperties(props map[string]interface{}) {
setters := map[string]func(value interface{}){ for key, value := range props {
"BaseFileName": assignStringTo(&cinfo.BaseFileName), switch key {
"DisablePrint": assignBoolTo(&cinfo.DisablePrint), case "BaseFileName":
"OwnerID": assignStringTo(&cinfo.OwnerID), cinfo.BaseFileName = value.(string)
"PostMessageOrigin": assignStringTo(&cinfo.PostMessageOrigin), case "DisablePrint":
"Size": assignInt64To(&cinfo.Size), cinfo.DisablePrint = value.(bool)
"TemplateSource": assignStringTo(&cinfo.TemplateSource), case "OwnerID":
"UserCanWrite": assignBoolTo(&cinfo.UserCanWrite), cinfo.OwnerID = value.(string)
"UserCanNotWriteRelative": assignBoolTo(&cinfo.UserCanNotWriteRelative), case "PostMessageOrigin":
"UserID": assignStringTo(&cinfo.UserID), cinfo.PostMessageOrigin = value.(string)
"UserFriendlyName": assignStringTo(&cinfo.UserFriendlyName), case "Size":
cinfo.Size = value.(int64)
case "TemplateSource":
cinfo.TemplateSource = value.(string)
case "UserCanWrite":
cinfo.UserCanWrite = value.(bool)
case "UserCanNotWriteRelative":
cinfo.UserCanNotWriteRelative = value.(bool)
case "UserID":
cinfo.UserID = value.(string)
case "UserFriendlyName":
cinfo.UserFriendlyName = value.(string)
"EnableInsertRemoteImage": assignBoolTo(&cinfo.EnableInsertRemoteImage), case "EnableInsertRemoteImage":
"DisableInsertLocalImage": assignBoolTo(&cinfo.DisableInsertLocalImage), cinfo.EnableInsertRemoteImage = value.(bool)
"HidePrintOption": assignBoolTo(&cinfo.HidePrintOption), case "DisableInsertLocalImage":
"HideSaveOption": assignBoolTo(&cinfo.HideSaveOption), cinfo.DisableInsertLocalImage = value.(bool)
"HideExportOption": assignBoolTo(&cinfo.HideExportOption), case "HidePrintOption":
"DisableExport": assignBoolTo(&cinfo.DisableExport), cinfo.HidePrintOption = value.(bool)
"DisableCopy": assignBoolTo(&cinfo.DisableCopy), case "HideSaveOption":
"DisableInactiveMessages": assignBoolTo(&cinfo.DisableInactiveMessages), cinfo.HideSaveOption = value.(bool)
"DownloadAsPostMessage": assignBoolTo(&cinfo.DownloadAsPostMessage), case "HideExportOption":
"SaveAsPostmessage": assignBoolTo(&cinfo.SaveAsPostmessage), cinfo.HideExportOption = value.(bool)
"EnableOwnerTermination": assignBoolTo(&cinfo.EnableOwnerTermination), case "DisableExport":
cinfo.DisableExport = value.(bool)
case "DisableCopy":
cinfo.DisableCopy = value.(bool)
case "DisableInactiveMessages":
cinfo.DisableInactiveMessages = value.(bool)
case "DownloadAsPostMessage":
cinfo.DownloadAsPostMessage = value.(bool)
case "SaveAsPostmessage":
cinfo.SaveAsPostmessage = value.(bool)
case "EnableOwnerTermination":
cinfo.EnableOwnerTermination = value.(bool)
//UserExtraInfo -> requires definition, currently not used //UserExtraInfo -> requires definition, currently not used
//UserPrivateInfo -> requires definition, currently not used //UserPrivateInfo -> requires definition, currently not used
"WatermarkText": assignStringTo(&cinfo.WatermarkText), case "WatermarkText":
cinfo.WatermarkText = value.(string)
"EnableShare": assignBoolTo(&cinfo.EnableShare), case "EnableShare":
"HideUserList": assignStringTo(&cinfo.HideUserList), cinfo.EnableShare = value.(bool)
"SupportsLocks": assignBoolTo(&cinfo.SupportsLocks), case "HideUserList":
"SupportsRename": assignBoolTo(&cinfo.SupportsRename), cinfo.HideUserList = value.(string)
"UserCanRename": assignBoolTo(&cinfo.UserCanRename), case "SupportsLocks":
"BreadcrumbDocName": assignStringTo(&cinfo.BreadcrumbDocName), cinfo.SupportsLocks = value.(bool)
} case "SupportsRename":
cinfo.SupportsRename = value.(bool)
for key, value := range props { case "UserCanRename":
setterFn := setters[key] cinfo.UserCanRename = value.(bool)
if setterFn != nil { case "BreadcrumbDocName":
setterFn(value) cinfo.BreadcrumbDocName = value.(string)
} }
} }
} }
@@ -24,80 +24,3 @@ type FileInfo interface {
// Note that the returned value must be unique among all the implementations // Note that the returned value must be unique among all the implementations
GetTarget() string GetTarget() string
} }
// assignStringTo will return a function whose parameter will be assigned
// to the provided key. The function will panic if the assignment isn't
// possible.
//
// fn := AssignStringTo(&target)
// fn(value)
//
// Is roughly equivalent to
//
// target = value
//
// The reason for this method is to help the `SetProperties` method in order
// to provide a setter function for each property.
// Expected code for the `SetProperties` should be similar to
//
// setters := map[string]func(value interface{}) {
// "Owner": AssignStringTo(&info.Owner),
// "DisplayName": AssignStringTo(&info.DisplayName),
// .....
// }
// for key, value := range props {
// fn := setters[key]
// fn(value)
// }
//
// Further `assign*To` functions will be provided to be able to assign
// different data types
func assignStringTo(targetKey *string) func(value interface{}) {
return func(value interface{}) {
*targetKey = value.(string)
}
}
// assignStringListTo will return a function whose parameter will be assigned
// to the provided key. The function will panic if the assignment isn't
// possible.
//
// See assignStringTo for more information
func assignStringListTo(targetKey *[]string) func(value interface{}) {
return func(value interface{}) {
*targetKey = value.([]string)
}
}
// assignInt64To will return a function whose parameter will be assigned
// to the provided key. The function will panic if the assignment isn't
// possible.
//
// See assignStringTo for more information
func assignInt64To(targetKey *int64) func(value interface{}) {
return func(value interface{}) {
*targetKey = value.(int64)
}
}
// assignIntTo will return a function whose parameter will be assigned
// to the provided key. The function will panic if the assignment isn't
// possible.
//
// See assignStringTo for more information
func assignIntTo(targetKey *int) func(value interface{}) {
return func(value interface{}) {
*targetKey = value.(int)
}
}
// assignBoolTo will return a function whose parameter will be assigned
// to the provided key. The function will panic if the assignment isn't
// possible.
//
// See assignStringTo for more information
func assignBoolTo(targetKey *bool) func(value interface{}) {
return func(value interface{}) {
*targetKey = value.(bool)
}
}
@@ -165,79 +165,133 @@ type Microsoft struct {
// SetProperties will set the file properties for the Microsoft implementation. // SetProperties will set the file properties for the Microsoft implementation.
func (minfo *Microsoft) SetProperties(props map[string]interface{}) { func (minfo *Microsoft) SetProperties(props map[string]interface{}) {
setters := map[string]func(value interface{}){
"BaseFileName": assignStringTo(&minfo.BaseFileName),
"OwnerID": assignStringTo(&minfo.OwnerID),
"Size": assignInt64To(&minfo.Size),
"UserID": assignStringTo(&minfo.UserID),
"Version": assignStringTo(&minfo.Version),
"SupportedShareURLTypes": assignStringListTo(&minfo.SupportedShareURLTypes),
"SupportsCobalt": assignBoolTo(&minfo.SupportsCobalt),
"SupportsContainers": assignBoolTo(&minfo.SupportsContainers),
"SupportsDeleteFile": assignBoolTo(&minfo.SupportsDeleteFile),
"SupportsEcosystem": assignBoolTo(&minfo.SupportsEcosystem),
"SupportsExtendedLockLength": assignBoolTo(&minfo.SupportsExtendedLockLength),
"SupportsFolders": assignBoolTo(&minfo.SupportsFolders),
//SupportsGetFileWopiSrc bool `json:"SupportsGetFileWopiSrc"` // wopivalidator is complaining and the property isn't used for now -> commented
"SupportsGetLock": assignBoolTo(&minfo.SupportsGetLock),
"SupportsLocks": assignBoolTo(&minfo.SupportsLocks),
"SupportsRename": assignBoolTo(&minfo.SupportsRename),
"SupportsUpdate": assignBoolTo(&minfo.SupportsUpdate),
"SupportsUserInfo": assignBoolTo(&minfo.SupportsUserInfo),
"IsAnonymousUser": assignBoolTo(&minfo.IsAnonymousUser),
"IsEduUser": assignBoolTo(&minfo.IsEduUser),
"LicenseCheckForEditIsEnabled": assignBoolTo(&minfo.LicenseCheckForEditIsEnabled),
"UserFriendlyName": assignStringTo(&minfo.UserFriendlyName),
"UserInfo": assignStringTo(&minfo.UserInfo),
"ReadOnly": assignBoolTo(&minfo.ReadOnly),
"RestrictedWebViewOnly": assignBoolTo(&minfo.RestrictedWebViewOnly),
"UserCanAttend": assignBoolTo(&minfo.UserCanAttend),
"UserCanNotWriteRelative": assignBoolTo(&minfo.UserCanNotWriteRelative),
"UserCanPresent": assignBoolTo(&minfo.UserCanPresent),
"UserCanRename": assignBoolTo(&minfo.UserCanRename),
"UserCanWrite": assignBoolTo(&minfo.UserCanWrite),
"CloseURL": assignStringTo(&minfo.CloseURL),
"DownloadURL": assignStringTo(&minfo.DownloadURL),
"FileEmbedCommandURL": assignStringTo(&minfo.FileEmbedCommandURL),
"FileSharingURL": assignStringTo(&minfo.FileSharingURL),
"FileURL": assignStringTo(&minfo.FileURL),
"FileVersionURL": assignStringTo(&minfo.FileVersionURL),
"HostEditURL": assignStringTo(&minfo.HostEditURL),
"HostEmbeddedViewURL": assignStringTo(&minfo.HostEmbeddedViewURL),
"HostViewURL": assignStringTo(&minfo.HostViewURL),
"SignoutURL": assignStringTo(&minfo.SignoutURL),
"AllowAdditionalMicrosoftServices": assignBoolTo(&minfo.AllowAdditionalMicrosoftServices),
"AllowErrorReportPrompt": assignBoolTo(&minfo.AllowErrorReportPrompt),
"AllowExternalMarketplace": assignBoolTo(&minfo.AllowExternalMarketplace),
"ClientThrottlingProtection": assignStringTo(&minfo.ClientThrottlingProtection),
"CloseButtonClosesWindow": assignBoolTo(&minfo.CloseButtonClosesWindow),
"CopyPasteRestrictions": assignStringTo(&minfo.CopyPasteRestrictions),
"DisablePrint": assignBoolTo(&minfo.DisablePrint),
"DisableTranslation": assignBoolTo(&minfo.DisableTranslation),
"FileExtension": assignStringTo(&minfo.FileExtension),
"FileNameMaxLength": assignIntTo(&minfo.FileNameMaxLength),
"LastModifiedTime": assignStringTo(&minfo.LastModifiedTime),
"RequestedCallThrottling": assignStringTo(&minfo.RequestedCallThrottling),
"SHA256": assignStringTo(&minfo.SHA256),
"SharingStatus": assignStringTo(&minfo.SharingStatus),
"TemporarilyNotWritable": assignBoolTo(&minfo.TemporarilyNotWritable),
"BreadcrumbBrandName": assignStringTo(&minfo.BreadcrumbBrandName),
"BreadcrumbBrandURL": assignStringTo(&minfo.BreadcrumbBrandURL),
"BreadcrumbDocName": assignStringTo(&minfo.BreadcrumbDocName),
"BreadcrumbFolderName": assignStringTo(&minfo.BreadcrumbFolderName),
"BreadcrumbFolderURL": assignStringTo(&minfo.BreadcrumbFolderURL),
}
for key, value := range props { for key, value := range props {
setterFn := setters[key] switch key {
if setterFn != nil { case "BaseFileName":
setterFn(value) minfo.BaseFileName = value.(string)
case "OwnerID":
minfo.OwnerID = value.(string)
case "Size":
minfo.Size = value.(int64)
case "UserID":
minfo.UserID = value.(string)
case "Version":
minfo.Version = value.(string)
case "SupportedShareURLTypes":
minfo.SupportedShareURLTypes = value.([]string)
case "SupportsCobalt":
minfo.SupportsCobalt = value.(bool)
case "SupportsContainers":
minfo.SupportsContainers = value.(bool)
case "SupportsDeleteFile":
minfo.SupportsDeleteFile = value.(bool)
case "SupportsEcosystem":
minfo.SupportsEcosystem = value.(bool)
case "SupportsExtendedLockLength":
minfo.SupportsExtendedLockLength = value.(bool)
case "SupportsFolders":
minfo.SupportsFolders = value.(bool)
//SupportsGetFileWopiSrc bool `json:"SupportsGetFileWopiSrc"` // wopivalidator is complaining and the property isn't used for now -> commented
case "SupportsGetLock":
minfo.SupportsGetLock = value.(bool)
case "SupportsLocks":
minfo.SupportsLocks = value.(bool)
case "SupportsRename":
minfo.SupportsRename = value.(bool)
case "SupportsUpdate":
minfo.SupportsUpdate = value.(bool)
case "SupportsUserInfo":
minfo.SupportsUserInfo = value.(bool)
case "IsAnonymousUser":
minfo.IsAnonymousUser = value.(bool)
case "IsEduUser":
minfo.IsEduUser = value.(bool)
case "LicenseCheckForEditIsEnabled":
minfo.LicenseCheckForEditIsEnabled = value.(bool)
case "UserFriendlyName":
minfo.UserFriendlyName = value.(string)
case "UserInfo":
minfo.UserInfo = value.(string)
case "ReadOnly":
minfo.ReadOnly = value.(bool)
case "RestrictedWebViewOnly":
minfo.RestrictedWebViewOnly = value.(bool)
case "UserCanAttend":
minfo.UserCanAttend = value.(bool)
case "UserCanNotWriteRelative":
minfo.UserCanNotWriteRelative = value.(bool)
case "UserCanPresent":
minfo.UserCanPresent = value.(bool)
case "UserCanRename":
minfo.UserCanRename = value.(bool)
case "UserCanWrite":
minfo.UserCanWrite = value.(bool)
case "CloseURL":
minfo.CloseURL = value.(string)
case "DownloadURL":
minfo.DownloadURL = value.(string)
case "FileEmbedCommandURL":
minfo.FileEmbedCommandURL = value.(string)
case "FileSharingURL":
minfo.FileSharingURL = value.(string)
case "FileURL":
minfo.FileURL = value.(string)
case "FileVersionURL":
minfo.FileVersionURL = value.(string)
case "HostEditURL":
minfo.HostEditURL = value.(string)
case "HostEmbeddedViewURL":
minfo.HostEmbeddedViewURL = value.(string)
case "HostViewURL":
minfo.HostViewURL = value.(string)
case "SignoutURL":
minfo.SignoutURL = value.(string)
case "AllowAdditionalMicrosoftServices":
minfo.AllowAdditionalMicrosoftServices = value.(bool)
case "AllowErrorReportPrompt":
minfo.AllowErrorReportPrompt = value.(bool)
case "AllowExternalMarketplace":
minfo.AllowExternalMarketplace = value.(bool)
case "ClientThrottlingProtection":
minfo.ClientThrottlingProtection = value.(string)
case "CloseButtonClosesWindow":
minfo.CloseButtonClosesWindow = value.(bool)
case "CopyPasteRestrictions":
minfo.CopyPasteRestrictions = value.(string)
case "DisablePrint":
minfo.DisablePrint = value.(bool)
case "DisableTranslation":
minfo.DisableTranslation = value.(bool)
case "FileExtension":
minfo.FileExtension = value.(string)
case "FileNameMaxLength":
minfo.FileNameMaxLength = value.(int)
case "LastModifiedTime":
minfo.LastModifiedTime = value.(string)
case "RequestedCallThrottling":
minfo.RequestedCallThrottling = value.(string)
case "SHA256":
minfo.SHA256 = value.(string)
case "SharingStatus":
minfo.SharingStatus = value.(string)
case "TemporarilyNotWritable":
minfo.TemporarilyNotWritable = value.(bool)
case "BreadcrumbBrandName":
minfo.BreadcrumbBrandName = value.(string)
case "BreadcrumbBrandURL":
minfo.BreadcrumbBrandURL = value.(string)
case "BreadcrumbDocName":
minfo.BreadcrumbDocName = value.(string)
case "BreadcrumbFolderName":
minfo.BreadcrumbFolderName = value.(string)
case "BreadcrumbFolderURL":
minfo.BreadcrumbFolderURL = value.(string)
} }
} }
} }
@@ -129,57 +129,88 @@ type OnlyOffice struct {
// SetProperties will set the file properties for the OnlyOffice implementation. // SetProperties will set the file properties for the OnlyOffice implementation.
func (oinfo *OnlyOffice) SetProperties(props map[string]interface{}) { func (oinfo *OnlyOffice) SetProperties(props map[string]interface{}) {
setters := map[string]func(value interface{}){
"BaseFileName": assignStringTo(&oinfo.BaseFileName),
"Version": assignStringTo(&oinfo.Version),
"BreadcrumbBrandName": assignStringTo(&oinfo.BreadcrumbBrandName),
"BreadcrumbBrandURL": assignStringTo(&oinfo.BreadcrumbBrandURL),
"BreadcrumbDocName": assignStringTo(&oinfo.BreadcrumbDocName),
"BreadcrumbFolderName": assignStringTo(&oinfo.BreadcrumbFolderName),
"BreadcrumbFolderURL": assignStringTo(&oinfo.BreadcrumbFolderURL),
"ClosePostMessage": assignBoolTo(&oinfo.ClosePostMessage),
"EditModePostMessage": assignBoolTo(&oinfo.EditModePostMessage),
"EditNotificationPostMessage": assignBoolTo(&oinfo.EditNotificationPostMessage),
"FileSharingPostMessage": assignBoolTo(&oinfo.FileSharingPostMessage),
"FileVersionPostMessage": assignBoolTo(&oinfo.FileVersionPostMessage),
"PostMessageOrigin": assignStringTo(&oinfo.PostMessageOrigin),
"CloseURL": assignStringTo(&oinfo.CloseURL),
"FileSharingURL": assignStringTo(&oinfo.FileSharingURL),
"FileVersionURL": assignStringTo(&oinfo.FileVersionURL),
"HostEditURL": assignStringTo(&oinfo.HostEditURL),
"CopyPasteRestrictions": assignStringTo(&oinfo.CopyPasteRestrictions),
"DisablePrint": assignBoolTo(&oinfo.DisablePrint),
"FileExtension": assignStringTo(&oinfo.FileExtension),
"FileNameMaxLength": assignIntTo(&oinfo.FileNameMaxLength),
"LastModifiedTime": assignStringTo(&oinfo.LastModifiedTime),
"IsAnonymousUser": assignBoolTo(&oinfo.IsAnonymousUser),
"UserFriendlyName": assignStringTo(&oinfo.UserFriendlyName),
"UserID": assignStringTo(&oinfo.UserID),
"ReadOnly": assignBoolTo(&oinfo.ReadOnly),
"UserCanNotWriteRelative": assignBoolTo(&oinfo.UserCanNotWriteRelative),
"UserCanRename": assignBoolTo(&oinfo.UserCanRename),
"UserCanReview": assignBoolTo(&oinfo.UserCanReview),
"UserCanWrite": assignBoolTo(&oinfo.UserCanWrite),
"SupportsLocks": assignBoolTo(&oinfo.SupportsLocks),
"SupportsRename": assignBoolTo(&oinfo.SupportsRename),
"SupportsReviewing": assignBoolTo(&oinfo.SupportsReviewing),
"SupportsUpdate": assignBoolTo(&oinfo.SupportsUpdate),
"EnableInsertRemoteImage": assignBoolTo(&oinfo.EnableInsertRemoteImage),
"HidePrintOption": assignBoolTo(&oinfo.HidePrintOption),
}
for key, value := range props { for key, value := range props {
setterFn := setters[key] switch key {
if setterFn != nil { case "BaseFileName":
setterFn(value) oinfo.BaseFileName = value.(string)
case "Version":
oinfo.Version = value.(string)
case "BreadcrumbBrandName":
oinfo.BreadcrumbBrandName = value.(string)
case "BreadcrumbBrandURL":
oinfo.BreadcrumbBrandURL = value.(string)
case "BreadcrumbDocName":
oinfo.BreadcrumbDocName = value.(string)
case "BreadcrumbFolderName":
oinfo.BreadcrumbFolderName = value.(string)
case "BreadcrumbFolderURL":
oinfo.BreadcrumbFolderURL = value.(string)
case "ClosePostMessage":
oinfo.ClosePostMessage = value.(bool)
case "EditModePostMessage":
oinfo.EditModePostMessage = value.(bool)
case "EditNotificationPostMessage":
oinfo.EditNotificationPostMessage = value.(bool)
case "FileSharingPostMessage":
oinfo.FileSharingPostMessage = value.(bool)
case "FileVersionPostMessage":
oinfo.FileVersionPostMessage = value.(bool)
case "PostMessageOrigin":
oinfo.PostMessageOrigin = value.(string)
case "CloseURL":
oinfo.CloseURL = value.(string)
case "FileSharingURL":
oinfo.FileSharingURL = value.(string)
case "FileVersionURL":
oinfo.FileVersionURL = value.(string)
case "HostEditURL":
oinfo.HostEditURL = value.(string)
case "CopyPasteRestrictions":
oinfo.CopyPasteRestrictions = value.(string)
case "DisablePrint":
oinfo.DisablePrint = value.(bool)
case "FileExtension":
oinfo.FileExtension = value.(string)
case "FileNameMaxLength":
oinfo.FileNameMaxLength = value.(int)
case "LastModifiedTime":
oinfo.LastModifiedTime = value.(string)
case "IsAnonymousUser":
oinfo.IsAnonymousUser = value.(bool)
case "UserFriendlyName":
oinfo.UserFriendlyName = value.(string)
case "UserID":
oinfo.UserID = value.(string)
case "ReadOnly":
oinfo.ReadOnly = value.(bool)
case "UserCanNotWriteRelative":
oinfo.UserCanNotWriteRelative = value.(bool)
case "UserCanRename":
oinfo.UserCanRename = value.(bool)
case "UserCanReview":
oinfo.UserCanReview = value.(bool)
case "UserCanWrite":
oinfo.UserCanWrite = value.(bool)
case "SupportsLocks":
oinfo.SupportsLocks = value.(bool)
case "SupportsRename":
oinfo.SupportsRename = value.(bool)
case "SupportsReviewing":
oinfo.SupportsReviewing = value.(bool)
case "SupportsUpdate":
oinfo.SupportsUpdate = value.(bool)
case "EnableInsertRemoteImage":
oinfo.EnableInsertRemoteImage = value.(bool)
case "HidePrintOption":
oinfo.HidePrintOption = value.(bool)
} }
} }
} }