Skip to content

Can't use custom func as output #49

Closed

Description

@queil

Trying to use a custom function in Output I am getting the following exception:

 System.Exception: Cannot convert output type.
         at Fli.CE.ICommandContext`1.outputTypeMapping[a,a](ICommandContext`1 _, a output)
         at Fli.CE.ICommandContext`1.Output[a,a](ICommandContext`1 this, ICommandContext`1 context, a output)

My command:

    let! output =
                    cli {
                        Shell BASH
                        Command (opts.Value.Path)
                        WorkingDirectory(opts.Value.WorkingDir)
                        Output (Custom(Func<string,unit>(fun s -> ctx.GetLogger().LogInformation("log: {Message}", s) )
                        ))
                    }
                    |> Command.executeAsync
                    |> Async.StartAsTask

Activity

  1. queil commented on May 26, 2023

    @queil
    Author

    Ok, I can see what you've done here. It's "generic". Why not make it type-safe?

    EDIT: Sorry for my initial harsh response. It was late... I think I've got confused by the docs stating:

    Provided Fli.Outputs:
    File of string a string with an absolute path of the output file.
    StringBuilder of StringBuilder a StringBuilder which will be filled with the output text.
    Custom of Func<string, unit> a custom function (string -> unit) that will be called with the output string (logging, printing etc.).

    So I used Output (Custom(Func<string,unit>(fun s -> ... which is not supported by outputTypeMapping.
    Rather than doing runtime casts - couldn't be multiple output types solved via separate overloads of the Output custom operation in the CE?

  2. added
    bug
    question
    and removed
    bug
    on May 26, 2023
  3. CaptnCodr commented on May 27, 2023

    @CaptnCodr
    Owner

    Hi @queil,
    I wanted to dig into your issue and saw your recent edit. Good that you found the correct overload now.

    couldn't be multiple output types solved via separate overloads of the Output custom operation in the CE?

    Do you have something in mind or a good example of what exactly you mean? To force the user to use the DU as you thought in your opening comment?

  4. queil commented on May 27, 2023

    @queil
    Author

    I'll try to put an example together in the evening.

  5. queil commented on May 27, 2023

    @queil
    Author

    @CaptnCodr I was thinking of something along the lines of the below. Essentially you can define Output multiple times with different types. That way you avoid casting on runtime and also the user can't get it wrong because the compiler will prevent it. Also the user doesn't need to use DU but can still conveniently just pass a value of an actual type. Another also, it would be a non-breaking API change too as any not supported types would be already throwing an exception.

    namespace Fli
    
    [<AutoOpen>]
    module CE =
    
        open System.Text
        open Domain
    
        type ICommandContext<'a> with
    
            member this.Yield(_) = this
    
        type StartingContext =
            { config: Config option }
    
            member this.CurrentConfig = this.config |> Option.defaultValue Defaults
    
            interface ICommandContext<StartingContext> with
                member this.Context = this
    
        let cli = { config = None }
    
        /// Extensions for Shell context.
        type ICommandContext<'a> with
    
            /// `Shell` opening keyword, which `Fli.Shell` shall be used.
            [<CustomOperation("Shell")>]
            member _.Shell(context: ICommandContext<StartingContext>, shell) =
                Cli.shell shell context.Context.CurrentConfig
    
            /// Which `Command` should be executed in the `Shell`.
            [<CustomOperation("Command")>]
            member _.Command(context: ICommandContext<ShellContext>, command) = Cli.command command context.Context
    
            /// `Input` string(s) that can be used to interact with the shell.
            [<CustomOperation("Input")>]
            member _.Input(context: ICommandContext<ShellContext>, input) = Cli.input input context.Context
    
            /// Extra `Output` that is being executed immediately after getting output from execution.
            [<CustomOperation("Output")>]
            member this.Output(context: ICommandContext<ShellContext>, filePath: string) =
                Cli.output (File filePath) context.Context
    
            [<CustomOperation("Output")>]
            member this.Output(context: ICommandContext<ShellContext>, func: string -> unit) =
                Cli.output (Custom func) context.Context
        
            [<CustomOperation("Output")>]
            member this.Output(context:  ICommandContext<ShellContext>, builder: StringBuilder) =
                Cli.output (StringBuilder builder) context.Context
    
            /// Current executing `working directory`.
            [<CustomOperation("WorkingDirectory")>]
            member _.WorkingDirectory(context: ICommandContext<ShellContext>, workingDirectory) =
                Cli.workingDirectory workingDirectory context.Context
    
            /// One tupled `EnvironmentVariable`.
            [<CustomOperation("EnvironmentVariable")>]
            member _.EnvironmentVariable(context: ICommandContext<ShellContext>, environmentVariable) =
                Cli.environmentVariables [ environmentVariable ] context.Context
    
            /// A list of tupled `EnvironmentVariables`.
            [<CustomOperation("EnvironmentVariables")>]
            member _.EnvironmentVariables(context: ICommandContext<ShellContext>, environmentVariables) =
                Cli.environmentVariables environmentVariables context.Context
    
            /// `Encoding` that'll be used for `Input`, 'Output.Text' and `Output.Error`.
            [<CustomOperation("Encoding")>]
            member _.Encoding(context: ICommandContext<ShellContext>, encoding) = Cli.encoding encoding context.Context
    
        /// Extensions for Exec context.
        type ICommandContext<'a> with
    
            /// `Exec` opening keyword, which binary/executable shall be started.
            [<CustomOperation("Exec")>]
            member _.Exec(context: ICommandContext<StartingContext>, program) =
                Program.program program context.Context.CurrentConfig
    
            /// `Arguments` that will be passed into the executable.
            [<CustomOperation("Arguments")>]
            member _.Arguments(context: ICommandContext<ExecContext>, arguments) =
                let matchArguments arguments =
                    match box (arguments) with
                    | :? string as s -> s
                    | :? seq<string> as s -> s |> Seq.map string |> String.concat " "
                    | _ -> failwith "Cannot convert arguments to a string!"
    
                Program.arguments (matchArguments arguments) context.Context
    
            /// `Input` string(s) that can be used to interact with the executable.
            [<CustomOperation("Input")>]
            member _.Input(context: ICommandContext<ExecContext>, input) = Program.input input context.Context
    
            /// Extra `Output` that is being executed immediately after getting output from execution.
            [<CustomOperation("Output")>]
            member this.Output(context: ICommandContext<ExecContext>, filePath: string) =
                Program.output (File filePath) context.Context
    
            [<CustomOperation("Output")>]
            member this.Output(context: ICommandContext<ExecContext>, func: string -> unit) =
                Program.output (Custom func) context.Context
        
            [<CustomOperation("Output")>]
            member this.Output(context:  ICommandContext<ExecContext>, builder: StringBuilder) =
                Program.output (StringBuilder builder) context.Context
    
            /// Current executing `working directory`.
            [<CustomOperation("WorkingDirectory")>]
            member _.WorkingDirectory(context: ICommandContext<ExecContext>, workingDirectory) =
                Program.workingDirectory workingDirectory context.Context
    
            /// `Verb` keyword that can be used to start the executable.
            [<CustomOperation("Verb")>]
            member _.Verb(context: ICommandContext<ExecContext>, verb) = Program.verb verb context.Context
    
            /// Start the executable with another `Username`.
            [<CustomOperation("Username")>]
            member _.UserName(context: ICommandContext<ExecContext>, userName) =
                Program.userName userName context.Context
    
            /// Start the executable with other `Credentials`.
            /// Hint: `Domain` and `Password` are available for Windows systems only.
            [<CustomOperation("Credentials")>]
            member _.Credentials(context: ICommandContext<ExecContext>, credentials) =
                let (domain, user, pw) = credentials in Program.credentials (Credentials(domain, user, pw)) context.Context
    
            /// One tupled `EnvironmentVariable`.
            [<CustomOperation("EnvironmentVariable")>]
            member _.EnvironmentVariable(context: ICommandContext<ExecContext>, environmentVariable) =
                Program.environmentVariables [ environmentVariable ] context.Context
    
            /// A list of tupled `EnvironmentVariables`.
            [<CustomOperation("EnvironmentVariables")>]
            member _.EnvironmentVariables(context: ICommandContext<ExecContext>, environmentVariables) =
                Program.environmentVariables environmentVariables context.Context
    
            /// `Encoding` that'll be used for `Input`, 'Output.Text' and `Output.Error`.
            [<CustomOperation("Encoding")>]
            member _.Encoding(context: ICommandContext<ExecContext>, encoding) =
                Program.encoding encoding context.Context
  6. added
    enhancement
    and removed
    question
    on May 27, 2023
  7. CaptnCodr commented on May 27, 2023

    @CaptnCodr
    Owner

    @queil Thanks for sharing. I was pretty sure that CustomOperations can't share the same name, but I was wrong.
    This change comes in the next version + I amended some docs about this.

  8. queil commented on May 27, 2023

    @queil
    Author

    I think it was implemented in dotnet/fsharp#9400

  9. queil commented on May 27, 2023

    @queil
    Author

    Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions