Saturn.Cli icon indicating copy to clipboard operation
Saturn.Cli copied to clipboard

Support 'int' as a type for 'id'

Open dharmatech opened this issue 5 years ago • 1 comments

This pull request is to add support for having id be of type int. See the following issue: #29 (int not supported as type for id).

dharmatech avatar Feb 09 '21 22:02 dharmatech

In summary, the function generateModel is changed from:

let generateModel name names (fields : Parameter []) =
    let id = fields.[0].name
    let fields = fields |> Array.map (fun f -> sprintf "%s: %s" f.name f.FSharpType) |> String.concat "\n  "

    sprintf """namespace %s
[<CLIMutable>]
type %s = {
  %s
}
module Validation =
  let validate v =
    let validators = [
      fun u -> if isNull u.%s then Some ("%s", "%s shouldn't be empty") else None
    ]
    validators
    |> List.fold (fun acc e ->
      match e v with
      | Some (k,v) -> Map.add k v acc
      | None -> acc
    ) Map.empty
"""     names name fields id id (upper id)

to:

let generateModel name names (fields : Parameter []) =
    let id = fields.[0].name

    let id_type = fields.[0].typ

    let fields = fields |> Array.map (fun f -> sprintf "%s: %s" f.name f.FSharpType) |> String.concat "\n    "

    let template_int = sprintf """namespace %s
    
[<CLIMutable>]
type %s = {
    %s
}
    
module Validation =
    let validate v =
        let validators = []
    
        validators
        |> List.fold (fun acc e ->
            match e v with
            | Some (k,v) -> Map.add k v acc
            | None -> acc
        ) Map.empty
"""

    let template_string = sprintf """namespace %s
[<CLIMutable>]
type %s = {
    %s
}
module Validation =
    let validate v =
        let validators = [
            fun u -> if isNull u.%s then Some ("%s", "%s shouldn't be empty") else None
            ]
        validators
        |> List.fold (fun acc e ->
            match e v with
            | Some (k,v) -> Map.add k v acc
            | None -> acc
        ) Map.empty
"""

    if id_type = Int then
        template_int names name fields
    else
        template_string names name fields id id (upper id)

dharmatech avatar Feb 09 '21 22:02 dharmatech