F#
An F# project that references NSmithy generates F#: records and discriminated
unions for the model’s shapes, their runtime schemas, and the service’s client
and server. Generation runs during dotnet build, as it does for C#, and the
generated code uses the same runtime packages.
The F# example has a server and a client that share a contracts project.
Set up a project
Section titled “Set up a project”To start from a template, pass -lang F# to dotnet new nsmithy-contracts,
nsmithy-server, or nsmithy-client; see Quick start.
Name the service with SmithyService. NSmithy sees that the project is an F#
project and generates F#. A client project references NSmithy.Client and the
package of the service’s protocol:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net10.0</TargetFramework> <SmithyService>example.weather#Weather</SmithyService> <SmithyGenerateServer>false</SmithyGenerateServer> </PropertyGroup>
<ItemGroup> <Compile Include="Program.fs" /> </ItemGroup>
<ItemGroup> <PackageReference Include="NSmithy.Client" Version="0.23.0" /> <PackageReference Include="NSmithy.Protocols.RestJson" Version="0.23.0" /> <PackageReference Include="NSmithy.SmithyCli" Version="1.75.0" /> </ItemGroup></Project>A server project uses the Web SDK and references NSmithy.Server.AspNetCore
instead:
<Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>net10.0</TargetFramework> <SmithyService>example.weather#Weather</SmithyService> <SmithyGenerateClient>false</SmithyGenerateClient> </PropertyGroup>
<ItemGroup> <Compile Include="Program.fs" /> </ItemGroup>
<ItemGroup> <PackageReference Include="NSmithy.Server.AspNetCore" Version="0.23.0" /> <PackageReference Include="NSmithy.Protocols.RestJson" Version="0.23.0" /> <PackageReference Include="NSmithy.SmithyCli" Version="1.75.0" /> </ItemGroup></Project>Both read the model through a smithy-build.json:
{ "version": "1.0", "sources": ["model"], "maven": { "dependencies": ["software.amazon.smithy:smithy-aws-traits:1.75.0"] }}The model can also come from a contracts project, as in Distributing contracts.
Generated files are compiled before the project’s own files, so every .fs
file in the project can use them.
Like a C# project, an F# project generates the shapes in its own model files and uses the types that the F# projects it references generated, so the modules of a server can each own their operations (Splitting a service across projects). C# and F# projects do not share generated types.
What the model becomes
Section titled “What the model becomes”| Model | Generated F# |
|---|---|
| Structures | Records, such as GetCityOutput. A member that may be absent is an option; a member with a default is a plain value that reading fills in. |
@error structures | Records, such as NoSuchResource, whose field labels need the type where F# cannot infer it: ({ ResourceType = "City" }: NoSuchResource). |
| Unions | Discriminated unions with an Unknown of tag * value case for a variant this model does not define. |
| Enums | Discriminated unions with an Unknown of string case. FromValue and .Value convert to and from the wire value. |
| Int enums | F# enums. |
| Enum members | Cases in Pascal case: IN_PROGRESS becomes InProgress. |
| Lists and maps | 'T list and Map<string, 'T>, with option elements when @sparse. |
| Operations | A union such as GetCityError of the errors each operation can return, which its handler and clients return. |
The service, when SmithyGenerateClient is on | IWeatherClient, WeatherClient, and WeatherClientConfig. |
The service, when SmithyGenerateServer is on | A handler interface IWeatherServiceHandler composed of one interface per operation, plus AddWeatherServiceHandler and MapWeatherService extensions for ASP.NET Core. |
| Every shape | A runtime schema in the child Schemas namespace, such as Example.Weather.Schemas.GetCityOutputSchema. |
Serve the service
Section titled “Serve the service”Implement the handler interface and register it with AddWeatherServiceHandler
and MapWeatherService; the Servers pages show each
step in C# and F#. The handler of an operation that can return errors returns a
Result with the operation’s error union, and the runtime writes the Error
case as that error:
member _.GetCityAsync(input, _) = match cities.TryFind input.CityId with | Some city -> Task.FromResult(Ok city) | None -> Task.FromResult(Error(GetCityError.NoSuchResource { ResourceType = "City" }))AddWeatherServiceHandler also takes a handler instance, such as an object
expression:
builder.Services.AddWeatherServiceHandler { new IWeatherServiceHandler with member _.GetCityAsync(input, _) = getCity input // one member per operation }|> ignoreA handler that needs services from the container takes a factory instead. It
runs once by default, or once per request with ServiceLifetime.Scoped, which
a handler using a DbContext needs:
builder.Services.AddWeatherServiceHandler( (fun services -> Weather.create (services.GetRequiredService<WeatherDb>())), ServiceLifetime.Scoped)|> ignoreCall the service
Section titled “Call the service”Each operation is a method that takes the input record and an optional
CancellationToken, and returns a Task. An operation whose input members are
all optional also has an overload without the input. In a project that
generates only the client, every input member is an option (see
Member optionality).
An operation that can return errors returns its output as a Result with its
error union:
open Systemopen Example.Weather
let run () = task { use client = new WeatherClient(Uri "https://weather.example.com")
match! client.GetCityAsync { CityId = "SEA" } with | Ok city -> printfn "%s is at %f, %f" city.Name city.Coordinates.Latitude city.Coordinates.Longitude | Error(GetCityError.NoSuchResource error) -> printfn "No such %s" error.ResourceType }
(run ()).GetAwaiter().GetResult()The union also has a case for each error the service declares for every operation, so handlers return those as well and never raise a modeled error. The project that generates an operation adds its service’s errors when it also generates the service. When operations live in a project of their own (see Splitting a service across projects), that project does not see the service, so the service’s project fails generation until each operation declares the service’s errors itself.
An operation that declares smithy.framework#ValidationException has a
ValidationException case: the server rejects a request that violates the
model’s constraint traits with that error, and a server requires the model to
declare it on every operation whose input is constrained (see
Validation). Declare
it on the operation. The case holds the runtime’s ValidationException, whose
FieldList names each member and the constraint it failed:
match! client.GetCityAsync { CityId = "SEA!" } with| Ok city -> printfn "%s" city.Name| Error(GetCityError.NoSuchResource error) -> printfn "No such %s" error.ResourceType| Error(GetCityError.ValidationException error) -> for field in error.FieldList do printfn "%s: %s" field.Path field.MessageOther failures are exceptions: transport failures, cancellation, and error responses the model does not declare for the operation.
The client is configured through WeatherClientConfig as in C#; see
Client configuration. For code
that uses async { }, wrap a call in Async.AwaitTask.
Limitations
Section titled “Limitations”- The shapes of two Smithy namespaces cannot refer to each other. F# compiles one
namespace after another, so generation fails; move the shapes into one
namespace, for example with the
renameShapesbuild transform. - A service with endpoint rules has no generated endpoint resolver. Set
EndpointorEndpointResolveron the client config. - The AWS JSON, AWS Query, EC2 Query, and REST XML protocols have no F# client.