github.com/sacloud/iaas-api-go@v1.12.0/types/web_ui.go (about) 1 // Copyright 2022-2023 The sacloud/iaas-api-go Authors 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 package types 16 17 import ( 18 "encoding/json" 19 "fmt" 20 ) 21 22 // WebUI データベースアプライアンスでのUI設定 23 type WebUI string 24 25 // String WebUIの文字列表現 26 func (w *WebUI) String() string { 27 return string(*w) 28 } 29 30 // Bool WebUIの有効/無効 31 func (w *WebUI) Bool() bool { 32 s := w.String() 33 if s == "" || s == "false" { 34 return false 35 } 36 return true 37 } 38 39 // ToWebUI bool値からWebUI型へ変換 40 func ToWebUI(v bool) WebUI { 41 return WebUI(fmt.Sprintf("%t", v)) 42 } 43 44 // MarshalJSON boolとstring両方に対応するための実装 45 func (w WebUI) MarshalJSON() ([]byte, error) { 46 s := string(w) 47 switch s { 48 case "true", `1`, `"1"`: 49 return json.Marshal(true) 50 case "", `""`, `0`, `"0"`, "false": 51 return json.Marshal(false) 52 default: 53 return json.Marshal(s) 54 } 55 } 56 57 // UnmarshalJSON 文字列/boolが混在することへの対応 58 func (w *WebUI) UnmarshalJSON(b []byte) error { 59 s := string(b) 60 switch s { 61 case "true", `1`, `"1"`: 62 *w = WebUI("true") 63 case "", `""`, `0`, `"0"`, "false": 64 *w = WebUI("false") 65 default: 66 *w = WebUI(s) 67 } 68 return nil 69 }