本文整理汇总了Golang中reflect.Value类的典型用法代码示例。如果您正苦于以下问题:Golang Value类的具体用法?Golang Value怎么用?Golang Value使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Value类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: encode
func (se *sliceEncoder) encode(e *encodeState, v reflect.Value, _ bool) {
if v.IsNil() {
e.WriteString("null")
return
}
se.arrayEnc(e, v, false)
}
示例2: WriteValue
func (par field) WriteValue(out io.Writer, valueOf reflect.Value) error {
if (par.Flags & (fieldNotAny | fieldFollowedBy)) != 0 {
return nil
}
if par.Index < 0 { // We can not out this value in all cases but if it was literal we can do it
// TODO: Check if it is string and output only in case it is literal
p := par.Parse
v := valueOf
for {
switch tp := p.(type) {
case *ptrParser:
p = tp.Parser
if v.IsNil() {
if tp.Optional {
return nil
}
return errors.New("Ptr value is nil")
}
v = v.Elem()
break
case *literalParser:
_, err := out.Write([]byte(tp.Literal))
return err
default:
return errors.New("Could not out anonymous field if it is not literal")
}
}
} else {
f := valueOf.Field(par.Index)
return par.Parse.WriteValue(out, f)
}
}
示例3: getCurrentContainer
func getCurrentContainer(current reflect.Value) *Container {
if !current.CanAddr() {
return nil
}
currentContainer, _ := current.Addr().Interface().(*Container)
return currentContainer
}
示例4: ContainerNameUnique
func ContainerNameUnique(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
}
containerName := field.String()
if containerName == "" {
return true
}
if hasReplTemplate(field) {
// all bets are off
return true
}
root, ok := topStruct.Interface().(*RootConfig)
if !ok {
// this is an issue with the code and really should be a panic
return true
}
currentContainer := getCurrentContainer(currentStructOrField)
if currentContainer == nil {
// this is an issue with the code and really should be a panic
return true
}
container := getContainerFromName(containerName, currentContainer, root)
if container != nil {
return false
}
return true
}
示例5: ComponentExistsValidation
// ComponentExistsValidation will validate that the specified component name is present in the current YAML.
func ComponentExistsValidation(v *validator.Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
// validates that the component exists in the root.Components slice
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 componentName string
componentName = field.String()
parts := strings.SplitN(componentName, ",", 2)
if len(parts) >= 2 {
componentName = parts[0]
}
return componentExists(componentName, root)
}
示例6: EncodeValue
// EncodeValue transmits the data item represented by the reflection value,
// guaranteeing that all necessary type information has been transmitted first.
func (enc *Encoder) EncodeValue(value reflect.Value) os.Error {
// Make sure we're single-threaded through here, so multiple
// goroutines can share an encoder.
enc.mutex.Lock()
defer enc.mutex.Unlock()
enc.err = nil
rt, _ := indirect(value.Type())
// Sanity check only: encoder should never come in with data present.
if enc.state.b.Len() > 0 || enc.countState.b.Len() > 0 {
enc.err = os.ErrorString("encoder: buffer not empty")
return enc.err
}
enc.sendTypeDescriptor(rt)
if enc.err != nil {
return enc.err
}
// Encode the object.
err := enc.encode(enc.state.b, value)
if err != nil {
enc.setError(err)
} else {
enc.send()
}
return enc.err
}
示例7: ParseValue
func (par *boolParser) ParseValue(ctx *parseContext, valueOf reflect.Value, location int, err *Error) int {
if strAt(ctx.str, location, "true") {
valueOf.SetBool(true)
location += 4
} else if strAt(ctx.str, location, "false") {
valueOf.SetBool(false)
location += 5
} else {
err.Location = location
err.Message = boolError
return -1
}
if location < len(ctx.str) {
if ctx.str[location] == '_' ||
(ctx.str[location] >= 'a' && ctx.str[location] <= 'z') ||
(ctx.str[location] >= 'A' && ctx.str[location] <= 'Z') ||
(ctx.str[location] >= '0' && ctx.str[location] <= '9') {
err.Location = location
err.Message = boolError
return -1
}
}
return location
}
示例8: getField
// getField gets the i'th field of the struct value.
// If the field is itself is an interface, return a value for
// the thing inside the interface, not the interface itself.
func getField(v reflect.Value, i int) reflect.Value {
val := v.Field(i)
if val.Kind() == reflect.Interface && !val.IsNil() {
val = val.Elem()
}
return val
}
示例9: readArrayDocTo
func (d *decoder) readArrayDocTo(out reflect.Value) {
end := int(d.readInt32())
end += d.i - 4
if end <= d.i || end > len(d.in) || d.in[end-1] != '\x00' {
corrupted()
}
i := 0
l := out.Len()
for d.in[d.i] != '\x00' {
if i >= l {
panic("Length mismatch on array field")
}
kind := d.readByte()
for d.i < end && d.in[d.i] != '\x00' {
d.i++
}
if d.i >= end {
corrupted()
}
d.i++
d.readElemTo(out.Index(i), kind)
if d.i >= end {
corrupted()
}
i++
}
if i != l {
panic("Length mismatch on array field")
}
d.i++ // '\x00'
if d.i != end {
corrupted()
}
}
示例10: parseValue
func (q *queryParser) parseValue(v url.Values, value reflect.Value, prefix string, tag reflect.StructTag) error {
value = elemOf(value)
// no need to handle zero values
if !value.IsValid() {
return nil
}
t := tag.Get("type")
if t == "" {
switch value.Kind() {
case reflect.Struct:
t = "structure"
case reflect.Slice:
t = "list"
case reflect.Map:
t = "map"
}
}
switch t {
case "structure":
return q.parseStruct(v, value, prefix)
case "list":
return q.parseList(v, value, prefix, tag)
case "map":
return q.parseMap(v, value, prefix, tag)
default:
return q.parseScalar(v, value, prefix, tag)
}
}
示例11: writeExtensions
// writeExtensions writes all the extensions in pv.
// pv is assumed to be a pointer to a protocol message struct that is extendable.
func writeExtensions(w *textWriter, pv reflect.Value) error {
emap := extensionMaps[pv.Type().Elem()]
ep := pv.Interface().(extendableProto)
// Order the extensions by ID.
// This isn't strictly necessary, but it will give us
// canonical output, which will also make testing easier.
var m map[int32]Extension
if em, ok := ep.(extensionsMap); ok {
m = em.ExtensionMap()
} else if em, ok := ep.(extensionsBytes); ok {
eb := em.GetExtensions()
var err error
m, err = BytesToExtensionsMap(*eb)
if err != nil {
return err
}
}
ids := make([]int32, 0, len(m))
for id := range m {
ids = append(ids, id)
}
sort.Sort(int32Slice(ids))
for _, extNum := range ids {
ext := m[extNum]
var desc *ExtensionDesc
if emap != nil {
desc = emap[extNum]
}
if desc == nil {
// Unknown extension.
if err := writeUnknownStruct(w, ext.enc); err != nil {
return err
}
continue
}
pb, err := GetExtension(ep, desc)
if err != nil {
return fmt.Errorf("failed getting extension: %v", err)
}
// Repeated extensions will appear as a slice.
if !desc.repeated() {
if err := writeExtension(w, desc.Name, pb); err != nil {
return err
}
} else {
v := reflect.ValueOf(pb)
for i := 0; i < v.Len(); i++ {
if err := writeExtension(w, desc.Name, v.Index(i).Interface()); err != nil {
return err
}
}
}
}
return nil
}
示例12: unflattenValue
func unflattenValue(v reflect.Value, t reflect.Type) reflect.Value {
// When t is an Interface, we can't do much, since we don't know the
// original (unflattened) type of the value placed in v, so we just nop it.
if t.Kind() == reflect.Interface {
return v
}
// v can be invalid, if it holds the nil value for pointer type
if !v.IsValid() {
return v
}
// Make sure v is indeed flat
if v.Kind() == reflect.Ptr {
panic("unflattening non-flat value")
}
// Add a *, one at a time
for t.Kind() == reflect.Ptr {
if v.CanAddr() {
v = v.Addr()
} else {
pw := reflect.New(v.Type())
pw.Elem().Set(v)
v = pw
}
t = t.Elem()
}
return v
}
示例13: bypassCanInterface
// bypassCanInterface returns a version of v that
// bypasses the CanInterface check.
func bypassCanInterface(v reflect.Value) reflect.Value {
if !v.IsValid() || v.CanInterface() {
return v
}
*flagField(&v) &^= flagRO
return v
}
示例14: value2error
// convert a value back to the original error interface. panics if value is not
// nil and also does not implement error.
func value2error(v reflect.Value) error {
i := v.Interface()
if i == nil {
return nil
}
return i.(error)
}
示例15: GetMethod
func GetMethod(s reflect.Value, name string) reflect.Value {
method := s.MethodByName(name)
if !method.IsValid() {
method = s.Elem().MethodByName(name)
}
return method
}