fix(17-02): handle float64 numeric values from Wails JSON deserialization
Wails deserializes JSON numbers from JavaScript as float64, not int. All .(int) type assertions on year, track_number, and disc_number silently failed. Add asInt() helper, replace all assertions.
This commit is contained in:
@@ -117,7 +117,7 @@ func syncDatabase(
|
||||
|
||||
// Determine year value.
|
||||
yearVal := params.oldRecording.Year
|
||||
if yv, yOK := params.changes[FieldYear].(int); yOK {
|
||||
if yv, yOK := asInt(params.changes[FieldYear]); yOK {
|
||||
yearVal = toNullInt64(yv)
|
||||
}
|
||||
|
||||
@@ -147,12 +147,12 @@ func syncDatabase(
|
||||
|
||||
// Determine track/disc numbers.
|
||||
trackNum := params.oldRecording.TrackNumber
|
||||
if tn, tnOK := params.changes[FieldTrackNumber].(int); tnOK {
|
||||
if tn, tnOK := asInt(params.changes[FieldTrackNumber]); tnOK {
|
||||
trackNum = toNullInt64(tn)
|
||||
}
|
||||
|
||||
discNum := params.oldRecording.DiscNumber
|
||||
if dn, dnOK := params.changes[FieldDiscNumber].(int); dnOK {
|
||||
if dn, dnOK := asInt(params.changes[FieldDiscNumber]); dnOK {
|
||||
discNum = toNullInt64(dn)
|
||||
}
|
||||
|
||||
@@ -216,17 +216,17 @@ func syncDatabase(
|
||||
}
|
||||
|
||||
newYear := rec.Year
|
||||
if v, ok := params.changes[FieldYear].(int); ok {
|
||||
if v, ok := asInt(params.changes[FieldYear]); ok {
|
||||
newYear = toNullInt64(v)
|
||||
}
|
||||
|
||||
newTrackNum := rec.TrackNumber
|
||||
if v, ok := params.changes[FieldTrackNumber].(int); ok {
|
||||
if v, ok := asInt(params.changes[FieldTrackNumber]); ok {
|
||||
newTrackNum = toNullInt64(v)
|
||||
}
|
||||
|
||||
newDiscNum := rec.DiscNumber
|
||||
if v, ok := params.changes[FieldDiscNumber].(int); ok {
|
||||
if v, ok := asInt(params.changes[FieldDiscNumber]); ok {
|
||||
newDiscNum = toNullInt64(v)
|
||||
}
|
||||
|
||||
|
||||
@@ -111,10 +111,21 @@ func applyFlacTextChanges(cmt *flacvorbis.MetaDataBlockVorbisComment, changes Ta
|
||||
}
|
||||
|
||||
var val string
|
||||
|
||||
if m.isInt {
|
||||
val = strconv.Itoa(v.(int))
|
||||
n, ok := asInt(v)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
val = strconv.Itoa(n)
|
||||
} else {
|
||||
val = v.(string)
|
||||
s, ok := v.(string)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
val = s
|
||||
}
|
||||
|
||||
replaceVorbisComment(cmt, m.vorbisID, val)
|
||||
|
||||
@@ -62,17 +62,17 @@ func applyTextChanges(tag *id3v2.Tag, changes TagChanges) {
|
||||
tag.SetGenre(v)
|
||||
}
|
||||
|
||||
if v, ok := changes[FieldYear].(int); ok {
|
||||
if v, ok := asInt(changes[FieldYear]); ok {
|
||||
tag.SetYear(strconv.Itoa(v))
|
||||
}
|
||||
|
||||
if v, ok := changes[FieldTrackNumber].(int); ok {
|
||||
if v, ok := asInt(changes[FieldTrackNumber]); ok {
|
||||
trckID := tag.CommonID("Track number/Position in set")
|
||||
tag.DeleteFrames(trckID)
|
||||
tag.AddTextFrame(trckID, id3v2.EncodingUTF8, strconv.Itoa(v))
|
||||
}
|
||||
|
||||
if v, ok := changes[FieldDiscNumber].(int); ok {
|
||||
if v, ok := asInt(changes[FieldDiscNumber]); ok {
|
||||
tposID := tag.CommonID("Part of a set")
|
||||
tag.DeleteFrames(tposID)
|
||||
tag.AddTextFrame(tposID, id3v2.EncodingUTF8, strconv.Itoa(v))
|
||||
|
||||
@@ -55,6 +55,24 @@ func DetectFormat(filePath string) (AudioFormat, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// asInt extracts an integer from a TagChanges value. JSON numbers from
|
||||
// Wails arrive as float64; Go callers may pass int. Returns (value, true)
|
||||
// on success or (0, false) if the value is not a recognised numeric type.
|
||||
func asInt(v any) (int, bool) {
|
||||
switch n := v.(type) {
|
||||
case int:
|
||||
return n, true
|
||||
case float64:
|
||||
return int(n), true
|
||||
case int64:
|
||||
return int(n), true
|
||||
case float32:
|
||||
return int(n), true
|
||||
default:
|
||||
return 0, false
|
||||
}
|
||||
}
|
||||
|
||||
// detectMIME returns the MIME type of image data by checking magic bytes.
|
||||
func detectMIME(data []byte) string {
|
||||
if len(data) >= 2 && data[0] == 0xFF && data[1] == 0xD8 {
|
||||
|
||||
Reference in New Issue
Block a user