github.com/binkynet/BinkyNet@v1.12.1-0.20240421190447-da4e34c20be0/apis/v1/objectconfigkey.go (about) 1 // Copyright 2023 Ewout Prangsma 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // Author Ewout Prangsma 16 // 17 18 package v1 19 20 import ( 21 "fmt" 22 "strconv" 23 ) 24 25 // ObjectConfigKey is a strongly typed well known key of a configuration entry of an object type. 26 type ObjectConfigKey string 27 28 const ( 29 // ObjectConfigKeyDebug turns on/off debugging information for a specific object. 30 // Values are true|false|0|1. 31 ObjectConfigKeyDebug ObjectConfigKey = "debug" 32 // ObjectConfigKeyMagneticAlwaysEnabled configures the always-enabled setting for 33 // a magnetic switch. If set to true, the magnets are always powered, even when 34 // the network controller does not indicate the switch as being enabled. 35 // Values are true|false|0|1. 36 ObjectConfigKeyMagneticAlwaysEnabled ObjectConfigKey = "magnetic-always-enabled" 37 // ObjectConfigKeyMagneticStraightInvert configures the inversion setting 38 // magnetic wires on the magnet driving the straight direction. 39 // Values are true|false|0|1. 40 ObjectConfigKeyMagneticStraightInvert ObjectConfigKey = "magnetic-straight-invert" 41 // ObjectConfigKeyMagneticOffInvert configures the inversion setting 42 // magnetic wires on the magnet driving the off direction. 43 // Values are true|false|0|1. 44 ObjectConfigKeyMagneticOffInvert ObjectConfigKey = "magnetic-off-invert" 45 ) 46 47 // DefaultValue returns the default value for a given configuration key. 48 func (key ObjectConfigKey) DefaultValue() string { 49 switch key { 50 case ObjectConfigKeyDebug, 51 ObjectConfigKeyMagneticAlwaysEnabled, 52 ObjectConfigKeyMagneticStraightInvert, 53 ObjectConfigKeyMagneticOffInvert: 54 return "false" 55 default: 56 return "" 57 } 58 } 59 60 // ValidateValue validates a given value for a given configuration key. 61 func (key ObjectConfigKey) ValidateValue(value string) error { 62 if value == "" { 63 // All ok 64 return nil 65 } 66 switch key { 67 case ObjectConfigKeyDebug, 68 ObjectConfigKeyMagneticAlwaysEnabled, 69 ObjectConfigKeyMagneticStraightInvert, 70 ObjectConfigKeyMagneticOffInvert: 71 if _, err := strconv.ParseBool(value); err != nil { 72 return fmt.Errorf("not a boolean") 73 } 74 } 75 return nil 76 } 77 78 func AllObjectConfigKeys() []ObjectConfigKey { 79 return []ObjectConfigKey{ 80 ObjectConfigKeyDebug, 81 ObjectConfigKeyMagneticAlwaysEnabled, 82 ObjectConfigKeyMagneticStraightInvert, 83 ObjectConfigKeyMagneticOffInvert, 84 } 85 }