Skip to content

Commit

Permalink
Add validator
Browse files Browse the repository at this point in the history
Signed-off-by: André R. de Miranda <[email protected]>
  • Loading branch information
ribeiromiranda committed Jun 8, 2023
1 parent 9e4f13f commit 5f830a8
Show file tree
Hide file tree
Showing 10 changed files with 346 additions and 85 deletions.
2 changes: 1 addition & 1 deletion cmd/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
func main() {
err := cmd.Rest()
if err != nil {
fmt.Println(err)
fmt.Print(err)
os.Exit(1)
}
os.Exit(0)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
cuelang.org/go v0.5.0
github.com/dave/jennifer v1.6.1
github.com/gin-gonic/gin v1.9.1
github.com/go-playground/validator/v10 v10.14.0
github.com/stretchr/testify v1.8.4
github.com/urfave/cli/v2 v2.25.5
)
Expand All @@ -20,7 +21,6 @@ require (
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/google/uuid v1.2.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand Down
2 changes: 1 addition & 1 deletion internal/gencode/gencode.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func Exec(root string, rootOutput string) error {
return nil
}

func gen(fhub model.Fhub) ([]byte, error) {
func gen(fhub model.FHub) ([]byte, error) {
f := jen.NewFile("main")
f.PackagePrefix = "pkg"

Expand Down
4 changes: 2 additions & 2 deletions internal/gencode/gencode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func Test_gen(t *testing.T) {

t.Run("not use initialize", func(t *testing.T) {
fhub := model.Fhub{
fhub := model.FHub{
Packages: map[string]model.Package{
"test": {
Import: "fhub.dev/test",
Expand Down Expand Up @@ -69,7 +69,7 @@ func (f *functions) function_test(input map[string]any) map[string]any {
})

t.Run("use initialize", func(t *testing.T) {
fhub := model.Fhub{
fhub := model.FHub{
Packages: map[string]model.Package{
"test": {
Import: "fhub.dev/test",
Expand Down
144 changes: 138 additions & 6 deletions model/fhub.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,145 @@

package model

type Fhub struct {
Name string
Version string
SpecVersion string
type FHub struct {
// Function namespace
Name string `validate:"required"`
// Function version
Version string `validate:"required"`
// FHub schema version
SpecVersion string `validate:"required"`
Constants map[string]string
Env []string
Import []string
Packages map[string]Package
Functions map[string]Function
Packages map[string]Package `validate:"min=1,dive"`
Functions map[string]Function `validate:"min=1,dive"`
}

func (in *FHub) DeepCopy() (out *FHub) {
out = new(FHub)
*out = *in

out.Env = make([]string, len(in.Env))
copy(out.Env, in.Env)

out.Import = make([]string, len(in.Import))
copy(out.Import, in.Env)

out.Constants = make(map[string]string, len(in.Constants))
for key, constant := range in.Constants {
out.Constants[key] = constant
}

out.Functions = make(map[string]Function, len(in.Functions))
for key, function := range in.Functions {
out.Functions[key] = function
}

out.Packages = make(map[string]Package, len(in.Packages))
for key, pkg := range in.Packages {
out.Packages[key] = pkg
}

return
}

type Package struct {
Import string `validate:"required"`
Launch string
Build Build
Serving Serving
}

func (p *Package) HasLaunch() bool {
return p.Launch != ""
}

func (in *Package) DeepCopy() (out *Package) {
*out = *in
return
}

type Serving struct {
Http *Http
Grpc *Grpc
}

func (in *Serving) DeepCopy() (out *Serving) {
out = new(Serving)
*out = *in

if out.Http != nil {
in, out := &in.Http, &out.Http
*out = new(Http)
**out = **in
}
if out.Grpc != nil {
in, out := &in.Grpc, &out.Grpc
*out = new(Grpc)
**out = **in
}
return
}

type Http struct {
Url string `validate:"required"`
}

func (in *Http) DeepCopy() (out *Http) {
out = new(Http)
*out = *in
return
}

type Grpc struct {
Proto string `validate:"required"`
}

func (in *Grpc) DeepCopy() (out *Grpc) {
out = new(Grpc)
*out = *in
return
}

type Build struct {
Local *Local `validate:"required_without=Container"`
Container *Container `validate:"required_without=Local"`
}

func (in *Build) DeepCopy() (out *Build) {
out = new(Build)
*out = *in
if out.Local != nil {
in, out := &in.Local, &out.Local
*out = new(Local)
**out = **in
}
if out.Container != nil {
in, out := &in.Container, &out.Container
*out = new(Container)
**out = **in
}
return out
}

type Local struct {
Source string `validate:"required"`
}

func (in *Local) DeepCopy() (out *Local) {
out = new(Local)
*out = *in
return
}

type Container struct {
Image string `validate:"required_without=ContainerFile"`
ContainerFile string `validate:"required_without=Image"`
Source string `validate:"required"`
}

func (in *Container) DeepCopy() (out *Container) {
out = new(Container)
*out = *in
return
}
12 changes: 6 additions & 6 deletions model/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ type Function struct {
inputValue cue.Value `fhub:"input" fhub-unmarshal:"true"`
outputValue cue.Value `fhub:"output" fhub-unmarshal:"true"`

Package string
Launch string
Package string `validate:"required"`
Launch string `validate:"required"`

InputsLabel []string
InputsType []string
OutputsLabel []string
OutputsType []string
InputsLabel []string `validate:"min=1"`
InputsType []string `validate:"min=1"`
OutputsLabel []string `validate:"min=1"`
OutputsType []string `validate:"min=1"`
}

func (f *Function) Unmarshal(field string, value cue.Value) (err error) {
Expand Down
52 changes: 0 additions & 52 deletions model/package.go

This file was deleted.

36 changes: 20 additions & 16 deletions model/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,48 +30,48 @@ import (
"cuelang.org/go/cue/cuecontext"
)

func UnmarshalFile(path string) (Fhub, error) {
func UnmarshalFile(path string) (FHub, error) {
data, err := os.ReadFile(path)
if err != nil {
return Fhub{}, err
return FHub{}, err
}
return UnmarshalBytes(data)
}

func UnmarshalHttp(url string) (Fhub, error) {
func UnmarshalHttp(url string) (FHub, error) {
resp, err := http.Get(url)
if err != nil {
return Fhub{}, err
return FHub{}, err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return Fhub{}, err
return FHub{}, err
}
return UnmarshalBytes(body)
}

func UnmarshalBytes(data []byte) (Fhub, error) {
func UnmarshalString(data string) (FHub, error) {
return UnmarshalBytes([]byte(data))
}

func UnmarshalBytes(data []byte) (FHub, error) {
ctx := cuecontext.New()
value := ctx.CompileBytes(data)
fhub, err := unmarshalStart(value)
if err != nil {
return Fhub{}, err
return FHub{}, err
}
return fhub, nil
}

func UnmarshalString(data string) (Fhub, error) {
ctx := cuecontext.New()
value := ctx.CompileString(data)
fhub, err := unmarshalStart(value)
err = Validator(fhub)
if err != nil {
return Fhub{}, err
return FHub{}, err
}

return fhub, nil
}

func unmarshalStart(value cue.Value) (Fhub, error) {
fhub := Fhub{}
func unmarshalStart(value cue.Value) (FHub, error) {
fhub := FHub{}

outValueOf := reflect.ValueOf(&fhub).Elem()
err := unmarshalDiscoverType("fhub", outValueOf, value)
Expand All @@ -98,9 +98,13 @@ func unmarshalDiscoverType(namespace string, outValueOf reflect.Value, value cue
err = unmarshalMapIt(namespace, outValueOf, value)
case reflect.Slice:
err = unmarshalListIt(namespace, outValueOf, value)
case reflect.Ptr:
outValueOf.Set(reflect.New(outValueOf.Type().Elem()))
err = unmarshalDiscoverType(namespace, outValueOf.Elem(), value)
default:
panic(fmt.Sprintf("type %q not implemented from key", kind))
}

return err
}

Expand Down
Loading

0 comments on commit 5f830a8

Please sign in to comment.