Skip to content

Commit

Permalink
Adding Default Value interfaces (#668)
Browse files Browse the repository at this point in the history
  • Loading branch information
bendbennett committed Feb 10, 2023
1 parent 1be6094 commit 8c30700
Show file tree
Hide file tree
Showing 10 changed files with 324 additions and 0 deletions.
33 changes: 33 additions & 0 deletions resource/schema/defaults/bool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package defaults

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/types"
)

// Bool is a schema default value for types.Bool attributes.
type Bool interface {
Describer

// DefaultBool should set the default value.
DefaultBool(context.Context, BoolRequest, *BoolResponse)
}

type BoolRequest struct {
// Path contains the path of the attribute for setting the
// default value. Use this path for any response diagnostics.
Path path.Path
}

type BoolResponse struct {
// Diagnostics report errors or warnings related to setting the
// default value resource configuration. An empty slice
// indicates success, with no warnings or errors generated.
Diagnostics diag.Diagnostics

// PlanValue is the planned new state for the attribute.
PlanValue types.Bool
}
27 changes: 27 additions & 0 deletions resource/schema/defaults/describer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package defaults

import "context"

// Describer is the common documentation interface for extensible schema
// default value functionality.
type Describer interface {
// Description should describe the default in plain text formatting.
// This information is used by provider logging and provider tooling such
// as documentation generation.
//
// The description should:
// - Begin with a lowercase or other character suitable for the middle of
// a sentence.
// - End without punctuation.
Description(ctx context.Context) string

// MarkdownDescription should describe the default in Markdown
// formatting. This information is used by provider logging and provider
// tooling such as documentation generation.
//
// The description should:
// - Begin with a lowercase or other character suitable for the middle of
// a sentence.
// - End without punctuation.
MarkdownDescription(ctx context.Context) string
}
33 changes: 33 additions & 0 deletions resource/schema/defaults/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Package defaults contains schema default value interfaces and
// request/response implementations. These default value interfaces
// are used by resource/schema and internally in the framework.
// Refer to the typed default packages, such as stringdefault,
// for framework-defined default values that can be used in
// provider-defined schemas.
//
// Each attr.Type has a corresponding {TYPE} interface which
// implements concretely typed Default{TYPE} methods, such as
// StringDefault and DefaultString.
//
// The framework has to choose between default value developers handling a
// concrete framework value type, such as types.Bool, or the framework
// interface for custom value basetypes, such as basetypes.BoolValuable.
//
// In the framework type model, the developer can immediately use the value.
// If the value was associated with a custom type and using the custom value
// type is desired, the developer must use the type's ValueFrom{TYPE} method.
//
// In the custom type model, the developer must always convert to a concrete
// type before using the value unless checking for null or unknown. Since any
// custom type may be passed due to the schema, it is possible, if not likely,
// that unknown concrete types will be passed to the default.
//
// The framework chooses to pass the framework value type. This prevents the
// potential for unexpected runtime panics and simplifies development for
// easier use cases where the framework type is sufficient. More advanced
// developers can choose to call the type's ValueFrom{TYPE} method to get the
// desired custom type in a plan modifier.
//
// Defaults that are not type dependent need to implement all interfaces,
// but can use shared logic to reduce implementation code.
package defaults
33 changes: 33 additions & 0 deletions resource/schema/defaults/float64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package defaults

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/types"
)

// Float64 is a schema default value for types.Float64 attributes.
type Float64 interface {
Describer

// DefaultFloat64 should set the default value.
DefaultFloat64(context.Context, Float64Request, *Float64Response)
}

type Float64Request struct {
// Path contains the path of the attribute for setting the
// default value. Use this path for any response diagnostics.
Path path.Path
}

type Float64Response struct {
// Diagnostics report errors or warnings related to setting the
// default value resource configuration. An empty slice
// indicates success, with no warnings or errors generated.
Diagnostics diag.Diagnostics

// PlanValue is the planned new state for the attribute.
PlanValue types.Float64
}
33 changes: 33 additions & 0 deletions resource/schema/defaults/int64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package defaults

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/types"
)

