pulumi/pkg/ast/stable.go

90 lines
1.5 KiB
Go
Raw Normal View History

// Copyright 2016 Marapongo, Inc. All rights reserved.
package ast
import (
"sort"
)
func StableClusters(cs Clusters) []string {
sorted := make([]string, 0, len(cs))
for c := range cs {
sorted = append(sorted, c)
}
sort.Strings(sorted)
return sorted
}
func StableDependencies(ds Dependencies) []Ref {
sorted := make(Refs, 0, len(ds))
for d := range ds {
sorted = append(sorted, d)
}
sort.Sort(sorted)
return sorted
}
func StableBoundDependencies(ds BoundDependencies) []Ref {
sorted := make(Refs, 0, len(ds))
for d := range ds {
sorted = append(sorted, d)
}
sort.Sort(sorted)
return sorted
}
func StableProperties(ps Properties) []string {
sorted := make([]string, 0, len(ps))
for p := range ps {
sorted = append(sorted, p)
}
sort.Strings(sorted)
return sorted
}
func StableServices(ss ServiceMap) []Name {
sorted := make(Names, 0, len(ss))
for s := range ss {
sorted = append(sorted, s)
Retain unrecognized service properties During unmarshaling, the default behavior of the stock Golang JSON marshaler, and consequently the YAML one we used which mimics its behavior, is to toss away unrecognized properties. This isn't what we want for two reasons: First, we want to issue errors/warnings on unrecognized fields to aid in diagnostics; we will set aside some extensible section for 3rd parties to use. This is not addressed in this change, however. Second, and more pertinent, is that we need to retain unrecognized fields for certain types like services, which are extensible by default. Until golang/go#6213 is addressed -- imminent, it seems -- we will have to do a somewhat hacky workaround to this problem. This change contains what I consider to be the "least bad" in that we won't introduce a lot of performance overhead, and just have to deal with the slight annoyance of the ast.Services node type containing both Public/Private *and* PublicUntyped/PrivateUntyped fields alongside one another. The marshaler dumps property bags into the *Untyped fields, and the parsetree analyzer expands them out into a structured ast.Service type. Subsequent passes can then ignore the *Untyped fields altogether. Note that this would cause some marshaling funkiness if we ever wanted to remarshal the mutated ASTs back into JSON/YAML. Since we don't do that right now, however, I've not made any attempt to keep the two pairs in synch. Post-parsetree analyzer, we literally just forget about the *Untyped guys.
2016-11-19 18:01:23 +01:00
}
sort.Sort(sorted)
return sorted
Retain unrecognized service properties During unmarshaling, the default behavior of the stock Golang JSON marshaler, and consequently the YAML one we used which mimics its behavior, is to toss away unrecognized properties. This isn't what we want for two reasons: First, we want to issue errors/warnings on unrecognized fields to aid in diagnostics; we will set aside some extensible section for 3rd parties to use. This is not addressed in this change, however. Second, and more pertinent, is that we need to retain unrecognized fields for certain types like services, which are extensible by default. Until golang/go#6213 is addressed -- imminent, it seems -- we will have to do a somewhat hacky workaround to this problem. This change contains what I consider to be the "least bad" in that we won't introduce a lot of performance overhead, and just have to deal with the slight annoyance of the ast.Services node type containing both Public/Private *and* PublicUntyped/PrivateUntyped fields alongside one another. The marshaler dumps property bags into the *Untyped fields, and the parsetree analyzer expands them out into a structured ast.Service type. Subsequent passes can then ignore the *Untyped fields altogether. Note that this would cause some marshaling funkiness if we ever wanted to remarshal the mutated ASTs back into JSON/YAML. Since we don't do that right now, however, I've not made any attempt to keep the two pairs in synch. Post-parsetree analyzer, we literally just forget about the *Untyped guys.
2016-11-19 18:01:23 +01:00
}
func StableUntypedServices(ss UntypedServiceMap) []Name {
sorted := make(Names, 0, len(ss))
for s := range ss {
sorted = append(sorted, s)
}
sort.Sort(sorted)
return sorted
}
type Names []Name
func (s Names) Len() int {
return len(s)
}
func (s Names) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s Names) Less(i, j int) bool {
return s[i] < s[j]
}
type Refs []Ref
func (s Refs) Len() int {
return len(s)
}
func (s Refs) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s Refs) Less(i, j int) bool {
return s[i] < s[j]
}