|
| 1 | +package dosingdecision |
| 2 | + |
| 3 | +import ( |
| 4 | + dataTypesBolusCombination "github.com/tidepool-org/platform/data/types/bolus/combination" |
| 5 | + "github.com/tidepool-org/platform/structure" |
| 6 | + structureValidator "github.com/tidepool-org/platform/structure/validator" |
| 7 | +) |
| 8 | + |
| 9 | +const ( |
| 10 | + BolusAmountMaximum = dataTypesBolusCombination.NormalMaximum |
| 11 | + BolusAmountMinimum = dataTypesBolusCombination.NormalMinimum |
| 12 | + BolusDurationMaximum = dataTypesBolusCombination.DurationMaximum |
| 13 | + BolusDurationMinimum = dataTypesBolusCombination.DurationMinimum |
| 14 | + BolusExtendedMaximum = dataTypesBolusCombination.ExtendedMaximum |
| 15 | + BolusExtendedMinimum = dataTypesBolusCombination.ExtendedMinimum |
| 16 | + BolusNormalMaximum = dataTypesBolusCombination.NormalMaximum |
| 17 | + BolusNormalMinimum = dataTypesBolusCombination.NormalMinimum |
| 18 | +) |
| 19 | + |
| 20 | +type Bolus struct { |
| 21 | + Amount *float64 `json:"amount,omitempty" bson:"amount,omitempty"` |
| 22 | + Duration *int `json:"duration,omitempty" bson:"duration,omitempty"` |
| 23 | + Extended *float64 `json:"extended,omitempty" bson:"extended,omitempty"` |
| 24 | + Normal *float64 `json:"normal,omitempty" bson:"normal,omitempty"` |
| 25 | +} |
| 26 | + |
| 27 | +func ParseBolus(parser structure.ObjectParser) *Bolus { |
| 28 | + if !parser.Exists() { |
| 29 | + return nil |
| 30 | + } |
| 31 | + datum := NewBolus() |
| 32 | + parser.Parse(datum) |
| 33 | + return datum |
| 34 | +} |
| 35 | + |
| 36 | +func NewBolus() *Bolus { |
| 37 | + return &Bolus{} |
| 38 | +} |
| 39 | + |
| 40 | +func (b *Bolus) Parse(parser structure.ObjectParser) { |
| 41 | + b.Amount = parser.Float64("amount") |
| 42 | + b.Duration = parser.Int("duration") |
| 43 | + b.Extended = parser.Float64("extended") |
| 44 | + b.Normal = parser.Float64("normal") |
| 45 | +} |
| 46 | + |
| 47 | +func (b *Bolus) Validate(validator structure.Validator) { |
| 48 | + if b.Amount != nil { |
| 49 | + validator.Float64("amount", b.Amount).InRange(BolusAmountMinimum, BolusAmountMaximum) |
| 50 | + validator.Int("duration", b.Duration).NotExists() |
| 51 | + validator.Float64("extended", b.Extended).NotExists() |
| 52 | + validator.Float64("normal", b.Normal).NotExists() |
| 53 | + } else { |
| 54 | + if durationValidator := validator.Int("duration", b.Duration); b.Extended != nil { |
| 55 | + durationValidator.Exists().InRange(BolusDurationMinimum, BolusDurationMaximum) |
| 56 | + } else { |
| 57 | + durationValidator.NotExists() |
| 58 | + } |
| 59 | + validator.Float64("extended", b.Extended).InRange(BolusExtendedMinimum, BolusExtendedMaximum) |
| 60 | + validator.Float64("normal", b.Normal).InRange(BolusNormalMinimum, BolusNormalMaximum) |
| 61 | + } |
| 62 | + |
| 63 | + if b.Amount == nil && b.Extended == nil && b.Normal == nil { |
| 64 | + validator.ReportError(structureValidator.ErrorValuesNotExistForAny("amount", "extended", "normal")) |
| 65 | + } |
| 66 | +} |
0 commit comments