From 38b0630a2a48a9332211cd0106cfb5116255ad90 Mon Sep 17 00:00:00 2001 From: Dan Willemsen Date: Mon, 6 Jul 2015 12:29:57 -0700 Subject: [PATCH] Add deep Copy() for parser types Change-Id: I94bca46d6d30da683cacb0f6ea1fdf26a5a46bc8 --- parser/parser.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/parser/parser.go b/parser/parser.go index a4282a1..6027275 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -507,6 +507,13 @@ type Expression struct { Pos scanner.Position } +func (e *Expression) Copy() *Expression { + ret := *e + ret.Args[0] = e.Args[0].Copy() + ret.Args[1] = e.Args[1].Copy() + return &ret +} + func (e *Expression) String() string { return fmt.Sprintf("(%s %c %s)@%d:%s", e.Args[0].String(), e.Operator, e.Args[1].String(), e.Pos.Offset, e.Pos) @@ -563,6 +570,15 @@ type Module struct { RbracePos scanner.Position } +func (m *Module) Copy() *Module { + ret := *m + ret.Properties = make([]*Property, len(m.Properties)) + for i := range m.Properties { + ret.Properties[i] = m.Properties[i].Copy() + } + return &ret +} + func (m *Module) String() string { propertyStrings := make([]string, len(m.Properties)) for i, property := range m.Properties { @@ -582,6 +598,12 @@ type Property struct { Pos scanner.Position } +func (p *Property) Copy() *Property { + ret := *p + ret.Value = p.Value.Copy() + return &ret +} + func (p *Property) String() string { return fmt.Sprintf("%s@%d:%s: %s", p.Name, p.Pos.Offset, p.Pos, p.Value) } @@ -607,6 +629,26 @@ type Value struct { EndPos scanner.Position } +func (p Value) Copy() Value { + ret := p + if p.MapValue != nil { + ret.MapValue = make([]*Property, len(p.MapValue)) + for i := range p.MapValue { + ret.MapValue[i] = p.MapValue[i].Copy() + } + } + if p.ListValue != nil { + ret.ListValue = make([]Value, len(p.ListValue)) + for i := range p.ListValue { + ret.ListValue[i] = p.ListValue[i].Copy() + } + } + if p.Expression != nil { + ret.Expression = p.Expression.Copy() + } + return ret +} + func (p Value) String() string { var s string if p.Variable != "" {