Skip to content

Commit

Permalink
feat: add server variable extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
geffersonFerraz committed Aug 28, 2024
1 parent b163092 commit 252e5bb
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
Binary file added datamodel/low/v3/__debug_bin2039468867
Binary file not shown.
1 change: 1 addition & 0 deletions datamodel/low/v3/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func (s *Server) Build(ctx context.Context, keyNode, root *yaml.Node, _ *index.S
variable.Reference = new(low.Reference)
_ = low.BuildModel(varNode, &variable)
variable.Nodes = low.ExtractNodesRecursive(ctx, varNode)
variable.Extensions = low.ExtractExtensions(varNode)
if localKeyNode != nil {
variable.Nodes.Store(localKeyNode.Line, localKeyNode)
}
Expand Down
55 changes: 55 additions & 0 deletions datamodel/low/v3/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,61 @@ variables:
for v := range n.Variables.Value.ValuesFromOldest() {
assert.NotNil(t, v.Value.GetKeyNode())
assert.NotNil(t, v.Value.GetRootNode())
assert.Nil(t, v.Value.GetExtensions())
}
}

func TestServerWithVariableExtension_Build(t *testing.T) {
yml := `url: https://pb33f.io
description: high quality software for developers.
variables:
var1:
default: hello
description: a var
enum: [one, two]
x-transforms:
allowMissing: true`

var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndex(&idxNode)

var n Server
err := low.BuildModel(idxNode.Content[0], &n)
assert.NoError(t, err)
assert.Nil(t, n.GetRootNode())

err = n.Build(context.Background(), nil, idxNode.Content[0], idx)
assert.NoError(t, err)
assert.NotNil(t, n.GetRootNode())
assert.Equal(t, "ec69dfcf68ad8988f3804e170ee6c4a7ad2e4ac51084796eea93168820827546",
low.GenerateHashString(&n))

assert.Equal(t, "https://pb33f.io", n.URL.Value)
assert.Equal(t, "high quality software for developers.", n.Description.Value)

variable := n.FindVariable("var1").Value
assert.Equal(t, "hello", variable.Default.Value)
assert.Equal(t, "a var", variable.Description.Value)

var_extensions := variable.Extensions.First()
assert.Equal(t, "x-transforms", var_extensions.Key().Value)
variable_pair := variable.Extensions.GetPair(var_extensions.Key())
assert.Equal(t, "allowMissing", variable_pair.Value.Value.Content[0].Value)
assert.Equal(t, "true", variable_pair.Value.Value.Content[1].Value)

// test var hash
s := n.FindVariable("var1")
assert.Equal(t, "00eef99ee4a7b746be7b4ccdece59c5a96222c6206f846fafed782c9f3f9b46b",
low.GenerateHashString(s.Value))

assert.Equal(t, 0, orderedmap.Len(n.GetExtensions()))

// check nodes on variables
for v := range n.Variables.Value.ValuesFromOldest() {
assert.NotNil(t, v.Value.GetKeyNode())
assert.NotNil(t, v.Value.GetRootNode())
assert.NotNil(t, v.Value.GetExtensions())
}
}

Expand Down

0 comments on commit 252e5bb

Please sign in to comment.