Skip to content

Commit

Permalink
feat: add type aliases, refactor go schema parsing (#1493)
Browse files Browse the repository at this point in the history
#1238
Adds the ability to declare type aliases in modules
```
//ftl:typealias
type UserId string
```

There is a lot of overlap between typealiases and enums. They both
redefine an existing type as a new type, and therefore need to make sure
our schema parsing does not skip this defined type by looking at the
underlying type.
There were also issues if implicit exports, enum cases (and more?) were
encountered in the ast tree before we found the typealias or enum
definition.
This PR solves that by doing an initial pass just to find all typealias
and enum declarations for the module and then doing the normal pass.

Previously typealiases were allowed without any declaration but they
would just fall back to the underlying type.
This PR makes this an error as we do not know if this type should be an
enum or a type alias. We may want to discuss this more.

fixes #1475
#1476
#1477
#1492
#1449
  • Loading branch information
matt2e authored May 16, 2024
1 parent 64c18c0 commit a44e99b
Show file tree
Hide file tree
Showing 34 changed files with 1,479 additions and 642 deletions.
2 changes: 1 addition & 1 deletion backend/controller/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (c *ConsoleService) GetModules(ctx context.Context, req *connect.Request[pb
Config: c,
})

case *schema.Database, *schema.Enum, *schema.FSM:
case *schema.Database, *schema.Enum, *schema.TypeAlias, *schema.FSM:
}
}

Expand Down
2 changes: 2 additions & 0 deletions backend/controller/ingress/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ func transformAliasedFields(sch *schema.Schema, t schema.Type, obj any, aliaser
}
}
}
case *schema.TypeAlias:
return transformAliasedFields(sch, decl.Type, obj, aliaser)
case *schema.Config, *schema.Database, *schema.FSM, *schema.Secret, *schema.Verb:
return fmt.Errorf("%s: unsupported ref type %T", t.Pos, decl)
}
Expand Down
2 changes: 2 additions & 0 deletions backend/controller/ingress/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ func validateValue(fieldType schema.Type, path path, value any, sch *schema.Sche
}
typeMatches = true
}
case *schema.TypeAlias:
return validateValue(d.Type, path, value, sch)
case *schema.Enum:
var inputName any
inputName = value
Expand Down
25 changes: 24 additions & 1 deletion backend/controller/ingress/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,30 @@ func TestValidation(t *testing.T) {
unit Unit
}
}`,
request: obj{}},
request: obj{},
},
{name: "StringAlias",
schema: `module test {
typealias StringAlias String
data Test { stringValue test.StringAlias }
}`,
request: obj{"stringValue": "test"},
},
{name: "IntAlias",
schema: `module test {
typealias IntAlias Int
data Test { intValue test.IntAlias }
}`,
request: obj{"intValue": 10.0},
},
{name: "DataAlias",
schema: `module test {
typealias IntAlias test.Inner
data Inner { string String }
data Test { obj test.IntAlias }
}`,
request: obj{"obj": obj{"string": "test"}},
},
{name: "RequiredFields",
schema: `module test { data Test { int Int } }`,
request: obj{},
Expand Down
Loading

0 comments on commit a44e99b

Please sign in to comment.