Add support for byte type#90
Conversation
|
Also note byte slices are still treated as strings, from here: But uint8 slices are converted to number[]. Is this the intended behavior? |
|
Thank you, I generated a script using a LLM to see what the package main
import (
"encoding/json"
"fmt"
)
type DataHolder struct {
SingleUint8 uint8 `json:"single_uint8"`
SliceOfUint8 []uint8 `json:"slice_of_uint8"`
SingleByte byte `json:"single_byte"`
SliceOfByte []byte `json:"slice_of_byte"`
}
func main() {
// 1. Create an instance of the struct with sample data.
data := DataHolder{
SingleUint8: 123,
SliceOfUint8: []uint8{10, 20, 30, 255},
SingleByte: 'A', // 'A' is 65 in ASCII
SliceOfByte: []byte("Hello, JSON!"),
}
// 2. Marshal the struct into JSON. We use MarshalIndent for pretty-printing.
// The standard json.Marshal() would produce a single line of output.
jsonData, err := json.MarshalIndent(data, "", " ")
if err != nil {
fmt.Println("Error marshalling JSON:", err)
return
}
// 3. Print the resulting JSON data.
fmt.Println("JSON Encoded Data:")
fmt.Println(string(jsonData))
}So I imagine that |
|
It's a good idea to follow the json typing. I made a change to handle both byte and uint8 slices as strings, with a Typescript comment to the original Go type. type DataHolder struct {
SingleUint8 uint8 `json:"single_uint8"`
SliceOfUint8 []uint8 `json:"slice_of_uint8"`
SingleByte byte `json:"single_byte"`
SliceOfByte []byte `json:"slice_of_byte"`
}export interface DataHolder {
single_uint8: number /* uint8 */;
slice_of_uint8: string /* []uint8 */;
single_byte: number /* byte */;
slice_of_byte: string /* []byte */;
} |
|
I don't think it's right to treat
data := []byte("hello") // meaningful: text / bytes
nums := []uint8{1, 2, 3} // meaningful: numbersIn my source code, I have a slice of |
|
Hmm.. You should be able to use the I fear there may not be one size fits all :( |
|
Yes, However, I just wanted to make aware that, from a conceptional standpoint, |
In Go, byte is an alias for uint8. However when tygo processes a struct with a field of type byte it does not treat it the same way as a struct with a field of type uint8. This pull request allows tygo to treat Go byte fields as a TS number in the same way as uint8 fields.
For example, currently this Go struct converts to the following TS interface:
After this change the same Go struct converts to: