From b53e3074cf3bb780adaf4aced08303c2a5fb254d Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Fri, 14 Feb 2020 18:21:10 -0800 Subject: [PATCH] addrs: Provider.LessThan When making lists of providers (or lists that contain providers) it's helpful to have a canonical ordering in order to produce deterministic results. This ordering has no semantic meaning and is just here for the sake of having a predictable standard. --- addrs/provider.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/addrs/provider.go b/addrs/provider.go index 617596436..18aca141e 100644 --- a/addrs/provider.go +++ b/addrs/provider.go @@ -109,6 +109,23 @@ func (pt Provider) IsZero() bool { return pt == Provider{} } +// LessThan returns true if the receiver should sort before the other given +// address in an ordered list of provider addresses. +// +// This ordering is an arbitrary one just to allow deterministic results from +// functions that would otherwise have no natural ordering. It's subject +// to change in future. +func (pt Provider) LessThan(other Provider) bool { + switch { + case pt.Hostname != other.Hostname: + return pt.Hostname < other.Hostname + case pt.Namespace != other.Namespace: + return pt.Namespace < other.Namespace + default: + return pt.Type < other.Type + } +} + // ParseProviderSourceString parses the source attribute and returns a provider. // This is intended primarily to parse the FQN-like strings returned by // terraform-config-inspect.