soft-deprecated Record.GetUploadedFiles in favour of Record.GetUnsavedFiles

This commit is contained in:
Gani Georgiev 2025-01-10 11:45:45 +02:00
parent 18d0b47aeb
commit e103d987ce
5 changed files with 27 additions and 10 deletions

View File

@ -14,6 +14,9 @@
- Added JSVM `new Timezone(name)` binding for constructing `time.Location` value ([#6219](https://github.com/pocketbase/pocketbase/discussions/6219)). - Added JSVM `new Timezone(name)` binding for constructing `time.Location` value ([#6219](https://github.com/pocketbase/pocketbase/discussions/6219)).
- Soft-deprecated `Record.GetUploadedFiles` in favour of `Record.GetUnsavedFiles` to minimize the ambiguities what the method do ([#6269](https://github.com/pocketbase/pocketbase/discussions/6269)).
(@todo update docs to reflect the `:unsaved` getter change)
## v0.24.2 ## v0.24.2

View File

@ -5,6 +5,7 @@ import (
"database/sql/driver" "database/sql/driver"
"errors" "errors"
"fmt" "fmt"
"log"
"regexp" "regexp"
"strings" "strings"
@ -637,7 +638,13 @@ func (f *FileField) FindGetter(key string) GetterFunc {
return func(record *Record) any { return func(record *Record) any {
return record.GetRaw(f.Name) return record.GetRaw(f.Name)
} }
case f.Name + ":unsaved":
return func(record *Record) any {
return f.extractUploadableFiles(f.toSliceValue(record.GetRaw(f.Name)))
}
case f.Name + ":uploaded": case f.Name + ":uploaded":
// deprecated
log.Println("[file field getter] please replace :uploaded with :unsaved")
return func(record *Record) any { return func(record *Record) any {
return f.extractUploadableFiles(f.toSliceValue(record.GetRaw(f.Name))) return f.extractUploadableFiles(f.toSliceValue(record.GetRaw(f.Name)))
} }

View File

@ -672,8 +672,8 @@ func TestFileFieldFindGetter(t *testing.T) {
`["300_UhLKX91HVb.png",{"name":"f1","originalName":"f1","size":4},{"name":"f2","originalName":"f2","size":4}]`, `["300_UhLKX91HVb.png",{"name":"f1","originalName":"f1","size":4},{"name":"f2","originalName":"f2","size":4}]`,
}, },
{ {
"uploaded", "unsaved",
field.GetName() + ":uploaded", field.GetName() + ":unsaved",
true, true,
`[{"name":"f1","originalName":"f1","size":4},{"name":"f2","originalName":"f2","size":4}]`, `[{"name":"f1","originalName":"f1","size":4},{"name":"f2","originalName":"f2","size":4}]`,
}, },

View File

@ -6,6 +6,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"log"
"maps" "maps"
"slices" "slices"
"sort" "sort"
@ -973,20 +974,20 @@ func (m *Record) GetStringSlice(key string) []string {
return list.ToUniqueStringSlice(m.Get(key)) return list.ToUniqueStringSlice(m.Get(key))
} }
// GetUploadedFiles returns the uploaded files for the provided "file" field key, // GetUnsavedFiles returns the uploaded files for the provided "file" field key,
// (aka. the current [*filesytem.File] values) so that you can apply further // (aka. the current [*filesytem.File] values) so that you can apply further
// validations or modifications (including changing the file name or content before persisting). // validations or modifications (including changing the file name or content before persisting).
// //
// Example: // Example:
// //
// files := record.GetUploadedFiles("documents") // files := record.GetUnsavedFiles("documents")
// for _, f := range files { // for _, f := range files {
// f.Name = "doc_" + f.Name // add a prefix to each file name // f.Name = "doc_" + f.Name // add a prefix to each file name
// } // }
// app.Save(record) // the files are pointers so the applied changes will transparently reflect on the record value // app.Save(record) // the files are pointers so the applied changes will transparently reflect on the record value
func (m *Record) GetUploadedFiles(key string) []*filesystem.File { func (m *Record) GetUnsavedFiles(key string) []*filesystem.File {
if !strings.HasSuffix(key, ":uploaded") { if !strings.HasSuffix(key, ":unsaved") {
key += ":uploaded" key += ":unsaved"
} }
values, _ := m.Get(key).([]*filesystem.File) values, _ := m.Get(key).([]*filesystem.File)
@ -994,6 +995,12 @@ func (m *Record) GetUploadedFiles(key string) []*filesystem.File {
return values return values
} }
// Deprecated: replaced with GetUnsavedFiles.
func (m *Record) GetUploadedFiles(key string) []*filesystem.File {
log.Println("Please replace GetUploadedFiles with GetUnsavedFiles")
return m.GetUnsavedFiles(key)
}
// Retrieves the "key" json field value and unmarshals it into "result". // Retrieves the "key" json field value and unmarshals it into "result".
// //
// Example // Example

View File

@ -1013,7 +1013,7 @@ func TestRecordGetStringSlice(t *testing.T) {
} }
} }
func TestRecordGetUploadedFiles(t *testing.T) { func TestRecordGetUnsavedFiles(t *testing.T) {
t.Parallel() t.Parallel()
app, _ := tests.NewTestApp() app, _ := tests.NewTestApp()
@ -1054,14 +1054,14 @@ func TestRecordGetUploadedFiles(t *testing.T) {
`[{"name":"f1","originalName":"f1","size":4},{"name":"f2","originalName":"f2","size":4}]`, `[{"name":"f1","originalName":"f1","size":4},{"name":"f2","originalName":"f2","size":4}]`,
}, },
{ {
"files:uploaded", "files:unsaved",
`[{"name":"f1","originalName":"f1","size":4},{"name":"f2","originalName":"f2","size":4}]`, `[{"name":"f1","originalName":"f1","size":4},{"name":"f2","originalName":"f2","size":4}]`,
}, },
} }
for i, s := range scenarios { for i, s := range scenarios {
t.Run(fmt.Sprintf("%d_%#v", i, s.key), func(t *testing.T) { t.Run(fmt.Sprintf("%d_%#v", i, s.key), func(t *testing.T) {
v := record.GetUploadedFiles(s.key) v := record.GetUnsavedFiles(s.key)
raw, err := json.Marshal(v) raw, err := json.Marshal(v)
if err != nil { if err != nil {