#Is it possible to use custom types with dagger dependencies?

1 messages · Page 1 of 1 (latest)

upper elk
#

Description

I have a dagger module that uses one custom type to define optional arguments, Is it possible to use that function from a dependant module?

Example

In module A i have something as follows:

type AssertOutputOpts struct {
    ExpectedOutput *string
    ExpectedError  *string
    ExpectedExitCode *int
    DisableCache *bool
}

// Test a container output
func (m *DaggerStructureTest) AssertOutput(ctx context.Context, container *Container, options *AssertOutputOpts) (bool, error) {} 

In module B I made dagger install <module A> to have it as a dependency. However the custom type generated created the properties as private so i cannot create a struct of such type:

// internal/dagger/dagger.gen.go
type DaggerStructureTestAssertOutputOpts struct {
    query *querybuilder.Selection

    disableCache     *bool
    expectedError    *string
    expectedExitCode *int
    expectedOutput   *string
    id               *DaggerStructureTestAssertOutputOptsID
}
west oyster
upper elk
drifting basin
#

To define options via a struct it has to be an anonymous struct, like this:

func (m *DaggerStructureTest) AssertOutput(ctx context.Context, container *Container, options struct {
    // +optional
    ExpectedOutput *string
    // +optional
    ExpectedError  *string
    // +optional
    ExpectedExitCode *int
    // +optional
    DisableCache *bool
}) (bool, error) {
} 
#

When you look at the generated code on a dependent module, an Opts struct is generated with the combination of all +optional arguments. It's not based on whether you use a struct for arguments or not.

upper elk
#

the only thing that I am worried about is how dagger handles optional fields

#

let's say that i want to do something like:

func (m *DaggerStructureTest) AssertOutput(ctx context.Context,
    // required
    container *Container,
    // +optional
    ExpectedExitCode int
}) (bool, error) {
} 
#

What would be the way to check if ExpectedExitCode has been set?

#

something like ExpectedEixtCode == 0 ? (similar to regular go behaviour when a field is not set)

drifting basin
#

You can make it a pointer and check for nil.

upper elk
#

yeah that was my original intent

#

just to confirm

#

ty ❤️

upper elk
#

@drifting basin tried it out creating a sample function similar to the one you provided:

func (m *DaggerStructureTest) AssertFoo(ctx context.Context, container *Container, options struct {
    // +optional
    ExpectedOutput *string
    // +optional
    ExpectedError  *string
    // +optional
    ExpectedExitCode *int
    // +optional
    DisableCache *bool
}) (bool, error) {
    return true, nil
} 

When i run dagger develop i get the following error:

Stderr:
Error: generate code: template: alias.go.tmpl:74:3: executing "_dagger.gen.go/alias.go.tmpl" at <ModuleMainSrc>: error calling ModuleMainSrc: failed to parse method AssertFoo: nested structs are not supported
drifting basin
#

Yeah, try this:

func (m *DaggerStructureTest) AssertFoo(ctx context.Context, opts struct {
    Container *Container,
    // +optional
    ExpectedOutput *string
    // +optional
    ExpectedError  *string
    // +optional
    ExpectedExitCode *int
    // +optional
    DisableCache *bool
}) (bool, error) {
    return true, nil
} 
#

context.Context is not a normal function argument, so it stays where it is.