44package marks
55
66import (
7+ "github.com/hashicorp/hcl/v2"
78 "github.com/zclconf/go-cty/cty"
89)
910
@@ -17,16 +18,30 @@ func (m valueMark) GoString() string {
1718}
1819
1920// Has returns true if and only if the cty.Value has the given mark.
20- func Has (val cty.Value , mark valueMark ) bool {
21- return val .HasMark (mark )
21+ func Has (val cty.Value , mark interface {}) bool {
22+ switch m := mark .(type ) {
23+ case valueMark :
24+ return val .HasMark (m )
25+
26+ // For value marks Has returns true if a mark of the type is present
27+ case DeprecationMark :
28+ for depMark := range val .Marks () {
29+ if _ , ok := depMark .(DeprecationMark ); ok {
30+ return true
31+ }
32+ }
33+ return false
34+ default :
35+ panic ("Unknown mark type" )
36+ }
2237}
2338
2439// Contains returns true if the cty.Value or any any value within it contains
2540// the given mark.
26- func Contains (val cty.Value , mark valueMark ) bool {
41+ func Contains (val cty.Value , mark interface {} ) bool {
2742 ret := false
2843 cty .Walk (val , func (_ cty.Path , v cty.Value ) (bool , error ) {
29- if v . HasMark ( mark ) {
44+ if Has ( v , mark ) {
3045 ret = true
3146 return false , nil
3247 }
@@ -35,6 +50,33 @@ func Contains(val cty.Value, mark valueMark) bool {
3550 return ret
3651}
3752
53+ func FilterDeprecationMarks (marks cty.ValueMarks ) []DeprecationMark {
54+ depMarks := []DeprecationMark {}
55+ for mark := range marks {
56+ if d , ok := mark .(DeprecationMark ); ok {
57+ depMarks = append (depMarks , d )
58+ }
59+ }
60+ return depMarks
61+ }
62+
63+ func GetDeprecationMarks (val cty.Value ) []DeprecationMark {
64+ _ , marks := val .UnmarkDeep ()
65+ return FilterDeprecationMarks (marks )
66+
67+ }
68+
69+ func RemoveDeprecationMarks (val cty.Value ) cty.Value {
70+ newVal , marks := val .Unmark ()
71+ for mark := range marks {
72+ if _ , ok := mark .(DeprecationMark ); ok {
73+ continue
74+ }
75+ newVal = newVal .Mark (mark )
76+ }
77+ return newVal
78+ }
79+
3880// Sensitive indicates that this value is marked as sensitive in the context of
3981// Terraform.
4082const Sensitive = valueMark ("Sensitive" )
@@ -51,3 +93,22 @@ const Ephemeral = valueMark("Ephemeral")
5193// another value's type. This is part of the implementation of the console-only
5294// `type` function.
5395const TypeType = valueMark ("TypeType" )
96+
97+ type DeprecationMark struct {
98+ Message string
99+ Origin * hcl.Range
100+ }
101+
102+ func (d DeprecationMark ) GoString () string {
103+ return "marks.deprecation<" + d .Message + ">"
104+ }
105+
106+ // Empty deprecation mark for usage in marks.Has / Contains / etc
107+ var Deprecation = NewDeprecation ("" , nil )
108+
109+ func NewDeprecation (message string , origin * hcl.Range ) DeprecationMark {
110+ return DeprecationMark {
111+ Message : message ,
112+ Origin : origin ,
113+ }
114+ }
0 commit comments