本文整理汇总了Golang中reflect.Value.String方法的典型用法代码示例。如果您正苦于以下问题:Golang Value.String方法的具体用法?Golang Value.String怎么用?Golang Value.String使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reflect.Value
的用法示例。
在下文中一共展示了Value.String方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: indexBytes
func indexBytes(typ reflect.Type, value reflect.Value) (b []byte, err error) {
switch typ.Kind() {
case reflect.String:
b = []byte(value.String())
case reflect.Int:
buf := new(bytes.Buffer)
if err = binary.Write(buf, binary.BigEndian, value.Int()); err != nil {
return
}
b = buf.Bytes()
case reflect.Slice:
switch typ.Elem().Kind() {
case reflect.Uint8:
b = value.Bytes()
default:
err = fmt.Errorf("%v is not an indexable type", typ)
}
case reflect.Bool:
if value.Bool() {
b = []byte{1}
} else {
b = []byte{0}
}
default:
err = fmt.Errorf("%v is not an indexable type", typ)
return
}
if len(b) == 0 {
b = []byte{0}
}
return
}
示例2: IsBytesValidation
// IsBytesValidation will return if a field is a parseable bytes value.
func IsBytesValidation(v *validator.Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
if fieldKind != reflect.String {
// this is an issue with the code and really should be a panic
return true
}
if field.String() == "" {
return true
}
if hasReplTemplate(field) {
// all bets are off
return true
}
parts := bytesRe.FindStringSubmatch(strings.TrimSpace(field.String()))
if len(parts) < 3 {
return false
}
value, err := strconv.ParseUint(parts[1], 10, 0)
if err != nil || value < 1 {
return false
}
return true
}
示例3: ClusterInstanceFalse
func ClusterInstanceFalse(v *validator.Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
if fieldKind != reflect.String {
return true
}
valueStr := field.String()
if valueStr == "" {
return true
}
if hasReplTemplate(field) {
// all bets are off
return true
}
currentContainer := getCurrentContainer(currentStructOrField)
if currentContainer == nil {
// this is an issue with the code and really should be a panic
return true
}
cluster, err := currentContainer.Cluster.ParseBool()
if err != nil {
// don't worry about this here. cluster should have the "bool" validator.
return true
}
if cluster {
return false
}
return true
}
示例4: GetValQuoteStr
// for insert and update
// If already assigned, then just ignore tag
func GetValQuoteStr(val reflect.Value) (string, error) {
switch val.Kind() {
case reflect.Bool:
boolStr := "N"
if val.Bool() {
boolStr = "Y"
}
return boolStr, nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return strconv.FormatInt(val.Int(), 10), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return strconv.FormatUint(val.Uint(), 10), nil
case reflect.Float32, reflect.Float64:
return strconv.FormatFloat(val.Float(), 'f', -1, 64), nil
case reflect.String:
return QuoteStr(val.String()), nil
case reflect.Slice: //[]byte
if val.Type().Elem().Name() != "uint8" {
return "", fmt.Errorf("GetValQuoteStr> slicetype is not []byte: %v", val.Interface())
}
return QuoteStr(string(val.Interface().([]byte))), nil
default:
return "", fmt.Errorf("GetValQuoteStr> reflect.Value is not a string/int/uint/float/bool/[]byte!\nval: %v", val)
}
return "", nil
}
示例5: ConfigItemWhenValidation
// ConfigItemWhenValidation will validate that the when element of a config item is in a valid format and references other valid, created objects.
func ConfigItemWhenValidation(v *validator.Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
root, ok := topStruct.Interface().(*RootConfig)
if !ok {
// this is an issue with the code and really should be a panic
return true
}
if fieldKind != reflect.String {
// this is an issue with the code and really should be a panic
return true
}
var whenValue string
whenValue = field.String()
if whenValue == "" {
return true
}
splitString := "="
if strings.Contains(whenValue, "!=") {
splitString = "!="
}
parts := strings.SplitN(whenValue, splitString, 2)
if len(parts) >= 2 {
whenValue = parts[0]
}
return configItemExists(whenValue, root)
}
示例6: value2Interface
func (this *databaseImplement) value2Interface(fieldValue reflect.Value) (interface{}, error) {
fieldType := fieldValue.Type()
fieldTypeKind := fieldType.Kind()
switch fieldTypeKind {
case reflect.Bool:
return fieldValue.Bool(), nil
case reflect.String:
return fieldValue.String(), nil
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
return fieldValue.Int(), nil
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
return fieldValue.Uint(), nil
case reflect.Float32, reflect.Float64:
return fieldValue.Float(), nil
case reflect.Struct:
if fieldType == reflect.TypeOf(time.Time{}) {
t := fieldValue.Interface().(time.Time)
tf := t.Format("2006-01-02 15:04:05")
return tf, nil
} else {
return nil, fmt.Errorf("Unsupported type %v", fieldType)
}
default:
return nil, fmt.Errorf("Unsupported type %v", fieldType)
}
}
示例7: marshalSimple
func (p *printer) marshalSimple(typ reflect.Type, val reflect.Value) (string, []byte, error) {
switch val.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return strconv.FormatInt(val.Int(), 10), nil, nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return strconv.FormatUint(val.Uint(), 10), nil, nil
case reflect.Float32, reflect.Float64:
return strconv.FormatFloat(val.Float(), 'g', -1, val.Type().Bits()), nil, nil
case reflect.String:
return val.String(), nil, nil
case reflect.Bool:
return strconv.FormatBool(val.Bool()), nil, nil
case reflect.Array:
if typ.Elem().Kind() != reflect.Uint8 {
break
}
// [...]byte
var bytes []byte
if val.CanAddr() {
bytes = val.Slice(0, val.Len()).Bytes()
} else {
bytes = make([]byte, val.Len())
reflect.Copy(reflect.ValueOf(bytes), val)
}
return "", bytes, nil
case reflect.Slice:
if typ.Elem().Kind() != reflect.Uint8 {
break
}
// []byte
return "", val.Bytes(), nil
}
return "", nil, &UnsupportedTypeError{typ}
}
示例8: Validate
// Validate checks whether the given value is writeable to this schema.
func (*NullSchema) Validate(v reflect.Value) bool {
// Check if the value is something that can be null
switch v.Kind() {
case reflect.Interface:
return v.IsNil()
case reflect.Array:
return v.Cap() == 0
case reflect.Slice:
return v.IsNil() || v.Cap() == 0
case reflect.Map:
return len(v.MapKeys()) == 0
case reflect.String:
return len(v.String()) == 0
case reflect.Float32:
// Should NaN floats be treated as null?
return math.IsNaN(v.Float())
case reflect.Float64:
// Should NaN floats be treated as null?
return math.IsNaN(v.Float())
case reflect.Ptr:
return v.IsNil()
case reflect.Invalid:
return true
}
// Nothing else in particular, so this should not validate?
return false
}
示例9: encodeCol
// Returns the string representation of the field value
func (enc *encoder) encodeCol(fv reflect.Value, st reflect.StructTag) string {
switch fv.Kind() {
case reflect.String:
return fv.String()
case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int8:
return fmt.Sprintf("%v", fv.Int())
case reflect.Float32:
return encodeFloat(32, fv)
case reflect.Float64:
return encodeFloat(64, fv)
case reflect.Bool:
return encodeBool(fv.Bool(), st)
case reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint8:
return fmt.Sprintf("%v", fv.Uint())
case reflect.Complex64, reflect.Complex128:
return fmt.Sprintf("%+.3g", fv.Complex())
case reflect.Interface:
return encodeInterface(fv, st)
case reflect.Struct:
return encodeInterface(fv, st)
default:
panic(fmt.Sprintf("Unsupported type %s", fv.Kind()))
}
return ""
}
示例10: encodeScalar
func (e *Encoder) encodeScalar(av *dynamodb.AttributeValue, v reflect.Value, fieldTag tag) error {
if v.Type() == numberType {
s := v.String()
if fieldTag.AsString {
av.S = &s
} else {
av.N = &s
}
return nil
}
switch v.Kind() {
case reflect.Bool:
av.BOOL = new(bool)
*av.BOOL = v.Bool()
case reflect.String:
if err := e.encodeString(av, v); err != nil {
return err
}
default:
// Fallback to encoding numbers, will return invalid type if not supported
if err := e.encodeNumber(av, v); err != nil {
return err
}
if fieldTag.AsString && av.NULL == nil && av.N != nil {
av.S = av.N
av.N = nil
}
}
return nil
}
示例11: encode
func encode(b []byte, rv reflect.Value) ([]byte, error) {
switch rk := rv.Kind(); rk {
case reflect.Bool:
b = encodeBool(b, rv.Bool())
case reflect.Int, reflect.Int8, reflect.Int64, reflect.Int32, reflect.Int16:
b = encodeInt(b, rv.Int())
case reflect.Uint8, reflect.Uint64, reflect.Uint, reflect.Uint32, reflect.Uint16:
b = encodeInt(b, int64(rv.Uint()))
case reflect.String:
b = encodeString(b, rv.String())
case reflect.Array, reflect.Slice:
if rv.Len() == 0 && rv.Type().Elem().Kind() == reflect.Uint8 {
b = encodeBytes(b, rv.Bytes())
} else if rv.Index(0).Kind() == reflect.Uint8 {
b = encodeBytes(b, rv.Bytes())
} else {
b = encodeArray(b, rv)
}
case reflect.Map:
b = encodeMap(b, rv)
case reflect.Struct:
b = encodeStruct(b, rv)
default:
panic("no support for type")
}
return b, nil
}
示例12: MapElem
func (w *interpolationWalker) MapElem(m, k, v reflect.Value) error {
w.csData = k
w.csKey = append(w.csKey, k)
w.key = append(w.key, k.String())
w.lastValue = v
return nil
}
示例13: refToVal
func refToVal(ref reflect.Value) Value {
switch ref.Kind() {
case reflect.Bool:
return boolValue(ref.Bool())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return intValue(ref.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32,
reflect.Uint64, reflect.Uintptr:
return intValue(ref.Uint())
case reflect.Float32, reflect.Float64:
return floatValue(ref.Float())
case reflect.Complex64, reflect.Complex128:
return complexValue(ref.Complex())
case reflect.Array, reflect.Slice:
return arrayValue{reflectValue(ref)}
case reflect.Chan:
return chanValue{reflectValue(ref)}
case reflect.Map:
return mapValue{reflectValue(ref)}
case reflect.Ptr:
return pointerValue{reflectValue(ref)}
case reflect.String:
return stringValue(ref.String())
case reflect.Struct:
return structValue{reflectValue(ref)}
}
return nilValue(0)
}
示例14: plain_extract_value
func plain_extract_value(name string, field reflect.Value) url.Values {
values := make(url.Values)
switch field.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
values.Add(name, strconv.FormatInt(field.Int(), 10))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
values.Add(name, strconv.FormatUint(field.Uint(), 10))
case reflect.Bool:
values.Add(name, strconv.FormatBool(field.Bool()))
case reflect.Struct:
values = merge(values, plain_extract_struct(field))
case reflect.Slice:
for i := 0; i < field.Len(); i++ {
sv := field.Index(i)
values = merge(values, plain_extract_value(name, sv))
}
case reflect.String:
values.Add(name, field.String())
case reflect.Float32, reflect.Float64:
values.Add(name, strconv.FormatFloat(field.Float(), 'f', -1, 64))
case reflect.Ptr, reflect.Interface:
values = merge(values, plain_extract_pointer(field))
}
return values
}
示例15: ToFloat64
// toFloat64 convert all reflect.Value-s into float64.
func ToFloat64(v reflect.Value) float64 {
if v.Kind() == reflect.Interface {
v = v.Elem()
}
switch v.Kind() {
case reflect.Float32, reflect.Float64:
return v.Float()
case reflect.Int16, reflect.Int8, reflect.Int, reflect.Int32, reflect.Int64:
return float64(v.Int())
case reflect.String:
s := v.String()
var f float64
var err error
if strings.HasPrefix(s, "0x") {
f, err = strconv.ParseFloat(s, 64)
} else {
f, err = strconv.ParseFloat(s, 64)
}
if err == nil {
return float64(f)
}
case reflect.Slice:
// Should we grab first one?
item1 := v.Index(0)
u.Infof("is slice of strings?: %T", v, item1)
default:
u.Warnf("Cannot convert type? %v", v.Kind())
}
return math.NaN()
}