github.com/juju/juju@v0.0.0-20240327075706-a90865de2538/core/life/life.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package life 5 6 import ( 7 "github.com/juju/errors" 8 ) 9 10 // Value indicates the state of some entity. 11 type Value string 12 13 const ( 14 // Alive indicates that some entity is meant to exist. 15 Alive Value = "alive" 16 17 // Dying indicates that some entity should be removed. 18 Dying Value = "dying" 19 20 // Dead indicates that some entity is no longer useful, 21 // and can be destroyed unconditionally. 22 Dead Value = "dead" 23 ) 24 25 // Validate returns an error if the value is not known. 26 func (v Value) Validate() error { 27 switch v { 28 case Alive, Dying, Dead: 29 return nil 30 } 31 return errors.NotValidf("life value %q", v) 32 } 33 34 // Predicate is a predicate. 35 type Predicate func(Value) bool 36 37 // IsDead is a Predicate that returns true if the supplied value is Dead. 38 // 39 // This indicates that the entity in question is dead. 40 func IsDead(v Value) bool { 41 return v == Dead 42 } 43 44 // IsAlive is a Predicate that returns true if the supplied value 45 // is Alive. 46 // 47 // This generally indicates that the entity in question is expected 48 // to be existing for now and not going away or gone completely. 49 func IsAlive(v Value) bool { 50 return v == Alive 51 } 52 53 // IsNotAlive is a Predicate that returns true if the supplied value 54 // is not Alive. 55 // 56 // This generally indicates that the entity in question is at some 57 // stage of destruction/cleanup. 58 func IsNotAlive(v Value) bool { 59 return v != Alive 60 } 61 62 // IsNotDead is a Predicate that returns true if the supplied value 63 // is not Dead. 64 // 65 // This generally indicates that the entity in question is active in 66 // some way, and can probably not be completely destroyed without 67 // consequences. 68 func IsNotDead(v Value) bool { 69 return v != Dead 70 }