Skip to content

Commit

Permalink
add entities
Browse files Browse the repository at this point in the history
  • Loading branch information
henriqueleite42 committed Aug 30, 2024
1 parent afada1f commit 300731e
Show file tree
Hide file tree
Showing 29 changed files with 1,257 additions and 444 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"Protofiles",
"Timestampz",
"Usecase",
"Usecases"
"Usecases",
"VARCHAR"
]
}
17 changes: 17 additions & 0 deletions cli/internal/formatter/string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package formatter

import (
"regexp"
"strings"
)

var matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")

func PascalToSnake(str string) string {
snake := matchAllCap.ReplaceAllString(str, "${1}_${2}")
return strings.ToLower(snake)
}

func PascalToCamel(str string) string {
return strings.ToLower(str[0:1]) + str[1:]
}
45 changes: 23 additions & 22 deletions cli/internal/parser/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"

"github.com/anvil/anvil/internal/hashing"
"github.com/anvil/anvil/internal/schema"
"github.com/anvil/anvil/schemas"
)

func (self *anvToAnvpParser) auth(file map[string]any) error {
Expand All @@ -13,32 +13,32 @@ func (self *anvToAnvpParser) auth(file map[string]any) error {
return nil
}

fullPath := self.getPath("Auth")
path := self.getPath("Auth")

authMap, ok := authSchema.(map[string]any)
if !ok {
return fmt.Errorf("fail to parse \"%s\" to `map[string]any`", fullPath)
return fmt.Errorf("fail to parse \"%s\" to `map[string]any`", path)
}

if self.schema.Auths == nil {
self.schema.Auths = &schema.Auths{}
self.schema.Auths = &schemas.Auths{}
}
if self.schema.Auths.Auths == nil {
self.schema.Auths.Auths = map[string]*schema.Auth{}
self.schema.Auths.Auths = map[string]*schemas.Auth{}
}

for k, v := range authMap {
vMap, ok := v.(map[string]any)
if !ok {
return fmt.Errorf("fail to parse \"%s.%s\" to `map[string]any`", fullPath, k)
return fmt.Errorf("fail to parse \"%s.%s\" to `map[string]any`", path, k)
}

var description *string = nil
descriptionAny, ok := vMap["Description"]
if ok {
descriptionString, ok := descriptionAny.(string)
if !ok {
return fmt.Errorf("fail to parse \"%s.%s.Description\" to `string`", fullPath, k)
return fmt.Errorf("fail to parse \"%s.%s.Description\" to `string`", path, k)
}
description = &descriptionString
}
Expand All @@ -48,7 +48,7 @@ func (self *anvToAnvpParser) auth(file map[string]any) error {
if ok {
schemeString, ok := schemeAny.(string)
if !ok {
return fmt.Errorf("fail to parse \"%s.%s.Scheme\" to `string`", fullPath, k)
return fmt.Errorf("fail to parse \"%s.%s.Scheme\" to `string`", path, k)
}
scheme = schemeString
}
Expand All @@ -58,46 +58,47 @@ func (self *anvToAnvpParser) auth(file map[string]any) error {
if ok {
formatString, ok := formatAny.(string)
if !ok {
return fmt.Errorf("fail to parse \"%s.%s.Format\" to `string`", fullPath, k)
return fmt.Errorf("fail to parse \"%s.%s.Format\" to `string`", path, k)
}
format = &formatString
}

var applyToallRoutes bool
applyToallRoutesAny, ok := vMap["ApplyToAllRoutes"]
var applyToAllRoutes bool
applyToAllRoutesAny, ok := vMap["ApplyToAllRoutes"]
if ok {
applyToallRoutesString, ok := applyToallRoutesAny.(bool)
applyToAllRoutesString, ok := applyToAllRoutesAny.(bool)
if !ok {
return fmt.Errorf("fail to parse \"%s.%s.ApplyToAllRoutes\" to `string`", fullPath, k)
return fmt.Errorf("fail to parse \"%s.%s.ApplyToAllRoutes\" to `string`", path, k)
}
applyToallRoutes = applyToallRoutesString
applyToAllRoutes = applyToAllRoutesString
}

originalPath := fullPath + "." + k
originalPathHash := hashing.String(originalPath)
ref := self.getRef("", "Auth."+k)
refHash := hashing.String(ref)

rootNode, err := getRootNode(fullPath)
rootNode, err := getRootNode(path)
if err != nil {
return err
}

auth := &schema.Auth{
auth := &schemas.Auth{
Ref: ref,
OriginalPath: path + k,
Name: k,
RootNode: rootNode,
OriginalPath: originalPath,
Description: description,
Scheme: scheme,
Format: format,
ApplyToAllRoutes: applyToallRoutes,
ApplyToAllRoutes: applyToAllRoutes,
}

stateHash, err := hashing.Struct(auth)
if err != nil {
return fmt.Errorf("fail to get enum \"%s\" state hash", originalPath)
return fmt.Errorf("fail to get enum \"%s.%s\" state hash", path, k)
}

auth.StateHash = stateHash
self.schema.Auths.Auths[originalPathHash] = auth
self.schema.Auths.Auths[refHash] = auth
}

return nil
Expand Down
6 changes: 3 additions & 3 deletions cli/internal/parser/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
)

func (self *anvToAnvpParser) domain(file map[string]any) error {
fullPath := self.getPath("Domain")

domainSchema, ok := file["Domain"]
if !ok {
return fmt.Errorf("\"%s\" must be specified", self.getPath("Domain"))
return fmt.Errorf("\"%s\" must be specified", fullPath)
}

fullPath := self.getPath("Domain")

domainString, ok := domainSchema.(string)
if !ok {
return fmt.Errorf("fail to parse \"%s\" to `string`", fullPath)
Expand Down
Loading

0 comments on commit 300731e

Please sign in to comment.