/* * Copyright (C) 2017 eschao * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package config import ( "fmt" "reflect" "strconv" "strings" "time" ) func SetValueWithBool(value reflect.Value, boolValue string) error { b, err := strconv.ParseBool(boolValue) if err != nil { return fmt.Errorf("parse bool: %w", err) } value.SetBool(b) return nil } func SetValueWithDuration(value reflect.Value, str string) error { d, err := time.ParseDuration(str) if err != nil { return fmt.Errorf("parse duration: %w", err) } value.Set(reflect.ValueOf(d)) return nil } func SetValueWithFloatX(value reflect.Value, floatValue string, bitSize int) error { f, err := strconv.ParseFloat(floatValue, bitSize) if err != nil { return fmt.Errorf("parse float: %w", err) } value.SetFloat(f) return nil } func SetValueWithIntX(value reflect.Value, intValue string, bitSize int) error { v, err := strconv.ParseInt(intValue, 10, bitSize) if err != nil { return fmt.Errorf("parse int: %w", err) } value.SetInt(v) return nil } func SetValueWithUintX(value reflect.Value, uintValue string, bitSize int) error { v, err := strconv.ParseUint(uintValue, 10, bitSize) if err != nil { return fmt.Errorf("parse uint: %w", err) } value.SetUint(v) return nil } func SetValueWithSlice(value reflect.Value, slice string, sep string) error { data := strings.Split(slice, sep) size := len(data) if size > 0 { slice := reflect.MakeSlice(value.Type(), size, size) for index := range size { var err error ele := slice.Index(index) kind := ele.Kind() switch kind { case reflect.Bool: err = SetValueWithBool(ele, data[index]) case reflect.String: ele.SetString(data[index]) case reflect.Uint8: err = SetValueWithUintX(ele, data[index], Uint8Size) case reflect.Uint16: err = SetValueWithUintX(ele, data[index], Uint16Size) case reflect.Uint, reflect.Uint32: err = SetValueWithUintX(ele, data[index], Uint32Size) case reflect.Uint64: err = SetValueWithUintX(ele, data[index], Uint64Size) case reflect.Int8: err = SetValueWithIntX(ele, data[index], Int8Size) case reflect.Int16: err = SetValueWithIntX(ele, data[index], Int16Size) case reflect.Int, reflect.Int32: err = SetValueWithIntX(ele, data[index], Int32Size) case reflect.Int64: err = SetValueWithIntX(ele, data[index], Int64Size) case reflect.Float32: err = SetValueWithFloatX(ele, data[index], Float32Size) case reflect.Float64: err = SetValueWithFloatX(ele, data[index], Float64Size) default: return fmt.Errorf("%w: %s", errUnsupportedType, kind.String()) } if err != nil { return err } } value.Set(slice) } return nil }