Commit 8419ba6c authored by Oscar Campos's avatar Oscar Campos Committed by Oscar Campos
Browse files

fix: fix linting errors and rename gdnativego compiler into gogdc

parent 0f2165e1
......@@ -4,6 +4,7 @@ package gdnative
import (
"encoding/json"
"errors"
"io/ioutil"
"log"
"os"
......@@ -21,30 +22,21 @@ type View struct {
// not the given element is the last in the slice or not. This is so we can
// correctly insert commas for argument lists.
func (v View) NotLastElement(n int, slice [][]string) bool {
if n != (len(slice) - 1) {
return true
}
return false
return n != (len(slice) - 1)
}
// NotVoid checks to see if the return string is void or not. This is used inside
// our template so we can determine if we need to use the `return` keyword in
// the function body.
func (v View) NotVoid(ret string) bool {
if ret != "void" {
return true
}
return false
return ret != "void"
}
// HasArgs is a function we use inside the template to test whether or not the
// function has arguments. This is so we can determine if we need to place a
// comma.
func (v View) HasArgs(args [][]string) bool {
if len(args) != 0 {
return true
}
return false
return len(args) != 0
}
// Generate generates the bindings from the JSON definition
......@@ -114,7 +106,9 @@ func Parse(packagePath string) APIs {
// Unmarshal the JSON into our struct.
var apis APIs
json.Unmarshal(body, &apis)
if err := json.Unmarshal(body, &apis); err != nil {
panic(errors.New("could not unmarshal Godot JSON API"))
}
return apis
}
......@@ -129,10 +123,10 @@ func WriteTemplate(templatePath, outputPath string, view View) {
// Open the output file for writing
f, err := os.Create(outputPath)
defer f.Close()
if err != nil {
panic(err)
}
defer f.Close()
// Write the template with the given view.
err = t.Execute(f, view)
......
......@@ -36,19 +36,13 @@ func (v View) Debug(itm string) string {
// IsValidProperty will determine if we should be generating the given property
// in our Go structure.
func (v View) IsValidProperty(prop TypeDef) bool {
if strings.Contains(prop.Name, "_touch_that") {
return false
}
return true
return strings.Contains(prop.Name, "_touch_that")
}
// IsGodotBaseType will check to see if the given simple type definition is defining
// a built-in C type or a Godot type.
func (v View) IsGodotBaseType(typeDef TypeDef) bool {
if strings.Contains(typeDef.Base, "godot_") {
return true
}
return false
return strings.Contains(typeDef.Base, "godot_")
}
// ToGoBaseType will convert a base type name to the correct Go base type.
......@@ -82,18 +76,12 @@ func (v View) ToGoReturnType(str string) string {
// HasReturn returns true if the given string is void
func (v View) HasReturn(str string) bool {
if str == "void" || str == "Void" || strings.Contains(str, "void") {
return false
}
return true
return str == "void" || str == "Void" || strings.Contains(str, "void")
}
// HasPointerReturn returns true if the given string contains an indirection operator
func (v View) HasPointerReturn(str string) bool {
if strings.Contains(str, "*") {
return true
}
return false
return strings.Contains(str, "*")
}
// IsVoidPointerType returns true if the given string matches godot object void types
......@@ -107,19 +95,13 @@ func (v View) IsVoidPointerType(str string) bool {
// IsWcharT returns true if the given strig contains wchar_t type
func (v View) IsWcharT(str string) bool {
if strings.Contains(str, "wchar_t") {
return true
}
return false
return strings.Contains(str, "wchar_t")
}
// IsDoublePointer returns true if the given string contains two indirection
// operators one beside another
func (v View) IsDoublePointer(str string) bool {
if strings.Contains(str, "**") {
return true
}
return false
return strings.Contains(str, "**")
}
// ToGoArgType converts arguments types to Go valid types
......@@ -244,18 +226,12 @@ func (v View) MethodsList(typeDef TypeDef) []Method {
// MethodIsConstructor returns true if the given method contains the `_new` sub string
func (v View) MethodIsConstructor(method Method) bool {
if strings.Contains(method.Name, "_new") {
return true
}
return false
return strings.Contains(method.Name, "_new")
}
// NotSelfArg return false if the given string contains any reference to self or p_self
func (v View) NotSelfArg(str string) bool {
if str == "self" || str == "p_self" {
return false
}
return true
return str == "self" || str == "p_self"
}
// StripPointer strips the indirection operator from a given string
......@@ -363,7 +339,8 @@ func Generate() {
// Organize the type definitions by header name
for _, typeDef := range allTypeDefinitions {
if _, ok := defMap[typeDef.HeaderName]; ok {
_, ok := defMap[typeDef.HeaderName]
if ok {
defMap[typeDef.HeaderName] = append(defMap[typeDef.HeaderName], typeDef)
} else {
defMap[typeDef.HeaderName] = []TypeDef{typeDef}
......@@ -411,7 +388,7 @@ func Generate() {
// Run gofmt on the generated Go file.
log.Println(" Running gofmt on output:", outFileName+"...")
if noGoImport == false {
if !noGoImport {
GoFmt(packagePath + "/gdnative/" + outFileName)
}
......@@ -437,10 +414,10 @@ func WriteTemplate(templatePath, outputPath string, view View) {
// Open the output file for writing
f, err := os.Create(outputPath)
defer f.Close()
if err != nil {
panic(err)
}
defer f.Close()
// Write the template with the given view.
err = t.Execute(f, view)
......
......@@ -7,8 +7,8 @@ type TypeDef struct {
Comment string // Contains the comment on the line of the struct
GoName string // The Go type name in camelCase
HeaderName string // The header file this type shows up in
IsPointer bool // Usually for properties; defines if it is a pointer type
Name string // The C type name in snake_case
Properties []TypeDef // Optional C struct fields
IsPointer bool // Usually for properties; defines if it is a pointer type
SimpleType bool // Whether or not the definition is just one line long (e.g. bool, int, etc.)
}
......@@ -24,19 +24,17 @@ type context struct {
Verbose bool
}
type generateCmd struct {
path string
}
type generateCmd struct{}
type listCmd struct{}
// cli defines our command line structure using Kong
var cli struct {
Generate generateCmd `cmd help:"Generates autotoregistration boilerplate Go code for user defined structures"` //nolint:govet
List listCmd `cmd help:"List user defined autoregistrable data structures"` //nolint:govet
Path string `type:"path" default:"." help:"Path where execute the command"`
Verbose bool `help:"Verbose output"`
Generate generateCmd `cmd help:"Generates autotoregistration boilerplate Go code for user defined structures"`
List listCmd `cmd help:"List user defined autoregistrable data structures"`
}
func main() {
......
......@@ -211,7 +211,6 @@ func validateConstructor(structName string, fd *ast.FuncDecl) (*registryConstruc
structName, structName, funcName, t.X.(*ast.Ident).Name,
)
}
break
default:
return nil, fmt.Errorf("constructors of %s values must return a pointer to *%s but %s returns %v", structName, structName, funcName, t)
}
......@@ -361,7 +360,7 @@ func lookupMethods(className string, file *ast.File) []*registryMethod {
// lookupSignals look up for every signal that is owned by the type and fill
// a registration data structure with it
func lookupSignals(className string, file *ast.File) []*registrySignal {
func lookupSignals(className string, file ast.Node) []*registrySignal {
signals := []*registrySignal{}
ast.Inspect(file, func(node ast.Node) (cont bool) {
......@@ -693,7 +692,7 @@ func parseMap(field *ast.MapType) string {
return fmt.Sprintf(result, key, value)
}
func parseKeyValueExpr(expr *ast.KeyValueExpr) (string, string) {
func parseKeyValueExpr(expr *ast.KeyValueExpr) (string, string) { //nolint:unused
var value string
key := expr.Key.(*ast.Ident).Name
......
......@@ -252,7 +252,7 @@ func (i Int64T) getBase() C.int64_t {
// SignedChar is a Godot C schar wrapper
type SignedChar int8
func (s SignedChar) getBase() *C.schar {
func (s SignedChar) getBase() *C.schar { //nolint:unused
intVal := int8(s)
return (*C.schar)(unsafe.Pointer(&intVal))
}
......@@ -260,7 +260,7 @@ func (s SignedChar) getBase() *C.schar {
// Uint is a Godot C uint wrapper
type Uint uint
func (u Uint) getBase() C.uint {
func (u Uint) getBase() C.uint { //nolint:unused
return C.uint(u)
}
......@@ -274,7 +274,7 @@ func (u Uint8T) getBase() C.uint8_t {
// Uint32T is a Godot C uint32_t wrapper
type Uint32T uint32
func (u Uint32T) getBase() C.uint32_t {
func (u Uint32T) getBase() C.uint32_t { //nolint:unused
return C.uint32_t(u)
}
......@@ -294,18 +294,6 @@ func newWcharT(str *C.wchar_t) WcharT {
return WcharT(goStr)
}
// newWcharTWithLength will convert the given C.wchar_t into a Go string
func newWcharTWithLength(str *C.wchar_t, length int) WcharT {
goStr, err := wchar.WcharStringPtrNToGoString(unsafe.Pointer(str), length)
if err != nil {
log.Println("Error converting wchar_t to Go string:", err)
}
if len(goStr) != length {
goStr = truncateString(goStr, length)
}
return WcharT(goStr)
}
// WcharT is a Godot C wchar_t wrapper
type WcharT string
......
......@@ -435,9 +435,7 @@ var nativeScriptInit = []func(){}
// This is used so you can define a function that will run to register all of the
// classes that you want exposed to Godot.
func SetNativeScriptInit(initFunc ...func()) {
for _, init := range initFunc {
nativeScriptInit = append(nativeScriptInit, init)
}
nativeScriptInit = append(nativeScriptInit, initFunc...)
}
/*------------------------------------------------------------------------------
......
......@@ -105,7 +105,9 @@ func (c *Class) register() {
// then iterate over any defined property and register them
for _, property := range c.properties {
property.register()
if err := property.register(); err != nil {
panic(fmt.Errorf("could not register class properties: %w", err))
}
}
// finally iterate over any defined signal and register them
......@@ -280,7 +282,7 @@ func NewGodotProperty(className, name, hint, hintString, usage, rset string,
if attributes.RsetType, ok = MethodRpcModeLookupMap[rsetType]; !ok {
var validTypes string
for key, _ := range MethodRpcModeLookupMap {
for key := range MethodRpcModeLookupMap {
validTypes = fmt.Sprintf("%s %s", validTypes, strings.Replace(key, "MethodRpcMode", "", 1))
}
panic(fmt.Sprintf("unknown rset %q, allowed types: %s", rset, validTypes))
......@@ -307,7 +309,7 @@ func (p *Property) register() error {
// if set and get functions are not defined generate generic ones
if p.setFunc == nil {
p.setFunc = p.createGenericSetter()
p.setFunc = p.CreateGenericSetter()
}
if p.setFunc == nil || p.getFunc == nil {
return fmt.Errorf("you can not register a property that does not defines both setter and getter functions")
......@@ -318,7 +320,7 @@ func (p *Property) register() error {
}
// creates a generic setter method to set property values if none is provided
func (p *Property) createGenericSetter() *InstancePropertySet {
func (p *Property) CreateGenericSetter() *InstancePropertySet {
propertySetter := func(object Object, classProperty, instanceString string, property Variant) {
Log.Println(fmt.Sprintf("Creating Go generic property setter for %s.%s", p.name, p.propertyName))
......@@ -334,7 +336,7 @@ func (p *Property) createGenericSetter() *InstancePropertySet {
}
// created a generic getter method to get property values if none is provided
func (p *Property) createGenericGetter() *InstancePropertyGet {
func (p *Property) CreateGenericGetter() *InstancePropertyGet {
propertyGetter := func(object Object, classProperty, instanceString string) Variant {
log.Println(fmt.Sprintf("Creating Go generic property getter for %s.%s", p.name, p.propertyName))
......
......@@ -15,7 +15,3 @@ func NewStringWithWideString(str string) String {
func NewString() String {
return ""
}
func truncateString(str string, num int) string {
return str[0:num]
}
......@@ -22,7 +22,7 @@ func unsafeToGoString(p unsafe.Pointer) string {
// Attribution:
// Author: https://github.com/mantenie
// Source: https://github.com/serenize/snaker
func camelToSnake(s string) string {
func camelToSnake(s string) string { //nolint:deadcode,unused
var result string
var words []string
var lastPos int
......@@ -60,7 +60,7 @@ func camelToSnake(s string) string {
}
// startsWithInitialism returns the initialism if the given string begins with it
func startsWithInitialism(s string) string {
func startsWithInitialism(s string) string { //nolint:unused
var initialism string
// the longest initialism is 5 char, the shortest 2
for i := 1; i <= 5; i++ {
......@@ -73,7 +73,7 @@ func startsWithInitialism(s string) string {
// commonInitialisms, taken from
// https://github.com/golang/lint/blob/206c0f020eba0f7fbcfbc467a5eb808037df2ed6/lint.go#L731
var commonInitialisms = map[string]bool{
var commonInitialisms = map[string]bool{ //nolint:unused
"ACL": true,
"API": true,
"ASCII": true,
......
......@@ -24,7 +24,6 @@ func (gdt *Variant) GetType() VariantType {
// VariantArray is a wrapper around Godot C **godot_variant
type VariantArray struct {
base **C.godot_variant
array []Variant
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment