Exemple #1
0
func encodeFixedSize(field interface{}, kind uint8, writer *bufio.Writer) {

	// Deal with vague types from JSON data
	switch field.(type) {
	case int:
		field = convertIntToFixedSize(field, reflect.Kind(kind))
	case float64:
		field = convertFloatToFixedSize(field, reflect.Kind(kind))
	}

	var err = binary.Write(writer, binary.BigEndian, field)
	if err != nil {
		panic(fmt.Sprintf("Fixed size encode error: %v\n", err))
	}
}
Exemple #2
0
func (tm *TypeMap) basicRuntimeType(b *types.Basic) llvm.Value {
	commonType := tm.makeCommonType(b, reflect.Kind(b.Kind))
	result := llvm.AddGlobal(tm.module, commonType.Type(), "")
	elementTypes := tm.runtimeCommonType.StructElementTypes()
	ptr := llvm.ConstBitCast(result, elementTypes[9])
	commonType = llvm.ConstInsertValue(commonType, ptr, []uint32{9})
	result.SetInitializer(commonType)
	return result
}
Exemple #3
0
func (c ConvertFuncs) get(val reflect.Value) ConvertFunc {
	if _, ok := val.Interface().(Config); ok {
		return convertConfig
	}

	kind := val.Kind()
	if kind > reflect.Kind(len(c)) {
		return nil
	}
	return c[kind]
}
Exemple #4
0
func main() {
	defer func() {
		if p := recover(); p != nil {
			fmt.Fprintf(os.Stderr, "recover: %v\n", p)
			fmt.Printf("recover: %v\n", p)
		}
	}()
	type intst []int
	//ints := []int{1, 2, 3}
	ints := intst{1, 2, 3}
	v := reflect.ValueOf(ints)
	fmt.Println(v)
	fmt.Println(v.Kind())
	fmt.Println(v.Type())
	fmt.Println(v.Type().Elem())
	fmt.Println(v.Type().Elem().Elem())
	fmt.Println(reflect.New(v.Type().Elem()))
	fmt.Println(reflect.New(v.Type().Elem()).Elem())
	k := reflect.Kind(1)
	fmt.Println(k.String())

}
Exemple #5
0
func getKind(e *Entry) reflect.Kind {
	integer, _ := e.Val(AttrGoKind).(int64)
	return reflect.Kind(integer)
}
Exemple #6
0
func (c MemPColor) RGBA() (r, g, b, a uint32) {
	if len(c.Pix) == 0 {
		return
	}
	switch c.Channels {
	case 1:
		switch reflect.Kind(c.DataType) {
		case reflect.Uint8:
			return color.Gray{
				Y: c.Pix[0],
			}.RGBA()
		case reflect.Uint16:
			return color.Gray16{
				Y: c.Pix.Uint16s()[0],
			}.RGBA()
		default:
			return color.Gray16{
				Y: uint16(c.Pix.Value(0, reflect.Kind(c.DataType))),
			}.RGBA()
		}
	case 2:
		switch reflect.Kind(c.DataType) {
		case reflect.Uint8:
			return color.RGBA{
				R: c.Pix[0],
				G: c.Pix[1],
				B: 0xFF,
				A: 0xFF,
			}.RGBA()
		case reflect.Uint16:
			return color.RGBA64{
				R: c.Pix.Uint16s()[0],
				G: c.Pix.Uint16s()[1],
				B: 0xFFFF,
				A: 0xFFFF,
			}.RGBA()
		default:
			return color.RGBA64{
				R: uint16(c.Pix.Value(0, reflect.Kind(c.DataType))),
				G: uint16(c.Pix.Value(1, reflect.Kind(c.DataType))),
				B: 0xFFFF,
				A: 0xFFFF,
			}.RGBA()
		}
	case 3:
		switch reflect.Kind(c.DataType) {
		case reflect.Uint8:
			return color.RGBA{
				R: c.Pix[0],
				G: c.Pix[1],
				B: c.Pix[2],
				A: 0xFF,
			}.RGBA()
		case reflect.Uint16:
			return color.RGBA64{
				R: c.Pix.Uint16s()[0],
				G: c.Pix.Uint16s()[1],
				B: c.Pix.Uint16s()[2],
				A: 0xFFFF,
			}.RGBA()
		default:
			return color.RGBA64{
				R: uint16(c.Pix.Value(0, reflect.Kind(c.DataType))),
				G: uint16(c.Pix.Value(1, reflect.Kind(c.DataType))),
				B: uint16(c.Pix.Value(2, reflect.Kind(c.DataType))),
				A: 0xFFFF,
			}.RGBA()
		}
	case 4:
		switch reflect.Kind(c.DataType) {
		case reflect.Uint8:
			return color.RGBA{
				R: c.Pix[0],
				G: c.Pix[1],
				B: c.Pix[2],
				A: c.Pix[3],
			}.RGBA()
		case reflect.Uint16:
			return color.RGBA64{
				R: c.Pix.Uint16s()[0],
				G: c.Pix.Uint16s()[1],
				B: c.Pix.Uint16s()[2],
				A: c.Pix.Uint16s()[3],
			}.RGBA()
		default:
			return color.RGBA64{
				R: uint16(c.Pix.Value(0, reflect.Kind(c.DataType))),
				G: uint16(c.Pix.Value(1, reflect.Kind(c.DataType))),
				B: uint16(c.Pix.Value(2, reflect.Kind(c.DataType))),
				A: uint16(c.Pix.Value(3, reflect.Kind(c.DataType))),
			}.RGBA()
		}
	}
	return
}
Exemple #7
0
func colorModelConvert(channels int, dataType reflect.Kind, c color.Color) color.Color {
	c2 := MemPColor{
		Channels: channels,
		DataType: dataType,
		Pix:      make(PixSilce, channels*SizeofKind(dataType)),
	}

	if c1, ok := c.(MemPColor); ok {
		if c1.Channels == c2.Channels && c1.DataType == c2.DataType {
			copy(c2.Pix, c1.Pix)
			return c2
		}
		if c1.DataType == c2.DataType {
			copy(c2.Pix, c1.Pix)
			return c2
		}
		for i := 0; i < c1.Channels && i < c2.Channels; i++ {
			c2.Pix.SetValue(i, reflect.Kind(c2.DataType), c1.Pix.Value(i, reflect.Kind(c1.DataType)))
		}
		return c2
	}

	switch {
	case channels == 1 && reflect.Kind(dataType) == reflect.Uint8:
		v := color.GrayModel.Convert(c).(color.Gray)
		c2.Pix[0] = v.Y
		return c2
	case channels == 1 && reflect.Kind(dataType) == reflect.Uint16:
		v := color.Gray16Model.Convert(c).(color.Gray16)
		c2.Pix[0] = uint8(v.Y >> 8)
		c2.Pix[1] = uint8(v.Y)
		return c2
	case channels == 3 && reflect.Kind(dataType) == reflect.Uint8:
		r, g, b, _ := c.RGBA()
		c2.Pix[0] = uint8(r >> 8)
		c2.Pix[1] = uint8(g >> 8)
		c2.Pix[2] = uint8(b >> 8)
		return c2
	case channels == 3 && reflect.Kind(dataType) == reflect.Uint16:
		r, g, b, _ := c.RGBA()
		c2.Pix[0] = uint8(r >> 8)
		c2.Pix[1] = uint8(r)
		c2.Pix[2] = uint8(g >> 8)
		c2.Pix[3] = uint8(g)
		c2.Pix[4] = uint8(b >> 8)
		c2.Pix[5] = uint8(b)
		return c2
	case channels == 4 && reflect.Kind(dataType) == reflect.Uint8:
		r, g, b, a := c.RGBA()
		c2.Pix[0] = uint8(r >> 8)
		c2.Pix[1] = uint8(g >> 8)
		c2.Pix[2] = uint8(b >> 8)
		c2.Pix[3] = uint8(a >> 8)
		return c2
	case channels == 4 && reflect.Kind(dataType) == reflect.Uint16:
		r, g, b, a := c.RGBA()
		c2.Pix[0] = uint8(r >> 8)
		c2.Pix[1] = uint8(r)
		c2.Pix[2] = uint8(g >> 8)
		c2.Pix[3] = uint8(g)
		c2.Pix[4] = uint8(b >> 8)
		c2.Pix[5] = uint8(b)
		c2.Pix[6] = uint8(a >> 8)
		c2.Pix[7] = uint8(a)
		return c2
	}

	r, g, b, a := c.RGBA()
	rgba := []uint32{r, g, b, a}
	for i := 0; i < c2.Channels && i < len(rgba); i++ {
		c2.Pix.SetValue(i, reflect.Kind(c2.DataType), float64(rgba[i]))
	}
	return c2
}
Exemple #8
0
func createMapValue(ft *fieldType) interface{} {
	switch reflect.Kind(ft.Kind) {
	case reflect.Int8:
		var val int8
		return &val

	case reflect.Int16:
		var val int16
		return &val

	case reflect.Int32:
		var val int32
		return &val

	case reflect.Int64:
		var val int64
		return &val

	case reflect.Uint8:
		var val uint8
		return &val

	case reflect.Uint16:
		var val uint16
		return &val

	case reflect.Uint32:
		var val uint32
		return &val

	case reflect.Uint64:
		var val uint64
		return &val

	case reflect.Float32:
		var val float32
		return &val

	case reflect.Float64:
		var val float64
		return &val

	case reflect.Complex64:
		var val complex64
		return &val

	case reflect.Complex128:
		var val complex128
		return &val

	case reflect.Bool:
		var val bool
		return &val

	case reflect.String:
		var val string
		return &val

	case reflect.Slice:
		var val = make([]interface{}, 0)
		return &val

	case reflect.Map:
		var val = make(map[interface{}]interface{})
		return &val

	case reflect.Ptr:
		var subVal = createMapValue(ft.Elem[0])
		return subVal

	case STRUCT_REFERENCE:
		var val = make(map[string]interface{})
		return &val
	}

	panic(fmt.Sprintf("Can't create map value for %v\n", ft))
}
Exemple #9
0
func decodeFieldInner(field interface{}, ft *fieldType, structs structMap, reader *bufio.Reader) {

	switch reflect.Kind(ft.Kind) {
	case reflect.Int8,
		reflect.Int16,
		reflect.Int32,
		reflect.Int64,
		reflect.Uint8,
		reflect.Uint16,
		reflect.Uint32,
		reflect.Uint64,
		reflect.Float32,
		reflect.Float64,
		reflect.Complex64,
		reflect.Complex128:
		var err = binary.Read(reader, binary.BigEndian, field)
		if err != nil {
			panic(fmt.Sprintf("Fixed size decode error: %v\n", err))
		}

	case reflect.Bool:
		var byte = make([]byte, 1)
		n, err := reader.Read(byte)
		if n != 1 || err != nil {
			panic(fmt.Sprintf("Bool decode error: %v\n", err))
		}

		if byte[0] == 0 {
			*field.(*bool) = false
		} else if byte[0] == 1 {
			*field.(*bool) = true
		} else {
			panic(fmt.Sprintf("Bool byte neither 0 nor 1: %v", byte[0]))
		}

	case reflect.String:
		byteLen, err := binary.ReadUvarint(reader)
		if err != nil {
			panic(fmt.Sprintf("Couldn't read string length: %v\n", err))
		}

		var runes = make([]rune, 0, byteLen)
		var byteCount uint64 = 0

		for byteCount < byteLen {
			rune, n, err := reader.ReadRune()
			if err != nil {
				panic(fmt.Sprintf("Rune read error: %v\n", err))
			}
			runes = append(runes, rune)
			byteCount += uint64(n)
		}

		*field.(*string) = string(runes)

	case reflect.Slice:

		elemCount64, err := binary.ReadUvarint(reader)
		if err != nil {
			panic(fmt.Sprintf("Couldn't read slice length: %v\n", err))
		}
		var elemCount = int(elemCount64)

		resultv := reflect.ValueOf(field)
		slicev := resultv.Elem()
		elemt := slicev.Type().Elem()

		for i := 0; i < elemCount; i++ {
			slicev = slicev.Slice(0, i)

			var elemp reflect.Value
			if elemt.Kind() == reflect.Interface {
				elemp = reflect.ValueOf(createMapValue(ft.Elem[0]))
			} else {
				elemp = reflect.New(elemt)
			}

			decodeFieldInner(elemp.Interface(), ft.Elem[0], structs, reader)
			slicev = reflect.Append(slicev, elemp.Elem())
		}

		resultv.Elem().Set(slicev.Slice(0, elemCount))

	case reflect.Map:

		keyCount64, err := binary.ReadUvarint(reader)
		if err != nil {
			panic(fmt.Sprintf("Couldn't read key count: %v\n", err))
		}

		var keyCount = int(keyCount64)
		var resultv = reflect.ValueOf(field).Elem()

		if resultv.IsNil() {
			resultv.Set(reflect.MakeMap(resultv.Type()))
		}

		var keyt = resultv.Type().Key()
		var valt = resultv.Type().Elem()

		for i := 0; i < keyCount; i++ {
			var keyp = reflect.New(keyt)
			decodeFieldInner(keyp.Interface(), ft.Elem[0], structs, reader)
			var valp = reflect.New(valt)
			decodeFieldInner(valp.Interface(), ft.Elem[1], structs, reader)
			resultv.SetMapIndex(keyp.Elem(), valp.Elem())
		}

	case reflect.Ptr:
		c, err := reader.ReadByte()
		if err != nil {
			panic(fmt.Sprintf("Couldn't read ptr nil byte: %v\n", err))
		}

		if c != 0 {
			var val = reflect.ValueOf(field)
			var target = reflect.Indirect(val)

			if target.IsNil() {
				target.Set(reflect.New(target.Type().Elem()))
			}

			decodeFieldInner(target.Interface(), ft.Elem[0], structs, reader)
		}

	case IGNORED_FIELD:
		return

	case STRUCT_REFERENCE:

		var val = reflect.ValueOf(field)
		val = reflect.Indirect(val)

		var structFt = structs[ft.StructName]

		if val.Type().Kind() == reflect.Map {
			for _, fieldFt := range structFt.Elem {
				if reflect.Kind(fieldFt.Kind) == IGNORED_FIELD {
					continue
				}
				var key = fieldFt.Label
				var fieldVal = createMapValue(fieldFt)
				decodeFieldInner(fieldVal, fieldFt, structs, reader)
				if fieldVal == nil {
					val.SetMapIndex(reflect.ValueOf(key), reflect.ValueOf(nil))
				} else {
					val.SetMapIndex(reflect.ValueOf(key), reflect.ValueOf(fieldVal).Elem())
				}
			}
		} else {

			var valName = val.Type().PkgPath() + "/" + val.Type().Name()
			if valName != ft.StructName {
				panic(fmt.Sprintf("Incompatible structs: %s, %s", valName, ft.StructName))
			}

			for i, fieldFt := range structFt.Elem {
				if reflect.Kind(fieldFt.Kind) == IGNORED_FIELD {
					continue
				}
				var fieldVal = val.Field(i).Addr()
				decodeFieldInner(fieldVal.Interface(), fieldFt, structs, reader)
			}
		}

	default:
		panic(fmt.Sprintf("Unsupported decode kind %v\n", ft.Kind))
	}
}
Exemple #10
0
func encodeFieldInner(field interface{}, ft *fieldType, structs structMap, writer *bufio.Writer) {

	switch reflect.Kind(ft.Kind) {
	case reflect.Int8,
		reflect.Int16,
		reflect.Int32,
		reflect.Int64,
		reflect.Uint8,
		reflect.Uint16,
		reflect.Uint32,
		reflect.Uint64,
		reflect.Float32,
		reflect.Float64,
		reflect.Complex64,
		reflect.Complex128:
		encodeFixedSize(field, ft.Kind, writer)

	case reflect.Bool:
		var n int
		var err error
		if field.(bool) {
			n, err = writer.Write([]byte{1})
		} else {
			n, err = writer.Write([]byte{0})
		}

		if n != 1 || err != nil {
			panic(fmt.Sprintf("Bool encode error: %v\n", err))
		}

	case reflect.String:
		var str = field.(string)
		writeLength(len(str), writer)
		_, err := writer.WriteString(str)
		if err != nil {
			panic(fmt.Sprintf("String length encode error: %v\n", err))
		}

	case reflect.Slice:
		var val = reflect.ValueOf(field)
		var sliceLen = val.Len()
		writeLength(sliceLen, writer)
		for i := 0; i < sliceLen; i++ {
			encodeFieldInner(val.Index(i).Interface(), ft.Elem[0], structs, writer)
		}

	case reflect.Map:
		var val = reflect.ValueOf(field)
		var keyCount = val.Len()
		writeLength(keyCount, writer)
		var keys = val.MapKeys()
		for _, key := range keys {
			encodeFieldInner(key.Interface(), ft.Elem[0], structs, writer)
			var value = val.MapIndex(key)
			encodeFieldInner(value.Interface(), ft.Elem[1], structs, writer)
		}

	case reflect.Ptr:

		var valType = reflect.TypeOf(field)
		var val = reflect.ValueOf(field)

		if valType == nil || val.IsNil() {
			writer.Write([]byte{0})
		} else {
			writer.Write([]byte{1})
			if valType.Kind() == reflect.Ptr {
				val = val.Elem()
			}
			encodeFieldInner(val.Interface(), ft.Elem[0], structs, writer)
		}

	case IGNORED_FIELD:
		return

	case STRUCT_REFERENCE:
		var val = reflect.Indirect(reflect.ValueOf(field))

		var structFt = structs[ft.StructName]

		if val.Type().Kind() == reflect.Map {
			var mapVal = val.Interface().(map[string]interface{})
			for _, fieldFt := range structFt.Elem {
				if reflect.Kind(fieldFt.Kind) == IGNORED_FIELD {
					continue
				}
				var fieldVal = mapVal[fieldFt.Label]
				encodeFieldInner(fieldVal, fieldFt, structs, writer)
			}
		} else {

			var valName = val.Type().PkgPath() + "/" + val.Type().Name()
			if valName != ft.StructName {
				panic(fmt.Sprintf("Incompatible structs: %s, %s", valName, ft.StructName))
			}

			for i, fieldFt := range structFt.Elem {
				// Unexported fields aren't accessible, so we need to
				// check this here so they can at least be ignored
				if reflect.Kind(fieldFt.Kind) == IGNORED_FIELD {
					continue
				}
				encodeFieldInner(val.Field(i).Interface(), fieldFt, structs, writer)
			}
		}

	default:
		panic(fmt.Sprintf("Unsupported encode kind %v\n", ft.Kind))
	}
}
Exemple #11
0
func (tm *TypeMap) basicRuntimeType(b *types.Basic) (global, ptr llvm.Value) {
	commonType := tm.makeCommonType(b, reflect.Kind(b.Kind))
	return tm.makeRuntimeTypeGlobal(commonType)
}
Exemple #12
0
func (k BasicTypeKind) String() string {
	return reflect.Kind(k).String()
}
Exemple #13
0
func (k Kind) String() string {
	return reflect.Kind(k).String()
}