Add schema support for deprecated properties. (#3865)

This is the schema-based version of
https://github.com/pulumi/pulumi-terraform-bridge/pull/96.
This commit is contained in:
Pat Gavlin 2020-02-06 09:29:06 -08:00 committed by GitHub
parent 548996d7ae
commit 0dbbd919b8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 24 deletions

View file

@ -341,6 +341,9 @@ func (pt *plainType) genInputProperty(w io.Writer, prop *schema.Property, indent
fmt.Fprintf(w, "\n")
printComment(w, prop.Comment, indent+" ")
}
if prop.DeprecationMessage != "" {
fmt.Fprintf(w, "%s [Obsolete(@\"%s\")]\n", indent, strings.Replace(prop.DeprecationMessage, `"`, `""`, -1))
}
// Note that we use the backing field type--which is just the property type without any nullable annotation--to
// ensure that the user does not see warnings when initializing these properties using object or collection

View file

@ -181,7 +181,11 @@ func sanitizeComment(str string) string {
return strings.Replace(str, "*/", "*/", -1)
}
func printComment(w io.Writer, comment string, indent string) {
func printComment(w io.Writer, comment, deprecationMessage, indent string) {
if comment == "" && deprecationMessage == "" {
return
}
lines := strings.Split(sanitizeComment(comment), "\n")
for len(lines) > 0 && lines[len(lines)-1] == "" {
lines = lines[:len(lines)-1]
@ -190,20 +194,23 @@ func printComment(w io.Writer, comment string, indent string) {
for _, l := range lines {
fmt.Fprintf(w, "%s * %s\n", indent, l)
}
if deprecationMessage != "" {
if len(lines) > 0 {
fmt.Fprintf(w, "%s * \n", indent)
}
fmt.Fprintf(w, "%s * @deprecated %s\n", indent, deprecationMessage)
}
fmt.Fprintf(w, "%s */\n", indent)
}
func (mod *modContext) genPlainType(w io.Writer, name, comment string, properties []*schema.Property, input, wrapInput, readonly bool, level int) {
indent := strings.Repeat(" ", level)
if comment != "" {
printComment(w, comment, indent)
}
printComment(w, comment, "", indent)
fmt.Fprintf(w, "%sexport interface %s {\n", indent, name)
for _, p := range properties {
if p.Comment != "" {
printComment(w, p.Comment, indent+" ")
}
printComment(w, p.Comment, p.DeprecationMessage, indent+" ")
prefix := ""
if readonly {
@ -315,9 +322,7 @@ func (mod *modContext) genResource(w io.Writer, r *schema.Resource) error {
name := resourceName(r)
// Write the TypeDoc/JSDoc for the resource class
if r.Comment != "" {
printComment(w, r.Comment, "")
}
printComment(w, r.Comment, "", "")
baseType := "CustomResource"
if r.IsProvider {
@ -382,9 +387,7 @@ func (mod *modContext) genResource(w io.Writer, r *schema.Resource) error {
allOptionalInputs = allOptionalInputs && !prop.IsRequired
}
for _, prop := range r.Properties {
if prop.Comment != "" {
printComment(w, prop.Comment, " ")
}
printComment(w, prop.Comment, prop.DeprecationMessage, " ")
// Make a little comment in the code so it's easy to pick out output properties.
var outcomment string
@ -528,9 +531,7 @@ func (mod *modContext) genFunction(w io.Writer, fun *schema.Function) {
name := camel(tokenToName(fun.Token))
// Write the TypeDoc/JSDoc for the data source function.
if fun.Comment != "" {
printComment(w, fun.Comment, "")
}
printComment(w, fun.Comment, "", "")
if fun.DeprecationMessage != "" {
fmt.Fprintf(w, "/** @deprecated %s */\n", fun.DeprecationMessage)
@ -751,9 +752,7 @@ func (mod *modContext) genConfig(w io.Writer, variables []*schema.Property) erro
getfunc = fmt.Sprintf("getObject<%s>", mod.typeString(p.Type, false, false, false))
}
if p.Comment != "" {
printComment(w, p.Comment, "")
}
printComment(w, p.Comment, "", "")
configFetch := fmt.Sprintf("__config.%s(\"%s\")", getfunc, p.Name)
if p.DefaultValue != nil {

View file

@ -194,6 +194,8 @@ type Property struct {
DefaultValue *DefaultValue
// IsRequired is true if the property must always be populated.
IsRequired bool
// DeprecationMessage indicates whether or not the property is deprecated.
DeprecationMessage string
// Language specifies additional language-specific data about the property.
Language map[string]json.RawMessage
}
@ -334,6 +336,8 @@ type PropertySpec struct {
Default interface{} `json:"default,omitempty"`
// DefautSpec contains additional information aboout the property's default value, if any.
DefaultInfo *DefaultSpec `json:"defaultInfo,omitempty"`
// DeprecationMessage indicates whether or not the property is deprecated.
DeprecationMessage string `json:"deprecationMessage,omitempty"`
// Language specifies additional language-specific data about the property.
Language map[string]json.RawMessage `json:"language,omitempty"`
}
@ -703,11 +707,12 @@ func (t *types) bindProperties(properties map[string]PropertySpec, required []st
}
p := &Property{
Name: name,
Comment: spec.Description,
Type: typ,
DefaultValue: dv,
Language: spec.Language,
Name: name,
Comment: spec.Description,
Type: typ,
DefaultValue: dv,
DeprecationMessage: spec.DeprecationMessage,
Language: spec.Language,
}
propertyMap[name], result = p, append(result, p)