Mutation method updated to return new copy

with value node (pointer) and value (non pointer) returned.
This commit is contained in:
Dave Shanley
2022-09-13 09:32:08 -04:00
parent a2b7119af7
commit aa4422fa74
4 changed files with 167 additions and 177 deletions

View File

@@ -40,8 +40,10 @@ func (n NodeReference[T]) GenerateMapKey() string {
return fmt.Sprintf("%d:%d", n.ValueNode.Line, n.ValueNode.Column)
}
func (n NodeReference[T]) Mutate(value T) {
func (n NodeReference[T]) Mutate(value T) NodeReference[T] {
n.ValueNode.Value = fmt.Sprintf("%v", value)
n.Value = value
return n
}
func (n ValueReference[T]) IsEmpty() bool {
@@ -60,8 +62,10 @@ func (n KeyReference[T]) GenerateMapKey() string {
return fmt.Sprintf("%d:%d", n.KeyNode.Line, n.KeyNode.Column)
}
func (n ValueReference[T]) Mutate(value T) {
func (n ValueReference[T]) Mutate(value T) ValueReference[T] {
n.ValueNode.Value = fmt.Sprintf("%v", value)
n.Value = value
return n
}
func IsCircular(node *yaml.Node, idx *index.SpecIndex) bool {

View File

@@ -25,6 +25,28 @@ func TestNodeReference_GenerateMapKey(t *testing.T) {
assert.Equal(t, "22:23", nr.GenerateMapKey())
}
func TestNodeReference_Mutate(t *testing.T) {
nr := new(NodeReference[string])
nr.ValueNode = &yaml.Node{
Line: 22,
Column: 23,
}
n := nr.Mutate("nice one!")
assert.Equal(t, "nice one!", n.Value)
assert.Equal(t, "nice one!", nr.ValueNode.Value)
}
func TestValueReference_Mutate(t *testing.T) {
nr := new(ValueReference[string])
nr.ValueNode = &yaml.Node{
Line: 22,
Column: 23,
}
n := nr.Mutate("nice one!")
assert.Equal(t, "nice one!", n.Value)
assert.Equal(t, "nice one!", nr.ValueNode.Value)
}
func TestValueReference_IsEmpty(t *testing.T) {
nr := new(ValueReference[string])
assert.True(t, nr.IsEmpty())

View File

@@ -160,7 +160,12 @@ func TestDocument_Serialize_JSON_Modified(t *testing.T) {
v3Doc, _ := doc.BuildV3Document()
v3Doc.Model.Info.GoLow().Title.Mutate("The magic API - but now, altered!")
// eventually this will be encapsulated up high.
// mutation does not replace low model, eventually pointers will be used.
newTitle := v3Doc.Model.Info.GoLow().Title.Mutate("The magic API - but now, altered!")
v3Doc.Model.Info.GoLow().Title = newTitle
assert.Equal(t, "The magic API - but now, altered!", v3Doc.Model.Info.GoLow().Title.Value)
serial, err := doc.Serialize()
assert.NoError(t, err)

View File

@@ -1,41 +0,0 @@
package main
//func main() {
//
// testData := `openapi: 3.0.1
//info:
// title: this is a title
// description: this is a description
//tags:
// - name: Tag A
// description: cake
// x-hack: true
// - name: Tag B
// description: coffee
// x-code: hack`
//
// data := []byte(testData)
// _ = ioutil.WriteFile("sample.yaml", data, 0664)
//
// info, _ := datamodel.ExtractSpecInfo(data)
// lowDoc, err := low.CreateDocument(info)
// if len(err) > 0 {
// for e := range err {
// fmt.Printf("%e\n", err[e])
// }
// return
// }
// highDoc := high.NewDocument(lowDoc)
//
// highDoc.Info.GoLow().Title.ValueNode.Value = "let's hack this"
// //highDoc.Tags[0].SetName("We are a new name now")
// //highDoc.Tags[0].SetDescription("and a new description")
//
// //newTag := lowDoc.AddTag()
// //fmt.Println(newTag)
// modified, _ := yaml.Marshal(info.RootNode)
// fmt.Println(string(modified))
//
// os.Remove("sample.yaml")
//
//}