s/Config/Doc

master
Jordan Orelli 10 years ago
parent 4ef77d7b6c
commit bc33c4936e

@ -43,11 +43,11 @@ func to() {
} }
func to_json(n int) { func to_json(n int) {
conf, err := moon.Read(input(n)) doc, err := moon.Read(input(n))
if err != nil { if err != nil {
bail(1, "input error: %s", err) bail(1, "input error: %s", err)
} }
b, err := json.MarshalIndent(conf, "", " ") b, err := json.MarshalIndent(doc, "", " ")
if err != nil { if err != nil {
bail(1, "encode error: %s", err) bail(1, "encode error: %s", err)
} }

@ -1,56 +0,0 @@
package moon
import (
"encoding/json"
"fmt"
)
type Config struct {
items map[string]interface{}
}
func (c *Config) hasKey(key string) bool {
if c.items == nil {
return false
}
_, ok := c.items[key]
return ok
}
func (c *Config) setUnique(key string, value interface{}) error {
if c.hasKey(key) {
return fmt.Errorf("the name %s is already defined in this scope", key)
}
c.set(key, value)
return nil
}
func (c *Config) set(key string, value interface{}) {
if c.items == nil {
c.items = make(map[string]interface{}, 12)
}
c.items[key] = value
}
func (c *Config) Get(key string) interface{} {
if c.items == nil {
return nil
}
return c.items[key]
}
func (c *Config) GetString(key string) string {
v := c.Get(key)
if v == nil {
return ""
}
s, ok := v.(string)
if ok {
return s
}
return ""
}
func (c *Config) MarshalJSON() ([]byte, error) {
return json.Marshal(c.items)
}

@ -0,0 +1,13 @@
package moon
import (
"encoding/json"
)
type Doc struct {
items map[string]interface{}
}
func (d *Doc) MarshalJSON() ([]byte, error) {
return json.Marshal(d.items)
}

@ -8,7 +8,7 @@ import (
const () const ()
func Read(r io.Reader) (*Config, error) { func Read(r io.Reader) (*Doc, error) {
tree, err := parse(r) tree, err := parse(r)
if err != nil { if err != nil {
return nil, err return nil, err
@ -22,7 +22,7 @@ func Read(r io.Reader) (*Config, error) {
delete(ctx, name) delete(ctx, name)
} }
} }
return &Config{items: ctx}, nil return &Doc{items: ctx}, nil
} }
func parse(r io.Reader) (node, error) { func parse(r io.Reader) (node, error) {

Loading…
Cancel
Save