Closed staceysalamon-aiven closed 4 months ago
Here's the example to give you an idea:
type EntityType int
const (
Resource EntityType = iota
DataSource
)
func (et EntityType) String() string {
return [...]string{"resource", "datasource"}[et]
}
type AvailabilityType int
const (
Beta AvailabilityType = iota + 1
Limited
)
type DescriptionBuilder struct {
entityType EntityType
mainText string
availabilityType AvailabilityType
}
func (db *DescriptionBuilder) EntityType(entityType EntityType) *DescriptionBuilder {
db.entityType = entityType
return db
}
func (db *DescriptionBuilder) MainText(mainText string) *DescriptionBuilder {
db.mainText = mainText
return db
}
func (db *DescriptionBuilder) AvailabilityType(availabilityType AvailabilityType) *DescriptionBuilder {
db.availabilityType = availabilityType
return db
}
func (db *DescriptionBuilder) Build() string {
var builder strings.Builder
if db.mainText != "" {
builder.WriteString(db.mainText)
if db.availabilityType != 0 {
builder.WriteString(" ")
}
}
commonPart := `
**This %[1]s is in the %[2]s stage and may change without notice.** %[3]s
the ` + "`PROVIDER_AIVEN_ENABLE_BETA`" + ` environment variable to use the %[1]s.`
switch db.availabilityType {
case Beta:
builder.WriteString(fmt.Sprintf(
commonPart,
db.entityType.String(),
"beta",
"Set",
))
case Limited:
builder.WriteString(fmt.Sprintf(
commonPart,
db.entityType.String(),
"limited availability",
"\nTo enable this feature, contact the [sales team](mailto:sales@aiven.io). After it's enabled, set",
))
}
return builder.String()
}
Usage:
new(DescriptionBuilder).
// Usage of `EntityType` is optional. It defaults to `Resource`,
// so it could be omitted for them.
EntityType(DataSource).
MainText("Main text.").
AvailabilityType(Beta).
Build()
I would recommend to either put it in userconfig
directly, or create a nested package there, e.g. desc
, and put it there.
Edit: never mind, looks like the existing builder is still used somewhere. Let's adjust it, I've prepared a PR to do that: https://github.com/aiven/terraform-provider-aiven/pull/1648. Let's get it merged first, and then we can rebase your branch when it's in the main one.
About this change—what it does