-
-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wrap reflect.Type.ConvertibleTo calls with Go/TinyGo implementations
Enable package to be usable with [TinyGo](https://tinygo.org). Changes calls to reflect.Type.ConvertibleTo in decode.go to Go/TinyGo specific implementations. Basic examples work, but (some) unit tests fail as conversions to `interface{}` do not currently work in TinyGo. Otherwise, all other functionality I've tested now appears to work properly: highlighted error output, `cmd/ycat`, the README.md examples, etc. As an example, both Go and TinyGo are now able to use this package vis-a-vis this example taken from README.md: ```go package main import ( "fmt" "strings" "github.com/goccy/go-yaml" ) func main() { yml := ` store: book: - author: john price: 10 - author: ken price: 12 bicycle: color: red price: 19.95 ` path, err := yaml.PathString("$.store.book[*].author") if err != nil { //... } var authors []string if err := path.Read(strings.NewReader(yml), &authors); err != nil { //... } fmt.Println(authors) } ``` Output: ```sh ken@ken-desktop:~/g$ go run main.go [john ken] ken@ken-desktop:~/g$ tinygo run main.go [john ken] ken@ken-desktop:~/g$ ``` (previously the `tinygo run` would panic)
- Loading branch information
Showing
3 changed files
with
80 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
//go:build !tinygo | ||
|
||
package yaml | ||
|
||
import ( | ||
"reflect" | ||
) | ||
|
||
func convertibleTo(src reflect.Value, typ reflect.Type) bool { | ||
return src.Type().ConvertibleTo(typ) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//go:build tinygo | ||
|
||
package yaml | ||
|
||
import ( | ||
"reflect" | ||
) | ||
|
||
func convertibleTo(src reflect.Value, typ reflect.Type) bool { | ||
srck, typk := src.Kind(), typ.Kind() | ||
if srck == typk { | ||
return true | ||
} | ||
switch srck { | ||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: | ||
switch typk { | ||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: | ||
return true | ||
case reflect.Float32, reflect.Float64: | ||
return true | ||
case reflect.String: | ||
return true | ||
} | ||
|
||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: | ||
switch typk { | ||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: | ||
return true | ||
case reflect.Float32, reflect.Float64: | ||
return true | ||
case reflect.String: | ||
return true | ||
} | ||
|
||
case reflect.Float32, reflect.Float64: | ||
switch typk { | ||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: | ||
return true | ||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: | ||
return true | ||
case reflect.Float32, reflect.Float64: | ||
return true | ||
} | ||
|
||
case reflect.Slice: | ||
if typk == reflect.String /*&& !src.Type().Elem().isNamed()*/ { | ||
switch src.Type().Elem().Kind() { | ||
case reflect.Uint8, reflect.Int32: | ||
return true | ||
} | ||
} | ||
|
||
case reflect.String: | ||
switch typk { | ||
case reflect.Slice: | ||
switch typ.Elem().Kind() { | ||
case reflect.Uint8, reflect.Int32: | ||
return true | ||
} | ||
return false | ||
} | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters