Giter Club home page Giter Club logo

Comments (10)

gentee avatar gentee commented on May 31, 2024 1

Well. I will try to realize something like that this week.

from gentee.

gentee avatar gentee commented on May 31, 2024

Yes, I think it should work.
But goMap must have the items of the same types.
For example,
int64 -> int gentee type
string -> str gentee type
float64 -> float gentee type
etc.
Otherwise, Gentee wouldn't be able to work with the converted map.

from gentee.

lsascha avatar lsascha commented on May 31, 2024

thanks.
Yes i tested that function and it works in my case.

My Question was more like if there is any already included easier way to convert between gentee and golang types.
Just that i was not able to find it with looking through the code or looking into the documentation.

If not, i guess it would be a nice feature suggestion?

But thanks anyway.
And thanks for making this nicely working scripting language for go.

from gentee.

gentee avatar gentee commented on May 31, 2024

What functions would you like to suggest?
go map -> gentee map
go slice -> gentee arr
go struct -> gentee struct ?
In this case, converting function must know the result Gentee type.
For example,
If we convert go map to gentee map the function must looks like
Prototype:
GoMap2Map(in map[string]interface{}, result string) (*core.Map, error)
Example:
result, err := GoMap2Map(mymap, "map.int")
result, err := GoMap2Map(mymap, "map.arr.mytype")
It will make the safe conversion or throw an error if the go source parameter is not compatible with the result type.

from gentee.

lsascha avatar lsascha commented on May 31, 2024

yes. i think that would be a solution.

For example i execute a gentee script and wanted to process the resulting string array further in golang.

I ended up making this methods:

func GenteeTypeToGolangInterface(genteeInterface interface{}) interface{} {
	var returnArray interface{}
	switch v := genteeInterface.(type) {
	case *core.Array:
		returnArray = v.Data
	}
	return returnArray
}
func InterfaceToStringArr(golangInterface interface{}) []string {
	tmpInterface := golangInterface.([]interface{})
	s := make([]string, len(tmpInterface))
	for i, v := range tmpInterface {
		s[i] = v.(string)
	}
	return s
}
// ...
goArray := InterfaceToStringArr(GenteeTypeToGolangInterface(scriptReturn))

and there are many more cases where i had wished it would be easy to just convert from one type to another.

at least the function to convert the gentee bool to the golang boolean was relatively simple.

func GenteeTypeInt64ToBool(integer int64) bool {
	if integer >= 1 {
		return true
	}
	return false
}

i guess the names should be different, or make a more general function that can convert between all types like you suggested like

result, err := GoType2GenteeType(goType, "arr.str")

and

result, err := GenteeType2GoType(genteeType)
/* (do we really need to tell it the target type
 or can we convert it automatically based on the provided variable?) */

from gentee.

gentee avatar gentee commented on May 31, 2024

I have just add Go@GenteeType & Gentee2GoType functions in master branch.
Documentation is available on https://docs.gentee.org/golang/reference
Test examples can be found in tests/custom_test.go
You can define your own conversion fucntions by using these two ones.

from gentee.

lsascha avatar lsascha commented on May 31, 2024

Sorry for the silence.
Just got to test it, and seems to work perfectly fine. :) Thanks.

from gentee.

lsascha avatar lsascha commented on May 31, 2024

Sorry. i think i was a bit too early to close it.

I am getting this error when i try to convert a arr.str to a golang string array.
so this line goArray := Gentee2GoType(genteeArray).([]string)
results in this error at runtime.
panic: interface conversion: interface {} is []interface {}, not []string

from gentee.

gentee avatar gentee commented on May 31, 2024

By default, any arr is converted to []interface{} and any map to map[string]interface{}. I have done this because it is impossible to predefine all possible cases. So, you can define your own function

arr := Gentee2GoType(genteeArray).([]interface{})
ret := make([]string, len(arr))
for i, item := range  arr {
    ret[i] = item.(string)
}
return ret

from gentee.

lsascha avatar lsascha commented on May 31, 2024

ah. now i understand the table in the documentation. 🙈
i was wondering about the Result type (vtype) column.

Sorry about that. Now it works. :)

from gentee.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.