Skip to main content

State as Functions

Object state can be exposed as a Dagger Function, without having to create a getter function explicitly. Depending on the language used, this state is exposed using struct fields (Go), object attributes (Python) or object properties (TypeScript).

Dagger only exposes a struct's public fields; private fields will not be exposed.

Here's an example where one struct field is exposed as a Dagger Function, while the other is not:

package main

import "fmt"

type MyModule struct {
// The greeting to use
// +default="Hello"
Greeting string
// Who to greet
// +private
// +default="World"
Name string
}

func New(
// The greeting to use
// +default="Hello"
greeting string,
// Who to greet
// +default="World"
name string,
) *MyModule {
return &MyModule{
Greeting: greeting,
Name: name,
}
}

// Return the greeting message
func (m *MyModule) Message() string {
str := fmt.Sprintf("%s, %s!", m.Greeting, m.Name)
return str
}

Confirm with dagger call --help that only the greeting function was created, with name remaining only a constructor argument:

FUNCTIONS
greeting The greeting to use
message Return the greeting message

ARGUMENTS
--greeting string The greeting to use (default "Hello")
--name string Who to greet (default "World")