Exemplo n.º 1
0
// Makes a Vector3 StateValue. Panics if the StructValue fields do not match the vector.
func makeVector3(v *reflect.StructValue) *protocol.StateValue {
	// If we panic here, struct layout was not as expected
	x := v.FieldByName("X").(*reflect.FloatValue).Get()
	y := v.FieldByName("Y").(*reflect.FloatValue).Get()
	z := v.FieldByName("Z").(*reflect.FloatValue).Get()

	vector3 := &protocol.Vector3{&x, &y, &z, nil}
	sv := &protocol.StateValue{
		Type:       protocol.NewStateValue_Type(protocol.StateValue_VECTOR3),
		Vector3Val: vector3,
	}
	return sv
}
Exemplo n.º 2
0
func (m *Model) translateMap(obj *reflect.StructValue) map[string]Value {
	ret := make(map[string]Value)
	for attr, typ := range m.attributes {
		switch typ.(type) {
		case *reflect.IntType:
			ret[attr] = SysInt(obj.FieldByName(attr).(*reflect.IntValue).Get()).Value()
		case *reflect.StringType:
			ret[attr] = SysString(obj.FieldByName(attr).(*reflect.StringValue).Get()).Value()
		case nil:
			ret[attr] = new(_Null)
		}
	}
	return ret
}
Exemplo n.º 3
0
func (m *Model) buildUpdateMap(st *reflect.StructValue, old map[string]Value) map[string]Value {
	ret := make(map[string]Value)
	for attr, typ := range m.attributes {
		switch typ.(type) {
		case *reflect.IntType:
			if tmp := st.FieldByName(attr).(*reflect.IntValue).Get(); int(old[strings.ToLower(attr)].Int()) != tmp {
				ret[attr] = SysInt(tmp).Value()
			}
		case *reflect.StringType:
			if tmp := st.FieldByName(attr).(*reflect.StringValue).Get(); string(old[strings.ToLower(attr)].String()) != tmp {
				ret[attr] = SysString(tmp).Value()
			}
		}
	}
	return ret
}
Exemplo n.º 4
0
func (m *Model) translateObjectValue(v map[string]Value, p *reflect.StructValue) interface{} {
	for lbl, _ := range m.Attributes() {
		vl := v[strings.ToLower(lbl)]
		switch vl.Kind() {
		//TODO MakeZero ??
		case IntKind:
			tmp := reflect.NewValue(1).(*reflect.IntValue)
			tmp.Set(int(vl.Int()))
			p.FieldByName(lbl).SetValue(tmp)
		case StringKind:
			tmp := reflect.NewValue("").(*reflect.StringValue)
			tmp.Set(string(vl.String()))
			p.FieldByName(lbl).SetValue(tmp)
		}
	}
	m.object_cache[int(v[strings.ToLower(m.identifier)].Int())] = v
	return p.Interface()

}
Exemplo n.º 5
0
// object consumes an object from d.data[d.off-1:], decoding into the value v.
// the first byte of the object ('{') has been read already.
func (d *decodeState) object(v reflect.Value) {
	// Check for unmarshaler.
	unmarshaler, pv := d.indirect(v, false)
	if unmarshaler != nil {
		d.off--
		err := unmarshaler.UnmarshalJSON(d.next())
		if err != nil {
			d.error(err)
		}
		return
	}
	v = pv

	// Decoding into nil interface?  Switch to non-reflect code.
	iv, ok := v.(*reflect.InterfaceValue)
	if ok {
		iv.Set(reflect.NewValue(d.objectInterface()))
		return
	}

	// Check type of target: struct or map[string]T
	var (
		mv *reflect.MapValue
		sv *reflect.StructValue
	)
	switch v := v.(type) {
	case *reflect.MapValue:
		// map must have string type
		t := v.Type().(*reflect.MapType)
		if t.Key() != reflect.Typeof("") {
			d.saveError(&UnmarshalTypeError{"object", v.Type()})
			break
		}
		mv = v
		if mv.IsNil() {
			mv.SetValue(reflect.MakeMap(t))
		}
	case *reflect.StructValue:
		sv = v
	default:
		d.saveError(&UnmarshalTypeError{"object", v.Type()})
	}

	if mv == nil && sv == nil {
		d.off--
		d.next() // skip over { } in input
		return
	}

	for {
		// Read opening " of string key or closing }.
		op := d.scanWhile(scanSkipSpace)
		if op == scanEndObject {
			// closing } - can only happen on first iteration.
			break
		}
		if op != scanBeginLiteral {
			d.error(errPhase)
		}

		// Read string key.
		start := d.off - 1
		op = d.scanWhile(scanContinue)
		item := d.data[start : d.off-1]
		key, ok := unquote(item)
		if !ok {
			d.error(errPhase)
		}

		// Figure out field corresponding to key.
		var subv reflect.Value
		if mv != nil {
			subv = reflect.MakeZero(mv.Type().(*reflect.MapType).Elem())
		} else {
			// First try for field with that tag.
			for i := 0; i < sv.NumField(); i++ {
				f := sv.Type().(*reflect.StructType).Field(i)
				if f.Tag == key {
					subv = sv.Field(i)
					break
				}
			}
			if subv == nil {
				// Second, exact match.
				subv = sv.FieldByName(key)
				if subv == nil {
					// Third, case-insensitive match.
					subv = sv.FieldByNameFunc(func(s string) bool { return matchName(key, s) })
				}
			}
		}

		// Read : before value.
		if op == scanSkipSpace {
			op = d.scanWhile(scanSkipSpace)
		}
		if op != scanObjectKey {
			d.error(errPhase)
		}

		// Read value.
		d.value(subv)

		// Write value back to map;
		// if using struct, subv points into struct already.
		if mv != nil {
			mv.SetElem(reflect.NewValue(key), subv)
		}

		// Next token must be , or }.
		op = d.scanWhile(scanSkipSpace)
		if op == scanEndObject {
			break
		}
		if op != scanObjectValue {
			d.error(errPhase)
		}
	}
}
Exemplo n.º 6
0
func (m *Model) getId(st *reflect.StructValue) int {
	return st.FieldByName(m.identifier).(*reflect.IntValue).Get()
}