Skip to content

Commit

Permalink
Merge 7c6cbaf into eea6140
Browse files Browse the repository at this point in the history
  • Loading branch information
t0yv0 authored Jul 6, 2023
2 parents eea6140 + 7c6cbaf commit 5cb3634
Show file tree
Hide file tree
Showing 27 changed files with 474 additions and 1,117 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ require (
github.com/hashicorp/go-sockaddr v1.0.2 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/hashicorp/terraform-plugin-go v0.14.3 // indirect
github.com/hashicorp/terraform-plugin-go v0.14.3
github.com/hashicorp/terraform-plugin-log v0.7.0 // indirect
github.com/hashicorp/terraform-registry-address v0.1.0 // indirect
github.com/hashicorp/vault/api v1.8.2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions pf/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ require (
github.com/Masterminds/sprig/v3 v3.2.2 // indirect
github.com/gedex/inflector v0.0.0-20170307190818-16278e9db813 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
)

require (
Expand Down
3 changes: 3 additions & 0 deletions pf/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb
github.com/apache/thrift v0.13.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
github.com/apparentlymart/go-cidr v1.1.0 h1:2mAhrMoF+nhXqxTzSZMUzDHkLjmIHC+Zzn4tdgBZjnU=
github.com/apparentlymart/go-cidr v1.1.0/go.mod h1:EBcsNrHc3zQeuaeCeCtQruQm+n9/YjEn/vI25Lg7Gwc=
github.com/apparentlymart/go-dump v0.0.0-20180507223929-23540a00eaa3 h1:ZSTrOEhiM5J5RFxEaFvMZVEAM1KvT1YzbEOwB2EAGjA=
github.com/apparentlymart/go-textseg v1.0.0/go.mod h1:z96Txxhf3xSFMPmb5X/1W05FF/Nj9VFpLOpjS5yuumk=
github.com/apparentlymart/go-textseg/v12 v12.0.0/go.mod h1:S/4uRK2UtaQttw1GenVJEynmyUenKwP++x/+DdGV/Ec=
github.com/apparentlymart/go-textseg/v13 v13.0.0 h1:Y+KvPE1NYz0xl601PVImeQfFyEy6iT90AvPUL1NNfNw=
Expand Down Expand Up @@ -1800,10 +1801,12 @@ github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaU
github.com/vmihailenco/msgpack/v4 v4.3.12 h1:07s4sz9IReOgdikxLTKNbBdqDMLsjPKXwvCazn8G65U=
github.com/vmihailenco/msgpack/v4 v4.3.12/go.mod h1:gborTTJjAo/GWTqqRjrLCn9pgNN+NXzzngzBKDPIqw4=
github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9znI5mJU=
github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
github.com/vmihailenco/tagparser v0.1.1/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
github.com/vmihailenco/tagparser v0.1.2 h1:gnjoVuB/kljJ5wICEEOpx98oXMWPLj22G67Vbd1qPqc=
github.com/vmihailenco/tagparser v0.1.2/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
github.com/vultr/govultr/v2 v2.17.2/go.mod h1:ZFOKGWmgjytfyjeyAdhQlSWwTjh2ig+X49cAp50dzXI=
github.com/willf/bitset v1.1.11-0.20200630133818-d5bec3311243/go.mod h1:RjeCKbqT1RxIR/KWY6phxZiaY1IyutSBfGjNPySAYV4=
github.com/willf/bitset v1.1.11/go.mod h1:83CECat5yLh5zVOf4P1ErAgKA5UDvKtgyUABdr3+MjI=
Expand Down
73 changes: 0 additions & 73 deletions pf/internal/pfutils/attr.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,10 @@
package pfutils

import (
"fmt"

"github.com/hashicorp/terraform-plugin-framework/attr"
dschema "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
pschema "github.com/hashicorp/terraform-plugin-framework/provider/schema"
rschema "github.com/hashicorp/terraform-plugin-framework/resource/schema"

"github.com/hashicorp/terraform-plugin-go/tftypes"
)

// Attr type works around not being able to link to fwschema.Attribute from
Expand All @@ -38,7 +34,6 @@ type Attr interface {
IsNested() bool
Nested() map[string]Attr
NestingMode() NestingMode
HasNestedObject() bool
}

type AttrLike interface {
Expand Down Expand Up @@ -81,15 +76,6 @@ type attrAdapter struct {

var _ Attr = (*attrAdapter)(nil)

func (a *attrAdapter) HasNestedObject() bool {
switch a.NestingMode() {
case NestingModeList, NestingModeMap, NestingModeSet:
return true
default:
return false
}
}

func (a *attrAdapter) IsNested() bool {
return a.nested != nil
}
Expand All @@ -111,62 +97,3 @@ const (
NestingModeSet NestingMode = 3
NestingModeMap NestingMode = 4
)

// Classifies the results of LookupTerraformPath. The only valid cases are:
//
// 1. IsAttr=true, and Attr is set; this means an Attribute was found
// 2. IsBlock=true, and Block is set; this means a Block was found
// 3. IsNestedObject=true; this means the path is a nested object one level down from Attr or Block
// 4. IsMisc=true; this groups all other cases, such as resolving to a simple atomic type as String
//
// All other combinations should not be valid.
type LookupResult struct {
IsAttr bool
IsBlock bool
IsNestedObject bool
IsMisc bool
Attr Attr
Block Block
}

// Drills down a Schema with a given AttributePath to classify what is found at that path, see LookupResult.
func LookupTerraformPath(schema Schema, path *tftypes.AttributePath) (LookupResult, error) {
res, ok, err := tryLookupAttrOrBlock(schema, path)
if err != nil {
return res, err
}
if ok {
return res, nil
}

// Perhaps our parent path is an Attribute or a Block with a nested object, then path is a path to a nested
// object. This is another way to detect, indirectly, if res is a fwschema.NestedBlockObject or
// fwschema.NestedAttributeObject, without relying on reflection.
if parent := path.WithoutLastStep(); parent != nil {
parentRes, parentOk, parentErr := tryLookupAttrOrBlock(schema, parent)
if parentErr == nil && parentOk {
if parentRes.IsAttr && parentRes.Attr.HasNestedObject() {
return LookupResult{IsNestedObject: true}, nil
}
if parentRes.IsBlock && parentRes.Block.HasNestedObject() {
return LookupResult{IsNestedObject: true}, nil
}
}
}

return LookupResult{IsMisc: true}, nil
}

func tryLookupAttrOrBlock(schema Schema, path *tftypes.AttributePath) (LookupResult, bool, error) {
res, remaining, err := tftypes.WalkAttributePath(schema, path)
if err != nil {
return LookupResult{}, false, fmt.Errorf("%v still remains in the path: %w", remaining, err)
}
switch res := res.(type) {
case AttrLike:
return LookupResult{IsAttr: true, Attr: FromAttrLike(res)}, true, nil
case BlockLike:
return LookupResult{IsBlock: true, Block: FromBlockLike(res)}, true, nil
}
return LookupResult{}, false, nil
}
5 changes: 4 additions & 1 deletion pf/internal/pfutils/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ type Block interface {
NestedBlocks() map[string]Block
GetMaxItems() int64
GetMinItems() int64
HasNestedObject() bool
}

type BlockLike interface {
Expand Down Expand Up @@ -145,6 +144,10 @@ func (b *blockAdapter) NestingMode() BlockNestingMode {
return b.nestingMode
}

type NestedBlock interface {
NestingMode() BlockNestingMode
}

type BlockNestingMode uint8

const (
Expand Down
69 changes: 0 additions & 69 deletions pf/internal/pfutils/eq.go

This file was deleted.

Loading

0 comments on commit 5cb3634

Please sign in to comment.