lang: funcs: Add nitpicks from funcgen

Discussed nitpicks with roidelapluie to clean up slightly for
consistency.
This commit is contained in:
James Shubin
2019-10-30 08:44:03 -04:00
parent 1685ee1ecb
commit 631124e658
6 changed files with 14 additions and 18 deletions

View File

@@ -1,2 +1 @@
generated_funcs.go generated_funcs.go
generated_funcs_test.go

View File

@@ -5,15 +5,12 @@ packages:
- name: math - name: math
- name: math/rand - name: math/rand
alias: rand alias: rand
mgmtAlias: rand
- name: os - name: os
- name: os/exec - name: os/exec
alias: exec alias: exec
mgmtAlias: exec
- name: path - name: path
- name: path/filepath - name: path/filepath
alias: filepath alias: filepath
mgmtAlias: filepath
- name: runtime - name: runtime
- name: strconv - name: strconv
- name: strings - name: strings

View File

@@ -59,7 +59,7 @@ func (obj *arg) ToMcl() (string, error) {
return fmt.Sprintf("%s%s", prefix, types.TypeBool.String()), nil return fmt.Sprintf("%s%s", prefix, types.TypeBool.String()), nil
case "string": case "string":
return fmt.Sprintf("%s%s", prefix, types.TypeStr.String()), nil return fmt.Sprintf("%s%s", prefix, types.TypeStr.String()), nil
case "int64", "int": case "int", "int64":
return fmt.Sprintf("%s%s", prefix, types.TypeInt.String()), nil return fmt.Sprintf("%s%s", prefix, types.TypeInt.String()), nil
case "float64": case "float64":
return fmt.Sprintf("%s%s", prefix, types.TypeFloat.String()), nil return fmt.Sprintf("%s%s", prefix, types.TypeFloat.String()), nil

View File

@@ -1,5 +1,5 @@
- mgmtPackage: golang/testpkg - mgmtPackage: golang/testpkg
mgmtName: all_kind mclName: all_kind
internalName: TestpkgAllKind internalName: TestpkgAllKind
golangPackage: &pkg golangPackage: &pkg
name: testpkg name: testpkg
@@ -9,7 +9,7 @@
args: [{name: x, type: int64},{name: y, type: string}] args: [{name: x, type: int64},{name: y, type: string}]
return: [{type: float64}] return: [{type: float64}]
- mgmtPackage: golang/testpkg - mgmtPackage: golang/testpkg
mgmtName: to_upper mclName: to_upper
internalName: TestpkgToUpper internalName: TestpkgToUpper
golangPackage: *pkg golangPackage: *pkg
golangFunc: ToUpper golangFunc: ToUpper
@@ -17,7 +17,7 @@
args: [{name: s, type: string}] args: [{name: s, type: string}]
return: [{type: string}] return: [{type: string}]
- mgmtPackage: golang/testpkg - mgmtPackage: golang/testpkg
mgmtName: max mclName: max
internalName: TestpkgMax internalName: TestpkgMax
golangPackage: *pkg golangPackage: *pkg
golangFunc: Max golangFunc: Max
@@ -25,7 +25,7 @@
args: [{name: x, type: float64},{name: y, type: float64}] args: [{name: x, type: float64},{name: y, type: float64}]
return: [{type: float64}] return: [{type: float64}]
- mgmtPackage: golang/testpkg - mgmtPackage: golang/testpkg
mgmtName: with_error mclName: with_error
internalName: TestpkgWithError internalName: TestpkgWithError
golangPackage: *pkg golangPackage: *pkg
golangFunc: WithError golangFunc: WithError
@@ -33,7 +33,7 @@
args: [{name: s, type: string}] args: [{name: s, type: string}]
return: [{type: string}] return: [{type: string}]
- mgmtPackage: golang/testpkg - mgmtPackage: golang/testpkg
mgmtName: with_int mclName: with_int
internalName: TestpkgWithInt internalName: TestpkgWithInt
golangPackage: *pkg golangPackage: *pkg
golangFunc: WithInt golangFunc: WithInt

View File

@@ -31,7 +31,7 @@ type function struct {
// MclName is the name of the package of the function in mcl. // MclName is the name of the package of the function in mcl.
MgmtPackage string `yaml:"mgmtPackage"` MgmtPackage string `yaml:"mgmtPackage"`
// MclName is the name of the function in mcl. // MclName is the name of the function in mcl.
MclName string `yaml:"mgmtName"` MclName string `yaml:"mclName"`
// InternalName is the name used inside the templated file. // InternalName is the name used inside the templated file.
// Used to avoid clash between same functions from different packages. // Used to avoid clash between same functions from different packages.
InternalName string `yaml:"internalName"` InternalName string `yaml:"internalName"`

View File

@@ -33,14 +33,14 @@ import (
) )
var ( var (
validSignature = regexp.MustCompile(`^func (?P<name>[A-Z][a-zA-Z0-9]+)\((?P<args>([a-zA-Z]+( (string|bool|float64|int64|int))?(, )?){0,})\) (?P<return>(string|float64|int64|bool|int)|\((string|float64|int64|bool|int), error\))$`) validSignature = regexp.MustCompile(`^func (?P<name>[A-Z][a-zA-Z0-9]+)\((?P<args>([a-zA-Z]+( (bool|string|int|int64|float64))?(, )?){0,})\) (?P<return>(bool|string|int|int64|float64|)|\((bool|string|int|int64|float64), error\))$`)
errExcluded = errors.New("function is excluded") errExcluded = errors.New("function is excluded")
) )
type golangPackages []*golangPackage type golangPackages []*golangPackage
type golangPackage struct { type golangPackage struct {
// Name is the name of the go package. // Name is the name of the golang package.
Name string `yaml:"name"` Name string `yaml:"name"`
// Alias is the alias of the package when imported in golang. // Alias is the alias of the package when imported in golang.
// e.g. import rand "os.rand" // e.g. import rand "os.rand"
@@ -142,11 +142,11 @@ func (obj *golangPackage) parseFunctionLine(line string, getHelp bool) (*functio
return nil, errExcluded return nil, errExcluded
} }
mgmtpackage := obj.Name mgmtPackage := obj.Name
if obj.MgmtAlias != "" { if obj.MgmtAlias != "" {
mgmtpackage = obj.MgmtAlias mgmtPackage = obj.MgmtAlias
} }
mgmtpackage = fmt.Sprintf("golang/%s", mgmtpackage) mgmtPackage = fmt.Sprintf("golang/%s", mgmtPackage)
internalName := fmt.Sprintf("%s%s", strcase.ToCamel(strings.Replace(obj.Name, "/", "", -1)), name) internalName := fmt.Sprintf("%s%s", strcase.ToCamel(strings.Replace(obj.Name, "/", "", -1)), name)
internalName = strings.Replace(internalName, "Html", "HTML", -1) internalName = strings.Replace(internalName, "Html", "HTML", -1)
@@ -159,7 +159,7 @@ func (obj *golangPackage) parseFunctionLine(line string, getHelp bool) (*functio
} }
return &function{ return &function{
MgmtPackage: mgmtpackage, MgmtPackage: mgmtPackage,
MclName: strcase.ToSnake(name), MclName: strcase.ToSnake(name),
InternalName: internalName, InternalName: internalName,
Help: help, Help: help,
@@ -205,7 +205,7 @@ func parseArgs(str string) []arg {
func parseReturn(str string) []arg { func parseReturn(str string) []arg {
var returns []arg var returns []arg
re := regexp.MustCompile(`(int64|float64|string|bool|int)`) re := regexp.MustCompile(`(bool|string|int|int64|float64)`)
t := string(re.Find([]byte(str))) t := string(re.Find([]byte(str)))
returns = append(returns, arg{Type: t}) returns = append(returns, arg{Type: t})
return returns return returns