Skip to content

Commit

Permalink
Move IdentityAttributes into separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanck committed Feb 17, 2025
1 parent 7d4baaf commit 39a29d4
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 57 deletions.
30 changes: 0 additions & 30 deletions internal/configs/configschema/decoder_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,19 +183,6 @@ func (b *Block) DecoderSpec() hcldec.Spec {
return ret
}

func (b *IdentityAttributes) DecoderSpec() hcldec.Spec {
ret := hcldec.ObjectSpec{}
if b == nil {
return ret
}

for name, attrS := range *b {
ret[name] = attrS.decoderSpec(name)
}

return ret
}

func (a *Attribute) decoderSpec(name string) hcldec.Spec {
if a == nil || (a.Type == cty.NilType && a.NestedType == nil) {
panic("Invalid attribute schema: schema is nil.")
Expand All @@ -218,23 +205,6 @@ func (a *Attribute) decoderSpec(name string) hcldec.Spec {
return ret
}

func (a *IdentityAttribute) decoderSpec(name string) hcldec.Spec {
if a == nil || a.Type == cty.NilType {
panic("Invalid attribute schema: schema is nil.")
}

ret := &hcldec.AttrSpec{Name: name}
ret.Type = a.Type
// When dealing with IdentityAttribute we expect every attribute to be required.
// This is generally true for all communication between providers and Terraform.
// For import, we allow the user to only specify a subset of the attributes, where
// RequiredForImport attributes are required and OptionalForImport attributes are optional.
// The validation for this will rely on a separate spec.
ret.Required = true

return ret
}

// listOptionalAttrsFromObject is a helper function which does *not* recurse
// into NestedType Attributes, because the optional types for each of those will
// belong to their own cty.Object definitions. It is used in other functions
Expand Down
65 changes: 65 additions & 0 deletions internal/configs/configschema/identity_schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1

package configschema

import (
"github.com/hashicorp/hcl/v2/hcldec"
"github.com/zclconf/go-cty/cty"
)

// IdentityAttributes represents a set of attributes in an identity schema
type IdentityAttributes map[string]*IdentityAttribute

// IdentityAttribute represents a single attribute in an identity schema
type IdentityAttribute struct {
// Type is a type specification that the attribute's value must conform to.
Type cty.Type

Description string

RequiredForImport bool
OptionalForImport bool
}

func (ia *IdentityAttributes) ImpliedType() cty.Type {
return ia.specType().WithoutOptionalAttributesDeep()
}

func (ia *IdentityAttributes) specType() cty.Type {
if ia == nil {
return cty.EmptyObject
}

return hcldec.ImpliedType(ia.DecoderSpec())
}

func (ia *IdentityAttributes) DecoderSpec() hcldec.Spec {
ret := hcldec.ObjectSpec{}
if ia == nil {
return ret
}

for name, attrS := range *ia {
ret[name] = attrS.decoderSpec(name)
}

return ret
}

func (ia *IdentityAttribute) decoderSpec(name string) hcldec.Spec {
if ia == nil || ia.Type == cty.NilType {
panic("Invalid attribute schema: schema is nil.")
}

ret := &hcldec.AttrSpec{Name: name}
ret.Type = ia.Type
// When dealing with IdentityAttribute we expect every attribute to be required.
// This is generally true for all communication between providers and Terraform.
// For import, we allow the user to only specify a subset of the attributes, where
// RequiredForImport attributes are required and OptionalForImport attributes are optional.
// The validation for this will rely on a separate spec.
ret.Required = true

return ret
}
15 changes: 0 additions & 15 deletions internal/configs/configschema/implied_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,6 @@ func (b *Block) specType() cty.Type {
return hcldec.ImpliedType(b.DecoderSpec())
}

func (b *IdentityAttributes) ImpliedType() cty.Type {
return b.specType().WithoutOptionalAttributesDeep()
}

// specType returns the cty.Type used for decoding a configuration
// block using the receiving block schema. This is the type used internally by
// hcldec to decode configuration.
func (b *IdentityAttributes) specType() cty.Type {
if b == nil {
return cty.EmptyObject
}

return hcldec.ImpliedType(b.DecoderSpec())
}

// ContainsSensitive returns true if any of the attributes of the receiving
// block or any of its descendant blocks are marked as sensitive.
//
Expand Down
12 changes: 0 additions & 12 deletions internal/configs/configschema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,6 @@ type Attribute struct {
WriteOnly bool
}

type IdentityAttributes map[string]*IdentityAttribute

type IdentityAttribute struct {
// Type is a type specification that the attribute's value must conform to.
Type cty.Type

Description string

RequiredForImport bool
OptionalForImport bool
}

// Object represents the embedding of a structural object inside an Attribute.
type Object struct {
// Attributes describes the nested attributes which may appear inside the
Expand Down

0 comments on commit 39a29d4

Please sign in to comment.