From 072a3a79e10a55112afb52ba78c0975b7ec5f8db Mon Sep 17 00:00:00 2001 From: James Harris Date: Sat, 11 Mar 2023 20:05:14 +1000 Subject: [PATCH] Change `variable.TypedSpec` to use a pointer receiver. --- variable/registry.go | 2 +- variable/spec.go | 26 +++++++++++++------------- variable/specbuilder.go | 4 ++-- variable/variable.go | 2 +- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/variable/registry.go b/variable/registry.go index d68910a..1b23249 100644 --- a/variable/registry.go +++ b/variable/registry.go @@ -59,7 +59,7 @@ var DefaultRegistry = Registry{ // Register registers a new variable. func Register[T any, Option RegisterOption]( - spec TypedSpec[T], + spec *TypedSpec[T], options ...Option, ) *OfType[T] { opts := registerOptions{ diff --git a/variable/spec.go b/variable/spec.go index 9f1dc9d..001370c 100644 --- a/variable/spec.go +++ b/variable/spec.go @@ -68,45 +68,45 @@ type TypedSpec[T any] struct { } // Name returns the name of the variable. -func (s TypedSpec[T]) Name() string { +func (s *TypedSpec[T]) Name() string { return s.name } // Description returns a human-readable description of the variable. -func (s TypedSpec[T]) Description() string { +func (s *TypedSpec[T]) Description() string { return s.desc } // Schema returns the schema that applies to the variable's value. -func (s TypedSpec[T]) Schema() Schema { +func (s *TypedSpec[T]) Schema() Schema { return s.schema } // Default returns the string representation of the default value. -func (s TypedSpec[T]) Default() (Literal, bool) { +func (s *TypedSpec[T]) Default() (Literal, bool) { return maybe.Map(s.def, valueOf[T].Canonical).Get() } // IsRequired returns true if the application MUST have a value for this // variable (even if it is fulfilled by a default value). -func (s TypedSpec[T]) IsRequired() bool { +func (s *TypedSpec[T]) IsRequired() bool { return s.required } // IsSensitive returns true if the variable's value contains sensitive // information. -func (s TypedSpec[T]) IsSensitive() bool { +func (s *TypedSpec[T]) IsSensitive() bool { return s.sensitive } // IsDeprecated returns true if the variable is deprecated. -func (s TypedSpec[T]) IsDeprecated() bool { +func (s *TypedSpec[T]) IsDeprecated() bool { return s.deprecated } // Constraints returns a list of additional constraints on the variable's // value. -func (s TypedSpec[T]) Constraints() []Constraint { +func (s *TypedSpec[T]) Constraints() []Constraint { constraints := make([]Constraint, len(s.constraints)) for i, c := range s.constraints { constraints[i] = c @@ -115,18 +115,18 @@ func (s TypedSpec[T]) Constraints() []Constraint { } // Examples returns a list of examples of valid values. -func (s TypedSpec[T]) Examples() []Example { +func (s *TypedSpec[T]) Examples() []Example { return s.examples } // Documentation returns a list of chunks of documentation text. -func (s TypedSpec[T]) Documentation() []Documentation { +func (s *TypedSpec[T]) Documentation() []Documentation { return s.docs } // CheckConstraints returns an error if v does not satisfy any one of the // specification's constraints. -func (s TypedSpec[T]) CheckConstraints(v T) ConstraintError { +func (s *TypedSpec[T]) CheckConstraints(v T) ConstraintError { for _, c := range s.constraints { if err := c.Check(v); err != nil { return err @@ -140,7 +140,7 @@ func (s TypedSpec[T]) CheckConstraints(v T) ConstraintError { // // It returns an error if v does not meet the specification's constraints or // marshaling fails at the schema level. -func (s TypedSpec[T]) Marshal(v T) (Literal, error) { +func (s *TypedSpec[T]) Marshal(v T) (Literal, error) { if err := s.CheckConstraints(v); err != nil { return Literal{}, err } @@ -152,7 +152,7 @@ func (s TypedSpec[T]) Marshal(v T) (Literal, error) { // // It returns an error if v does not meet the specification's constraints or // unmarshaling fails at the schema level. -func (s TypedSpec[T]) Unmarshal(v Literal) (T, Literal, error) { +func (s *TypedSpec[T]) Unmarshal(v Literal) (T, Literal, error) { n, err := s.schema.Unmarshal(v) if err != nil { return n, Literal{}, err diff --git a/variable/specbuilder.go b/variable/specbuilder.go index f61a410..ff68e55 100644 --- a/variable/specbuilder.go +++ b/variable/specbuilder.go @@ -109,14 +109,14 @@ func (b *TypedSpecBuilder[T]) Documentation() DocumentationBuilder { } // Done builds the specification and registers the variable. -func (b *TypedSpecBuilder[T]) Done(schema TypedSchema[T]) TypedSpec[T] { +func (b *TypedSpecBuilder[T]) Done(schema TypedSchema[T]) *TypedSpec[T] { b.spec.schema = schema if err := b.finalize(); err != nil { panic(err.Error()) } - return b.spec + return &b.spec } func (b *TypedSpecBuilder[T]) finalize() error { diff --git a/variable/variable.go b/variable/variable.go index 8e829df..6819a64 100644 --- a/variable/variable.go +++ b/variable/variable.go @@ -25,7 +25,7 @@ type Any interface { // OfType is an environment variable depicted by type T. type OfType[T any] struct { - spec TypedSpec[T] + spec *TypedSpec[T] env Environment once sync.Once