Repository: ariga/atlas
Pull request #2829 (2024-06-02 07:54:54)

schemahcl: change WithPos to set hcl.Range
Range holds also the filename. 


Patch:
diff --git a/schemahcl/schemahcl.go b/schemahcl/schemahcl.go
index 7c7c2ab3..abc101db 100644
--- a/schemahcl/schemahcl.go
+++ b/schemahcl/schemahcl.go
@@ -592,7 +592,7 @@ func (s *State) toAttrs(ctx *hcl.EvalContext, vr SchemaValidator, hclAttrs hclsy
 		}
 		at := &Attr{K: hclAttr.Name}
 		if s.config.withPos {
-			at.SetPos(&hclAttr.SrcRange.Start)
+			at.SetRange(&hclAttr.SrcRange)
 		}
 		switch t := value.Type(); {
 		case isRef(value):
@@ -706,7 +706,7 @@ func (s *State) toResource(ctx *hcl.EvalContext, vr SchemaValidator, block *hcls
 	}
 	spec = &Resource{Type: block.Type}
 	if s.config.withPos {
-		spec.SetPos(&block.TypeRange.Start)
+		spec.SetRange(&block.TypeRange)
 	}
 	switch len(block.Labels) {
 	case 0:
diff --git a/schemahcl/spec.go b/schemahcl/spec.go
index 49ef9644..505eda05 100644
--- a/schemahcl/spec.go
+++ b/schemahcl/spec.go
@@ -25,14 +25,14 @@ type (
 		Type      string
 		Attrs     []*Attr
 		Children  []*Resource
-		pos       *hcl.Pos
+		rang      *hcl.Range
 	}
 
 	// Attr is an attribute of a Resource.
 	Attr struct {
-		K   string
-		V   cty.Value
-		pos *hcl.Pos
+		K    string
+		V    cty.Value
+		rang *hcl.Range
 	}
 
 	// Ref implements Value and represents a reference to another Resource.
@@ -383,26 +383,26 @@ func (a *Attr) Bools() (vs []bool, err error) {
 	return vs, nil
 }
 
-// SetPos sets the position of this attribute.
-func (a *Attr) SetPos(p *hcl.Pos) {
-	a.pos = p
+// SetRange sets the range of this attribute.
+func (a *Attr) SetRange(p *hcl.Range) {
+	a.rang = p
 }
 
-// Pos returns the attribute position on the
+// Range returns the attribute range on the
 // file, or nil if it is not set.
-func (a *Attr) Pos() *hcl.Pos {
-	return a.pos
+func (a *Attr) Range() *hcl.Range {
+	return a.rang
 }
 
-// SetPos sets the position of this resource.
-func (r *Resource) SetPos(p *hcl.Pos) {
-	r.pos = p
+// SetRange sets the range of this resource.
+func (r *Resource) SetRange(p *hcl.Range) {
+	r.rang = p
 }
 
-// Pos returns the resource position on the
+// Range returns the resource range on the
 // file, or nil if it is not set.
-func (r *Resource) Pos() *hcl.Pos {
-	return r.pos
+func (r *Resource) Range() *hcl.Range {
+	return r.rang
 }
 
 // Resource returns the first child Resource by its type and reports whether it was found.


Test patch:
diff --git a/schemahcl/schemahcl_test.go b/schemahcl/schemahcl_test.go
index 7a33d82b..0bb30e52 100644
--- a/schemahcl/schemahcl_test.go
+++ b/schemahcl/schemahcl_test.go
@@ -1091,11 +1091,11 @@ baz = 1
 	require.NoError(t, New(WithPos()).EvalBytes(b, &doc, nil))
 	at, ok := doc.Extra.Attr("baz")
 	require.True(t, ok)
-	require.Equal(t, 8, at.Pos().Line)
+	require.Equal(t, 8, at.Range().Start.Line)
 	rs := doc.Extra.Resources("foo")
 	require.Len(t, rs, 2)
-	require.Equal(t, 2, rs[0].Pos().Line)
-	require.Equal(t, 3, rs[1].Pos().Line)
-	require.Equal(t, 4, rs[1].Children[0].Pos().Line)
-	require.Equal(t, 5, rs[1].Children[0].Attrs[0].Pos().Line)
+	require.Equal(t, 2, rs[0].Range().Start.Line)
+	require.Equal(t, 3, rs[1].Range().Start.Line)
+	require.Equal(t, 4, rs[1].Children[0].Range().Start.Line)
+	require.Equal(t, 5, rs[1].Children[0].Attrs[0].Range().Start.Line)
 }