Skip to content
This repository was archived by the owner on Dec 30, 2024. It is now read-only.

Commit 95c0324

Browse files
authored
feat: add deprecated schema (#81)
1 parent 179fd72 commit 95c0324

File tree

3 files changed

+136
-12
lines changed

3 files changed

+136
-12
lines changed

.changelog/80.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:feature
2+
`Deprecated` - Added a new DeprecatedResource Struct in base schema.
3+
```

deprecated.go

Lines changed: 110 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ package superschema
33
import "fmt"
44

55
const (
6-
warningDeprecated = "\n\n ~> **Attribute deprecated** "
6+
warningAttributeDeprecated = "\n\n ~> **Attribute deprecated** "
7+
warningResourceDeprecated = "\n\n !> **Resource deprecated** "
78
)
89

910
// Deprecated is a struct to describe a deprecated attribute.
@@ -64,7 +65,7 @@ func (d *Deprecated) computeDeprecatedDocumentation() string {
6465
return d.MarkdownDeprecationMessage
6566
}
6667

67-
message := warningDeprecated
68+
message := warningAttributeDeprecated
6869

6970
switch {
7071
case d.Renamed:
@@ -108,3 +109,110 @@ func (d *Deprecated) GetDeprecationMessage() string {
108109
func (d *Deprecated) GetMarkdownDeprecationMessage() string {
109110
return d.computeDeprecatedDocumentation()
110111
}
112+
113+
// DeprecatedResource is a struct to describe a deprecated resource or data source.
114+
type DeprecatedResource struct {
115+
// DeprecationMessage is the message to display in the CLI when the user
116+
// attempts to use the deprecated resource.
117+
// This field is required.
118+
DeprecationMessage string
119+
120+
// MarkdownDeprecationMessage is the message to display in the Documentation portal
121+
// when the user attempts to use the deprecated attribute.
122+
// This field is required if ComputeMarkdownDeprecationMessage is false.
123+
MarkdownDeprecationMessage string
124+
125+
// ComputeMarkdownDeprecationMessage is a flag to indicate whether the MarkdownDeprecationMessage
126+
// should be computed from the parameters of the Deprecated struct.
127+
ComputeMarkdownDeprecationMessage bool
128+
129+
// Renamed is a flag to indicate whether the resource or datasource has been renamed.
130+
// Removed is a flag to indicate whether the resource or datasource has been removed.
131+
// One of these fields must be true.
132+
Renamed, Removed bool
133+
134+
// TargetResourceName is the name of the resource that replaces the deprecated resource or data source.
135+
// These fields are required if the resource or data source has been renamed and computeMarkdownDeprecationMessage is true.
136+
TargetResourceName string
137+
138+
// TargetRelease is the release version in which the resource or datasource was deprecated. (e.g. v1.0.0).
139+
// This field is Required.
140+
TargetRelease string
141+
// LinkToIssue is the link to the GitHub issue that describes the deprecation.
142+
// This field is optional.
143+
LinkToIssue string
144+
// LinkToMigrationGuide is the link to the actual resource documentation.
145+
// This field is optional.
146+
LinkToMigrationGuide string
147+
// LinkToNewResourceDoc is the link to the terraform documentation for the resource that replaces the deprecated attribute.
148+
// This field is optional.
149+
LinkToNewResourceDoc string
150+
// LinkToMilestone is the link to the GitHub milestone that describes the release in which the attribute was deprecated.
151+
// This field is optional.
152+
LinkToMilestone string
153+
}
154+
155+
func (d *DeprecatedResource) computeDeprecatedDocumentation(isResource bool) string {
156+
157+
if (!d.ComputeMarkdownDeprecationMessage && d.MarkdownDeprecationMessage == "") || (d.Renamed && d.TargetResourceName == "" && d.ComputeMarkdownDeprecationMessage) || d.TargetRelease == "" {
158+
return ""
159+
}
160+
161+
if d.Removed && d.Renamed {
162+
return ""
163+
}
164+
165+
ressOrData := func() string {
166+
if isResource {
167+
return "resource"
168+
}
169+
return "data source"
170+
}()
171+
172+
message := warningResourceDeprecated + d.MarkdownDeprecationMessage
173+
174+
if d.ComputeMarkdownDeprecationMessage {
175+
switch {
176+
case d.Renamed:
177+
if d.TargetResourceName == "" {
178+
return ""
179+
}
180+
if d.LinkToNewResourceDoc != "" {
181+
message += fmt.Sprintf("The %s has renamed to [`%s`](%s)", ressOrData, d.TargetResourceName, d.LinkToNewResourceDoc)
182+
} else {
183+
message += fmt.Sprintf("The %s has renamed to `%s`", ressOrData, d.TargetResourceName)
184+
}
185+
case d.Removed:
186+
message += fmt.Sprintf("The %s has been removed", ressOrData)
187+
default:
188+
return ""
189+
}
190+
}
191+
192+
if d.LinkToMilestone != "" {
193+
message += fmt.Sprintf(", it will be removed in the version [`%s`](%s) of the provider", d.TargetRelease, d.LinkToMilestone)
194+
} else {
195+
message += fmt.Sprintf(", it will be removed in the version `%s` of the provider", d.TargetRelease)
196+
}
197+
198+
if d.LinkToIssue != "" {
199+
message += fmt.Sprintf(". See the [GitHub issue](%s) for more information.", d.LinkToIssue)
200+
}
201+
202+
return addEndDot(message)
203+
}
204+
205+
// GetDeprecationMessage returns the deprecation message for the attribute.
206+
func (d *DeprecatedResource) GetDeprecationMessage() string {
207+
208+
if d.LinkToMigrationGuide != "" {
209+
return fmt.Sprintf("%s. See the migration guide(%s) for more information.", d.DeprecationMessage, d.LinkToMigrationGuide)
210+
}
211+
212+
return d.DeprecationMessage
213+
}
214+
215+
// GetMarkdownDeprecationMessage returns the markdown deprecation message for the attribute.
216+
func (d *DeprecatedResource) GetMarkdownDeprecationMessage(isResource bool) string {
217+
return d.computeDeprecatedDocumentation(isResource)
218+
}

schema.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ const (
3535
)
3636

3737
type Schema struct {
38-
Deprecated Deprecated
3938
Common SchemaDetails
4039
Resource SchemaDetails
4140
DataSource SchemaDetails
@@ -44,25 +43,34 @@ type Schema struct {
4443

4544
type SchemaDetails struct {
4645
MarkdownDescription string
47-
DeprecationMessage string
46+
Deprecated DeprecatedResource
47+
48+
// Deprecated: Use Deprecated instead.
49+
DeprecationMessage string
4850
}
4951

5052
func (s Schema) GetResource(ctx context.Context) schemaR.Schema {
53+
5154
if s.Resource.MarkdownDescription != "" {
5255
s.Common.MarkdownDescription = addToDescription(s.Common.MarkdownDescription, s.Resource.MarkdownDescription)
5356
}
5457

58+
// * Deprecated is a struct that contains the deprecation message and the target resource name.
5559
if s.Resource.DeprecationMessage != "" {
56-
s.Common.DeprecationMessage = addToDescription(s.Common.MarkdownDescription, s.Resource.DeprecationMessage)
60+
s.Common.DeprecationMessage = s.Resource.DeprecationMessage
61+
}
62+
63+
if s.Resource.Deprecated != (DeprecatedResource{}) {
64+
s.Common.Deprecated = s.Resource.Deprecated
5765
}
5866

59-
if s.Deprecated.DeprecationMessage != "" {
60-
s.Common.DeprecationMessage = addToDescription(s.Common.MarkdownDescription, s.Deprecated.DeprecationMessage)
67+
if s.Common.Deprecated != (DeprecatedResource{}) {
68+
s.Common.MarkdownDescription = addToDescription(s.Common.MarkdownDescription, s.Common.Deprecated.GetMarkdownDeprecationMessage(true))
6169
}
6270

6371
return schemaR.Schema{
6472
MarkdownDescription: s.Common.MarkdownDescription,
65-
DeprecationMessage: s.Common.DeprecationMessage,
73+
DeprecationMessage: s.Common.Deprecated.GetDeprecationMessage(),
6674
Attributes: s.Attributes.process(ctx, resourceT).(map[string]schemaR.Attribute),
6775
}
6876
}
@@ -72,17 +80,22 @@ func (s Schema) GetDataSource(ctx context.Context) schemaD.Schema {
7280
s.Common.MarkdownDescription = addToDescription(s.Common.MarkdownDescription, s.DataSource.MarkdownDescription)
7381
}
7482

83+
// * Deprecated is a struct that contains the deprecation message and the target resource name.
7584
if s.DataSource.DeprecationMessage != "" {
76-
s.Common.DeprecationMessage = addToDescription(s.Common.MarkdownDescription, s.DataSource.DeprecationMessage)
85+
s.Common.DeprecationMessage = s.DataSource.DeprecationMessage
86+
}
87+
88+
if s.DataSource.Deprecated != (DeprecatedResource{}) {
89+
s.Common.Deprecated = s.DataSource.Deprecated
7790
}
7891

79-
if s.Deprecated.DeprecationMessage != "" {
80-
s.Common.DeprecationMessage = addToDescription(s.Common.MarkdownDescription, s.Deprecated.DeprecationMessage)
92+
if s.Common.Deprecated != (DeprecatedResource{}) {
93+
s.Common.MarkdownDescription = addToDescription(s.Common.MarkdownDescription, s.Common.Deprecated.GetMarkdownDeprecationMessage(true))
8194
}
8295

8396
return schemaD.Schema{
8497
MarkdownDescription: s.Common.MarkdownDescription,
85-
DeprecationMessage: s.Common.DeprecationMessage,
98+
DeprecationMessage: s.Common.Deprecated.GetDeprecationMessage(),
8699
Attributes: s.Attributes.process(ctx, dataSourceT).(map[string]schemaD.Attribute),
87100
}
88101
}

0 commit comments

Comments
 (0)