// Int64 is a schema default value for types.Int64 attributes.
type Int64 interface {
Describer

// DefaultInt64 should set the default value.
DefaultInt64(context.Context, Int64Request, *Int64Response)
}

type Int64Request struct {
// Path contains the path of the attribute for setting the
// default value. Use this path for any response diagnostics.
Path path.Path
}

type Int64Response struct {
// Diagnostics report errors or warnings related to setting the
// default value resource configuration. An empty slice
// indicates success, with no warnings or errors generated.
Diagnostics diag.Diagnostics

// PlanValue is the planned new state for the attribute.
PlanValue types.Int64
}
33 changes: 33 additions & 0 deletions resource/schema/defaults/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package defaults

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/types"
)

// List is a schema default value for types.List attributes.
type List interface {
Describer

// DefaultList should set the default value.
DefaultList(context.Context, ListRequest, *ListResponse)
}

type ListRequest struct {
// Path contains the path of the attribute for setting the
// default value. Use this path for any response diagnostics.
Path path.Path
}

type ListResponse struct {
// Diagnostics report errors or warnings related to setting the
// default value resource configuration. An empty slice
// indicates success, with no warnings or errors generated.
Diagnostics diag.Diagnostics

// PlanValue is the planned new state for the attribute.
PlanValue types.List
}
33 changes: 33 additions & 0 deletions resource/schema/defaults/number.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package defaults

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/types"
)

// Number is a schema default value for types.Number attributes.
type Number interface {
Describer

// DefaultNumber should set the default value.
DefaultNumber(context.Context, NumberRequest, *NumberResponse)
}

type NumberRequest struct {
// Path contains the path of the attribute for setting the
// default value. Use this path for any response diagnostics.
Path path.Path
}

type NumberResponse struct {
// Diagnostics report errors or warnings related to setting the
// default value resource configuration. An empty slice
// indicates success, with no warnings or errors generated.
Diagnostics diag.Diagnostics

// PlanValue is the planned new state for the attribute.
PlanValue types.Number
}
33 changes: 33 additions & 0 deletions resource/schema/defaults/object.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package defaults

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/types"
)

// Object is a schema default value for types.Object attributes.
type Object interface {
Describer

// DefaultObject should set the default value.
DefaultObject(context.Context, ObjectRequest, *ObjectResponse)
}

type ObjectRequest struct {
// Path contains the path of the attribute for setting the
// default value. Use this path for any response diagnostics.
Path path.Path
}

type ObjectResponse struct {
// Diagnostics report errors or warnings related to setting the
// default value resource configuration. An empty slice
// indicates success, with no warnings or errors generated.
Diagnostics diag.Diagnostics

// PlanValue is the planned new state for the attribute.
PlanValue types.Object
}
33 changes: 33 additions & 0 deletions resource/schema/defaults/set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package defaults

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/types"
)

// Set is a schema default value for types.Set attributes.
type Set interface {
Describer

// DefaultSet should set the default value.
DefaultSet(context.Context, SetRequest, *SetResponse)
}

type SetRequest struct {
// Path contains the path of the attribute for setting the
// default value. Use this path for any response diagnostics.
Path path.Path
}

type SetResponse struct {
// Diagnostics report errors or warnings related to setting the
// default value resource configuration. An empty slice
// indicates success, with no warnings or errors generated.
Diagnostics diag.Diagnostics

// PlanValue is the planned new state for the attribute.
PlanValue types.Set
}
33 changes: 33 additions & 0 deletions resource/schema/defaults/string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package defaults

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/types"
)

// String is a schema default value for types.String attributes.
type String interface {
Describer

// DefaultString should String the default value.
DefaultString(context.Context, StringRequest, *StringResponse)
}

type StringRequest struct {
// Path contains the path of the attribute for Stringting the
// default value. Use this path for any response diagnostics.
Path path.Path
}

type StringResponse struct {
// Diagnostics report errors or warnings related to Stringting the
// default value resource configuration. An empty slice
// indicates success, with no warnings or errors generated.
Diagnostics diag.Diagnostics

// PlanValue is the planned new state for the attribute.
PlanValue types.String
}

0 comments on commit 8c30700

Please sign in to comment.