forbid unexported input/output name

This commit is contained in:
Adrien Marquès 2020-03-29 19:13:07 +02:00
parent 4877d0ea23
commit 66985dfbd0
Signed by: xdrm-brackets
GPG Key ID: D75243CA236D825E
2 changed files with 19 additions and 1 deletions

View File

@ -29,6 +29,9 @@ const ErrMissingRequestArgument = cerr("handler first argument must be of type a
// ErrMissingParamArgument - missing parameters argument for handler
const ErrMissingParamArgument = cerr("handler second argument must be a struct")
// ErrUnexportedParamName - argument is unexported in struct
const ErrUnexportedName = cerr("unexported name")
// ErrMissingParamOutput - missing output argument for handler
const ErrMissingParamOutput = cerr("handler first output must be a *struct")

View File

@ -3,6 +3,7 @@ package dynamic
import (
"fmt"
"reflect"
"strings"
"git.xdrm.io/go/aicra/api"
"git.xdrm.io/go/aicra/internal/config"
@ -50,8 +51,15 @@ func (s spec) checkInput(fnv reflect.Value) error {
return ErrMissingParamArgument
}
// check for invlaid param
// check for invalid param
for name, ptype := range s.Input {
if len(name) < 1 {
continue
}
if name[0] == strings.ToLower(name)[0] {
return fmt.Errorf("%s: %w", name, ErrUnexportedName)
}
field, exists := structArg.FieldByName(name)
if !exists {
return fmt.Errorf("%s: %w", name, ErrMissingParamFromConfig)
@ -100,6 +108,13 @@ func (s spec) checkOutput(fnv reflect.Value) error {
// fail on invalid output
for name, ptype := range s.Output {
if len(name) < 1 {
continue
}
if name[0] == strings.ToLower(name)[0] {
return fmt.Errorf("%s: %w", name, ErrUnexportedName)
}
field, exists := structOutput.FieldByName(name)
if !exists {
return fmt.Errorf("%s: %w", name, ErrMissingOutputFromConfig)