blob: c6ab87c477d8813d54347523f2ef4d715c135600 [file] [log] [blame]
// Copyright 2021 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package codifier
import "log"
// StoreKeyValue stores the given value under the given given. The key must not
// exists already. The value is a string. If it's a list, use StoreKeyValueList.
func (p *Proc) StoreKeyValue(key, value string) *Proc {
if p == nil {
log.Println(" StoreKeyValue() error: nil receiver")
return nil
}
_, ok := p.Store[key].(string)
if ok {
log.Printf(" StoreKeyValue() error: key %q already present in store", key)
return nil
}
p.Store[key] = value
return p
}
// Value returns the value stored under the given key else empty string if not
// present.
func (p *Proc) Value(key string) string {
if p == nil {
log.Println(" Value() error: nil receiver")
return ""
}
value, ok := p.Store[key].(string)
if !ok {
log.Printf(" Value(): key %q not present in store", key)
return ""
}
return value
}
// ExtractValue is an operator that extracts a value by key from the current
// replacement, if present. The expected k/v format is `k="v"` with single or
// double quotes and optional spaces. The key and the extracted value are
// stored.
func (p *Proc) ExtractValue(key string) *Proc {
if p == nil {
log.Println(" ExtractValue() error: nil receiver")
return nil
}
value, err := getValue(p.replacement, key)
if err != nil {
log.Printf(" ExtractValue() error: %v", err)
return nil
}
p.Store[key] = value
return p
}
// ExtractList is an operator that extracts a list by name from the Proc's
// replacement. The argument is the list name. The extracted list is stored
// under the name.
func (p *Proc) ExtractList(listName string) *Proc {
if p == nil {
log.Println(" ExtractList() error: nil receiver")
return nil
}
list, err := extractNamedList(p.replacement, listName)
if err != nil {
log.Printf(" ExtractList() error: %v", err)
return nil
}
if list == nil {
log.Printf(" ExtractList() error: found no list named %q", listName)
return nil
}
log.Printf("ExtractList(): list %q has %d items", listName, len(list))
p.Store[listName] = list
return p
}
// ListValue returns the list stored under the given key else nil if not
// present.
func (p *Proc) ListValue(key string) []string {
if p == nil {
log.Println(" ListValue() error: nil receiver")
return nil
}
v := p.Store[key]
if v == nil {
return nil
}
return v.([]string)
}
// lastItemInList returns the last item stored under the given listKey, else a
// blank string.
func (p *Proc) lastItemInList(listKey string) string {
if p == nil {
log.Println(" lastItemInList() error: nil receiver")
return ""
}
storedList, ok := p.Store[listKey].([]string)
if !ok {
log.Printf(" lastItemInList(): list %q not present in store", listKey)
return ""
}
if len(storedList) == 0 {
return ""
}
return storedList[len(storedList)-1]
}
// copyStore copies the given Proc's store into it's store. If a key already
// exists in p's store, return nil.
func (p *Proc) copyStore(q *Proc) *Proc {
if p == nil {
log.Println(" copyStore() error: nil receiver")
return nil
}
for k, v := range q.Store {
_, ok := p.Store[k]
if ok {
log.Printf(" copyStore() error: key %q exists in destination store", k)
return nil
}
p.Store[k] = v
}
return p
}
// ClearStore copies the given Proc's store into it's store. If a key already
// exists in p's store, return nil.
func (p *Proc) ClearStore() *Proc {
if p == nil {
log.Println(" ClearStore() error: nil receiver")
return nil
}
p.Store = make(map[string]interface{})
return p
}