feat(API): media field type
This commit is contained in:
1 parent
ad5faf3d19
commit
a2d596f4ca
63 files changed
+2567
-259
No files matched your search
@@ -25,6 +25,7 @@ const (
|
||||
TypeBoolean FieldType = "boolean"
|
||||
TypeDate FieldType = "date"
|
||||
TypeSelect FieldType = "select"
|
||||
TypeMedia FieldType = "media"
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
@@ -20,6 +20,36 @@ var kinds = map[FieldType]kind{
|
||||
TypeBoolean: {accepts: isBoolean},
|
||||
TypeDate: {accepts: isDate, measure: dateDays, bound: dateDays, unique: true},
|
||||
TypeSelect: {accepts: isString, unique: true},
|
||||
TypeMedia: {accepts: isMediaID},
|
||||
}
|
||||
|
||||
func MediaID(value any) (uint32, bool) {
|
||||
switch typed := value.(type) {
|
||||
case uint32:
|
||||
return typed, typed >= 1
|
||||
case int:
|
||||
return mediaIDFromInt(int64(typed))
|
||||
case int64:
|
||||
return mediaIDFromInt(typed)
|
||||
case float64:
|
||||
if typed != math.Trunc(typed) || typed < 1 || typed > math.MaxUint32 {
|
||||
return 0, false
|
||||
}
|
||||
return uint32(typed), true
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
func mediaIDFromInt(number int64) (uint32, bool) {
|
||||
if number < 1 || number > math.MaxUint32 {
|
||||
return 0, false
|
||||
}
|
||||
return uint32(number), true
|
||||
}
|
||||
|
||||
func isMediaID(value any) bool {
|
||||
_, ok := MediaID(value)
|
||||
return ok
|
||||
}
|
||||
|
||||
func isString(value any) bool {
|
||||
|
||||
@@ -109,5 +109,18 @@ func (f Field) validDefault() bool {
|
||||
if f.Default == nil {
|
||||
return true
|
||||
}
|
||||
if f.Type == TypeMedia {
|
||||
return false
|
||||
}
|
||||
return !f.Unique && f.Violation(f.Default) == ""
|
||||
}
|
||||
|
||||
func MediaFields(fields []Field) []Field {
|
||||
var media []Field
|
||||
for _, field := range fields {
|
||||
if field.Type == TypeMedia {
|
||||
media = append(media, field)
|
||||
}
|
||||
}
|
||||
return media
|
||||
}
|
||||
Reference in new issue
Block a user