Skip to content

Commit

Permalink
fix things
Browse files Browse the repository at this point in the history
  • Loading branch information
henriqueleite42 committed Oct 1, 2024
1 parent a31925e commit a19ae16
Show file tree
Hide file tree
Showing 11 changed files with 379 additions and 335 deletions.
2 changes: 1 addition & 1 deletion cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/henriqueleite42/anvil/cli
go 1.22.4

require (
github.com/henriqueleite42/anvil/language-helpers/golang v0.0.0-20240926193919-c12edacca189
github.com/henriqueleite42/anvil/language-helpers/golang v0.0.0-20241001175549-a31925e518ee
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
gopkg.in/yaml.v3 v3.0.1
Expand Down
4 changes: 2 additions & 2 deletions cli/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/henriqueleite42/anvil/language-helpers/golang v0.0.0-20240926193919-c12edacca189 h1:S100YyQ7hEvBmsGtdXf+u91kSuvWtAfquwfV+PKKFtk=
github.com/henriqueleite42/anvil/language-helpers/golang v0.0.0-20240926193919-c12edacca189/go.mod h1:1aCAFI8VjcMPFPPx55qS1gV768EL24uu7veUx1SsPpE=
github.com/henriqueleite42/anvil/language-helpers/golang v0.0.0-20241001175549-a31925e518ee h1:1DIE26hBYQ4dKKUuV5iy0yBgnLGPuooAXI0kmyqEmr4=
github.com/henriqueleite42/anvil/language-helpers/golang v0.0.0-20241001175549-a31925e518ee/go.mod h1:1aCAFI8VjcMPFPPx55qS1gV768EL24uu7veUx1SsPpE=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
Expand Down
41 changes: 19 additions & 22 deletions cli/internal/parser/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,6 @@ func (self *anvToAnvpParser) resolveEntity(i *resolveInput) (string, error) {

for columnOrder, kk := range keys {
vv := columnsMap[kk]
vvMap, ok := vv.(map[string]any)
if !ok {
return "", fmt.Errorf("fail to parse \"%s.%s.Columns.%s\" to `map[string]any`", i.path, i.k, kk)
}

var columnName string
columnNameAny, ok := vvMap["Name"]
if ok {
columnNameString, ok := columnNameAny.(string)
if !ok {
return "", fmt.Errorf("fail to parse \"%s.%s.Columns.%s.Name\" to `string`", i.path, i.k, kk)
}
columnName = columnNameString
} else {
columnName = self.formatToEntitiesNamingCase(kk)
}

columnPath := fmt.Sprintf("%s.%s.Columns.%s", i.path, i.k, kk)
columnRef := ref + "." + kk
Expand All @@ -148,7 +132,6 @@ func (self *anvToAnvpParser) resolveEntity(i *resolveInput) (string, error) {
OriginalPath: columnPath,
Order: columnOrder,
Name: kk,
DbName: columnName,
TypeHash: typeHash,
}

Expand Down Expand Up @@ -253,7 +236,14 @@ func (self *anvToAnvpParser) resolveEntity(i *resolveInput) (string, error) {
if !ok {
return "", fmt.Errorf("fail to find one of the columns to created database name for \"%s.%s.Indexes.%d\"", i.path, i.k, kk)
}
columnsToCreateName = append(columnsToCreateName, column.DbName)
cType, ok := self.schema.Types.Types[column.TypeHash]
if !ok {
return "", fmt.Errorf("fail to find one of the columns types to created database name for \"%s.%s.Indexes.%d\"", i.path, i.k, kk)
}
if cType.DbName == nil {
return "", fmt.Errorf("fail to find get DbName to created database name for \"%s.%s.Indexes.%d\"", i.path, i.k, kk)
}
columnsToCreateName = append(columnsToCreateName, *cType.DbName)
}
// TODO make it dynamic to match pattern specified in Entities.NamingCase (maybe create a Entities.ConstraintCase?)
name = strings.Join(columnsToCreateName, "_") + "_idx"
Expand Down Expand Up @@ -323,13 +313,20 @@ func (self *anvToAnvpParser) resolveEntity(i *resolveInput) (string, error) {
columnRef := fmt.Sprintf("Entities.%s.%s", i.k, vvvString)
hash := hashing.String(columnRef)

column := columns[hash]

if column == nil {
column, ok := columns[hash]
if !ok {
return "", fmt.Errorf("fail to find column \"%s\" for \"%s.%s.ForeignKeys.%d.Columns.%d\"", vvvString, i.path, i.k, kk, kkk)
}

columnsNamesForFkName = append(columnsNamesForFkName, column.DbName)
cType, ok := self.schema.Types.Types[column.TypeHash]
if !ok {
return "", fmt.Errorf("fail to find one of the columns types to created database name for \"%s.%s.Indexes.%d\"", i.path, i.k, kk)
}
if cType.DbName == nil {
return "", fmt.Errorf("fail to find get DbName to created database name for \"%s.%s.Indexes.%d\"", i.path, i.k, kk)
}

columnsNamesForFkName = append(columnsNamesForFkName, *cType.DbName)
columnsHashes = append(columnsHashes, hash)
}

Expand Down
24 changes: 19 additions & 5 deletions cli/internal/parser/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ func (self *anvToAnvpParser) resolveType(i *resolveInput) (string, error) {
return refHash, nil
}

rootNode, err := getRootNode(i.path)
if err != nil {
return "", err
}

vMap, ok := i.v.(map[string]any)
if !ok {
return "", fmt.Errorf("fail to parse \"%s.%s\" to `map[string]any`", i.path, i.k)
Expand Down Expand Up @@ -234,6 +239,19 @@ func (self *anvToAnvpParser) resolveType(i *resolveInput) (string, error) {
return "", fmt.Errorf("Type \"%s.%s\" must have property \"Values\". All types with enum \"Type\" must.", i.path, i.k)
}

var dbName *string = nil
dbNameAny, ok := vMap["DbName"]
if ok {
dbNameString, ok := dbNameAny.(string)
if !ok {
return "", fmt.Errorf("fail to parse \"%s.%s.DbName\" to `string`", i.path, i.k)
}
dbName = &dbNameString
} else if rootNode == "Entities" {
r := self.formatToEntitiesNamingCase(i.k)
dbName = &r
}

var dbType *string = nil
dbTypeAny, ok := vMap["DbType"]
if ok {
Expand All @@ -256,11 +274,6 @@ func (self *anvToAnvpParser) resolveType(i *resolveInput) (string, error) {
dbType = &enum.DbType
}

rootNode, err := getRootNode(i.path)
if err != nil {
return "", err
}

name := i.k
if typeType == schemas.TypeType_Map {
name = i.namePrefix + i.k
Expand All @@ -278,6 +291,7 @@ func (self *anvToAnvpParser) resolveType(i *resolveInput) (string, error) {
AutoIncrement: autoIncrement,
Default: defaultV,
Type: typeType,
DbName: dbName,
DbType: dbType,
ChildTypesHashes: childTypesHashes,
EnumHash: enumHash,
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/how-it-works.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Think about the `.anv` files like a `schemas.prisma` or an OpenApi spec, and fro

`.anvilconfig` is the configuration file for Anvil, where you put information like the plugins that you are using, the things that you want to generate, and any other configuration that Anvil CLI or the plugins may need.

It is written in [TOML](https://toml.io/en/).
It is written in [INI](https://en.wikipedia.org/wiki/INI_file).

## CLI

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/tech-decisions.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Because [Henrique Leite](https://henriqueleite42.com), the initial main contribu

### Why YAML?

The options that we have are: YAML, JSON and TOML. YAML is the best between the three for long nested configuration. TOML doesn't look good and can be extremely repetitive when dealing with these things, and JSON doesn't support comments.
The options that we have are: YAML, JSON, TOML and INI. YAML is the best between the three for long nested configuration. TOML doesn't look good and can be extremely repetitive when dealing with these things, JSON doesn't support comments and INI is more focused in configuration, not specification.

### Why does Anvil have some hidden behavior?

Expand Down
Loading

0 comments on commit a19ae16

Please sign in to comment.