Comments (1)
I think it's a bit difficult to implement the same syntax as Java :), but we can use the golang style to nest the interface.
In your example, you can use existed interface defined in other packages
(The following code will work on genny df3d48a)
package genny
import (
"bytes"
"fmt"
)
type Something interface {
fmt.Stringer
}
type SomethingQueue struct {
items []Something
}
...
func (q *SomethingQueue) String() string {
var buf bytes.Buffer
for _, item := range q.items {
buf.WriteString(item.String())
}
return buf.String()
}
It's also easy to extend other interfaces, like json.Marshaler
. We can also nest with generic.Type
to indicate the interface is generic
type Something interface {
generic.Type
json.Marshaler
fmt.Stringer
}
We can do this because genney
will replace the string identifier of the file, regardless of its actual type. In this way, we can extend more complex types while being type-safe.
I have summarized several common usages used in my toy project.
- Type
Object
extends to struct (has fields)
type Object struct {
generic.Type
name string
age int
}
// Now you can use it in function without compile errors
func doSomeThing(o *Object) (string, int) {
...
return o.name, o.age
}
- Type
Object
extends to interface (has method)
type Object interface {
generic.Type // can be removed
run() Object
stop() Object
}
// use it as normal interface
func operate(o Object) {
o.run().stop()
}
- Mock package
// file1: demo.go
type mockPackage struct {
// package function
Log(...interface{}) error
// package exported variable
ColorRed int
ColorGreen int
// Not find a way to mock exported Type
}
var PackageName mockPackage
// file2: demo_template.go
// Use in another file
func normalFunc() {
PackageName.Log("Hello", "Good")
color := PackageName.ColorGreen
...
}
// use command
// geeny -in=demo_template.go -out=gen_demo.go gen "PackageName=mypkg"
Hope to be helpful :)
from genny.
Related Issues (20)
- Imports not generated correctly HOT 3
- Separate generated code in multiple files HOT 3
- Flags usage not correct in README HOT 1
- Suggestion: make the genny project visible to Hacktoberfest searches. Add a contributing.md file too.
- Is this project still maintained? HOT 2
- wordify breaks calls to APIs made while generating
- -pkg doesn't appear to work HOT 2
- code cleanup in parse.go drops all import statements and is potentially buggy in other infrequent scenarios HOT 1
- Lowercase replace in strings? HOT 6
- Collect generics into composed interface HOT 2
- Switch to a standard codegen comment marker
- Are slices supported? HOT 1
- Allow generation of exported and non exported types from the same generic code.
- TestCustomTypesMap test fails on some systems
- running "genny": exec: "genny": executable file not found in $PATH HOT 3
- Check if specific type name has a valid syntax
- Drops imports on github actions HOT 1
- import stmt removed in output (sometimes) HOT 2
- Please create a new tag for the latest master branch
- generic.Type support function definition ?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from genny.