1// Copyright 2024 Google Inc. All rights reserved. 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 15package release_config_lib 16 17import ( 18 "strings" 19 20 rc_proto "android/soong/cmd/release_config/release_config_proto" 21) 22 23type FlagValue struct { 24 // The path providing this value. 25 path string 26 27 // Protobuf 28 proto rc_proto.FlagValue 29} 30 31func FlagValueFactory(protoPath string) (fv *FlagValue) { 32 fv = &FlagValue{path: protoPath} 33 if protoPath != "" { 34 LoadMessage(protoPath, &fv.proto) 35 } 36 return fv 37} 38 39func UnmarshalValue(str string) *rc_proto.Value { 40 ret := &rc_proto.Value{} 41 switch v := strings.ToLower(str); v { 42 case "true": 43 ret = &rc_proto.Value{Val: &rc_proto.Value_BoolValue{true}} 44 case "false": 45 ret = &rc_proto.Value{Val: &rc_proto.Value_BoolValue{false}} 46 case "##obsolete": 47 ret = &rc_proto.Value{Val: &rc_proto.Value_Obsolete{true}} 48 default: 49 ret = &rc_proto.Value{Val: &rc_proto.Value_StringValue{str}} 50 } 51 return ret 52} 53 54func MarshalValue(value *rc_proto.Value) string { 55 if value == nil { 56 return "" 57 } 58 switch val := value.Val.(type) { 59 case *rc_proto.Value_UnspecifiedValue: 60 // Value was never set. 61 return "" 62 case *rc_proto.Value_StringValue: 63 return val.StringValue 64 case *rc_proto.Value_BoolValue: 65 if val.BoolValue { 66 return "true" 67 } 68 // False ==> empty string 69 return "" 70 case *rc_proto.Value_Obsolete: 71 return " #OBSOLETE" 72 default: 73 // Flagged as error elsewhere, so return empty string here. 74 return "" 75 } 76} 77 78// Returns a string representation of the type of the value for make 79func ValueType(value *rc_proto.Value) string { 80 if value == nil || value.Val == nil { 81 return "unspecified" 82 } 83 switch value.Val.(type) { 84 case *rc_proto.Value_UnspecifiedValue: 85 return "unspecified" 86 case *rc_proto.Value_StringValue: 87 return "string" 88 case *rc_proto.Value_BoolValue: 89 return "bool" 90 case *rc_proto.Value_Obsolete: 91 return "obsolete" 92 default: 93 panic("Unhandled type") 94 } 95} 96