Example #1
0
func (this *FileInfo) addStruct(val reflect.Value) error {
	t := val.Type()
	Infof("Add struct %s", t.Name())

	item := StructInfo{
		Type:        t.Name(),
		ReflectType: t,
	}

	fields := getStructFields(val)
	for _, field := range fields {

		alias, stdType := this.getAlias(this.Packet, field.Type)
		el := FieldInfo{
			Name:        field.Tag,
			StructName:  field.Name,
			Type:        alias,
			IsStdType:   stdType,
			IsPointer:   field.Type.Kind() == reflect.Ptr,
			IsMarshal:   msgp.IsSupportMarshal(field.Type),
			IsUnmarshal: msgp.IsSupportUnmarshal(field.Type),
			ReflectType: field.Type,
		}

		switch field.Type.Kind() {
		case reflect.Slice, reflect.Array, reflect.Ptr:
			elType := field.Type.Elem()
			alias, stdType := this.getAlias(this.Packet, elType)

			el.ChildInfo = &FieldInfo{
				Type:        alias,
				IsStdType:   stdType,
				IsPointer:   false,
				IsMarshal:   msgp.IsSupportMarshal(elType),
				IsUnmarshal: msgp.IsSupportUnmarshal(elType),
				ReflectType: elType,
			}
		default:
			break
		}

		item.Fields = append(item.Fields, el)
		Infof("Field: %s (%s, %v)", field.Tag, field.Name, field.Type.Kind() == reflect.Ptr)
	}

	this.Structs[item.Type] = item

	return nil
}
Example #2
0
func writeMapUnmarshal(out *bufio.Writer, fi *FileInfo, s StructInfo) {
	out.WriteString(fmt.Sprintf("func (this *%s) UnmarshalMsg(out []byte) ([]byte, error) {\n", s.Type))
	out.WriteString(fmt.Sprintf("var (\nsize uint32\nerr error\nkey %s\n)\n", s.ReflectType.Key().Name()))
	out.WriteString("size, out, err = msgp.ReadMapHeaderBytes(out)\n")
	out.WriteString(errBlock)
	out.WriteString(fmt.Sprintf("*this = make(%s)\n", s.Type))
	out.WriteString("for idx := uint32(0); idx < size; idx++ {\n")

	//Unmarshal key
	if s.ReflectType.Key().PkgPath() == "" {
		elType := s.ReflectType.Key().Name()
		out.WriteString(fmt.Sprintf("key, out, err = msgp.Read%sBytes(out)\n", strings.ToUpper(elType[:1])+elType[1:]))
	} else {
		if msgp.IsSupportUnmarshal(s.ReflectType.Key()) || fi.IsSupport(s.Type) {
			out.WriteString("out, err = key.UnmarshalMsg(out)\n")
		} else {
			out.WriteString("out, err = msgp.UnmarshalMsg(out, &key)\n")
		}
	}
	out.WriteString(errBlock)

	//Unmarshal value
	out.WriteString(fmt.Sprintf("var val %s\n", s.ReflectType.Elem().Name()))
	if s.ReflectType.Elem().PkgPath() == "" {
		elType := s.ReflectType.Elem().Name()
		out.WriteString(fmt.Sprintf("val, out, err = msgp.Read%sBytes(out)\n", strings.ToUpper(elType[:1])+elType[1:]))
	} else {
		if msgp.IsSupportUnmarshal(s.ReflectType.Elem()) || fi.IsSupport(s.Type) {
			out.WriteString("out, err = val.UnmarshalMsg(out)\n")
		} else {
			out.WriteString("out, err = msgp.UnmarshalMsg(out, &val)\n")
		}
	}
	out.WriteString(errBlock)
	out.WriteString("(*this)[key] = val")
	out.WriteString("\n}\n")
	out.WriteString(endBlock)
}
Example #3
0
func writeArrayUnmarshal(out *bufio.Writer, fi *FileInfo, s StructInfo) {
	out.WriteString(fmt.Sprintf("func (this *%s) UnmarshalMsg(out []byte) ([]byte, error) {\n", s.Type))
	out.WriteString("var (\nsize uint32\nerr error\n)\n")
	out.WriteString("size, out, err = msgp.ReadArrayHeaderBytes(out)\n")
	out.WriteString(errBlock)
	out.WriteString(fmt.Sprintf("*this = make(%s, size)\n", s.Type))
	out.WriteString("for idx := uint32(0); idx < size; idx++ {\n")

	if s.ReflectType.Elem().PkgPath() == "" {
		elType := s.ReflectType.Elem().Name()
		out.WriteString(fmt.Sprintf("(*this)[idx], out, err = msgp.Read%sBytes(out)\n", strings.ToUpper(elType[:1])+elType[1:]))
	} else {
		if msgp.IsSupportUnmarshal(s.ReflectType.Elem()) || fi.IsSupport(s.Type) {
			out.WriteString("out, err = (*this)[idx].UnmarshalMsg(out)\n")
		} else {
			out.WriteString("out, err = msgp.UnmarshalMsg(out, &(*this)[idx])\n")
		}
	}
	out.WriteString(errBlock)

	out.WriteString("\n}\n")
	out.WriteString(endBlock)
}