commit 434af35e4bcf926824b333e71c4abd59cfef701c
parent 168d2dc0ac539fb5f8f014a16a56fc6e9e94f5d3
Author: Katja Ramona Sophie Kwast (zaphyra) <git@zaphyra.eu>
Date: Wed, 13 Aug 2025 19:48:34 +0200
parent 168d2dc0ac539fb5f8f014a16a56fc6e9e94f5d3
Author: Katja Ramona Sophie Kwast (zaphyra) <git@zaphyra.eu>
Date: Wed, 13 Aug 2025 19:48:34 +0200
src/types: rename `Any` to `Option`
4 files changed, 62 insertions(+), 62 deletions(-)
diff --git a/src/types/Any.go b/src/types/Any.go @@ -1,40 +0,0 @@ -package types - -import ( - "bytes" - "encoding/json" -) - -type Any[T any] struct { - Set bool - Valid bool - Value T -} - -func (s Any[T]) MarshalJSON() ([]byte, error) { - if !s.Valid { - return []byte("null"), nil - } - return json.Marshal(s.Value) -} - -func (s *Any[T]) UnmarshalJSON(data []byte) error { - s.Set = true - s.Valid = false - - if bytes.Equal(data, []byte("null")) { - // The key was set to null, set value to zero/default value - var zero T - s.Value = zero - return nil - } - - // The key isn't set to null - var v T - if err := json.Unmarshal(data, &v); err != nil { - return err - } - s.Value = v - s.Valid = true - return nil -}
diff --git a/src/types/BatteryTTLEstimate.go b/src/types/BatteryTTLEstimate.go @@ -7,16 +7,16 @@ import ( "git.zaphyra.eu/airpodsctl/helpers" ) -type BatteryTTLEstimate Any[int] +type BatteryTTLEstimate Option[int] func (s *BatteryTTLEstimate) UnmarshalJSON(data []byte) error { - var v Any[int] + var v Option[int] if err := json.Unmarshal(data, &v); err != nil { return err } *s = BatteryTTLEstimate{ - Set: v.Set, + Set: v.Set, Valid: v.Valid, Value: v.Value, }
diff --git a/src/types/Device.go b/src/types/Device.go @@ -5,7 +5,7 @@ type BatteryState struct { Level int `json:"level"` } -type EarDetectionState struct { +type EarDetectionState struct { LeftInEar bool `json:"left_in_ear"` RightInEar bool `json:"right_in_ear"` } @@ -13,24 +13,24 @@ type EarDetectionState struct { type Device struct { Address string `json:"address"` Battery struct { - Case Any[BatteryState] `json:"case"` - Headphone Any[BatteryState] `json:"headphone"` - LeftBud Any[BatteryState] `json:"left"` - RightBud Any[BatteryState] `json:"right"` + Case Option[BatteryState] `json:"case"` + Headphone Option[BatteryState] `json:"headphone"` + LeftBud Option[BatteryState] `json:"left"` + RightBud Option[BatteryState] `json:"right"` } `json:"battery"` - BatteryTTLEstimate BatteryTTLEstimate `json:"battery_ttl_estimate"` - Connected Bool `json:"connected"` - EarDetection Any[EarDetectionState] `json:"ear_detection"` - Features struct { - Num35 Any[Bool] `json:"35"` - ThreeE Any[Bool] `json:"3e"` - AdaptiveVolume Any[Bool] `json:"adaptive_volume"` - Conversational Any[Bool] `json:"conversational"` - HearingAidSettings Any[Bool] `json:"hearing_aid_settings"` - OneBudAnc Any[Bool] `json:"one_bud_anc"` - Ssl Any[Bool] `json:"ssl"` - VolumeSwipe Any[Bool] `json:"volume_swipe"` + BatteryTTLEstimate BatteryTTLEstimate `json:"battery_ttl_estimate"` + Connected Bool `json:"connected"` + EarDetection Option[EarDetectionState] `json:"ear_detection"` + Features struct { + Num35 Option[Bool] `json:"35"` + ThreeE Option[Bool] `json:"3e"` + AdaptiveVolume Option[Bool] `json:"adaptive_volume"` + Conversational Option[Bool] `json:"conversational"` + HearingAidSettings Option[Bool] `json:"hearing_aid_settings"` + OneBudAnc Option[Bool] `json:"one_bud_anc"` + Ssl Option[Bool] `json:"ssl"` + VolumeSwipe Option[Bool] `json:"volume_swipe"` } `json:"features"` - Name string `json:"name"` - ANCMode Any[ANCMode] `json:"noise_mode"` + Name string `json:"name"` + ANCMode Option[ANCMode] `json:"noise_mode"` }
diff --git a/src/types/Option.go b/src/types/Option.go @@ -0,0 +1,40 @@ +package types + +import ( + "bytes" + "encoding/json" +) + +type Option[T any] struct { + Set bool + Valid bool + Value T +} + +func (s Option[T]) MarshalJSON() ([]byte, error) { + if !s.Valid { + return []byte("null"), nil + } + return json.Marshal(s.Value) +} + +func (s *Option[T]) UnmarshalJSON(data []byte) error { + s.Set = true + s.Valid = false + + if bytes.Equal(data, []byte("null")) { + // The key was set to null, set value to zero/default value + var zero T + s.Value = zero + return nil + } + + // The key isn't set to null + var v T + if err := json.Unmarshal(data, &v); err != nil { + return err + } + s.Value = v + s.Valid = true + return nil +}