Add some usage of strongly-typed maps to the index program.

This doesn't really feel like progress to me though. Going to let
it rattle around in my head a bit more before going further.
This commit is contained in:
Aaron Boodman
2015-07-22 15:51:07 -07:00
parent 6db73c518e
commit e39d74543d
4 changed files with 146 additions and 12 deletions

View File

@@ -30,9 +30,21 @@ func main() {
types.NewString("elem"), pitch,
)
stringPitchListMap := types.NewMap(
types.NewString("$type"), types.NewString("noms.MapDef"),
types.NewString("key"), types.NewString("string"),
types.NewString("value"), pitchList,
)
stringStringMap := types.NewMap(
types.NewString("$type"), types.NewString("noms.MapDef"),
types.NewString("key"), types.NewString("string"),
types.NewString("value"), types.NewString("string"),
)
f, err := os.OpenFile(*outFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
defer f.Close()
dbg.Chk.NoError(err)
ng := nomgen.New(f)
ng.WriteGo(pitchList, "main")
ng.WriteGo("main", pitch, pitchList, stringPitchListMap, stringStringMap)
}

View File

@@ -85,11 +85,11 @@ func processInning(m types.Map) map[string][]Pitch {
return pitchCounts
}
func getIndex(input types.List) types.Map {
func getIndex(input types.List) StringPitchListMap {
// Walk through the list in inputDataset and basically switch
// on the top-level key to know if it's an inning or a pitcher.
pitchCounts := types.NewMap()
pitchers := types.NewMap()
pitchCounts := NewStringPitchListMap()
pitchers := NewStringStringMap()
for i := uint64(0); i < input.Len(); i++ {
m := input.Get(i).(types.Map)
if key := types.NewString("inning"); m.Has(key) {
@@ -97,9 +97,9 @@ func getIndex(input types.List) types.Map {
id := types.NewString(idStr)
pitches := NewPitchList()
if pitchCounts.Has(id) {
pitches = PitchListFromVal(pitchCounts.Get(id))
pitches = pitchCounts.Get(id)
}
pitchCounts = pitchCounts.Set(id, pitches.Append(p...).NomsValue())
pitchCounts = pitchCounts.Set(id, pitches.Append(p...))
}
} else if key := types.NewString("Player"); m.Has(key) {
id, name := processPitcher(m.Get(key).(types.Map))
@@ -109,8 +109,8 @@ func getIndex(input types.List) types.Map {
}
}
namedPitchCounts := types.NewMap()
pitchCounts.Iter(func(id, p types.Value) (stop bool) {
namedPitchCounts := NewStringPitchListMap()
pitchCounts.Iter(func(id types.String, p PitchList) (stop bool) {
if pitchers.Has(id) {
namedPitchCounts = namedPitchCounts.Set(pitchers.Get(id), p)
} else {
@@ -142,9 +142,9 @@ func main() {
inputDataset := dataset.NewDataset(dataStore, *inputID)
outputDataset := dataset.NewDataset(dataStore, *outputID)
input := inputDataset.Heads().Any().Value().(types.List)
input := types.ListFromVal(inputDataset.Heads().Any().Value())
output := getIndex(input)
outputDataset.Commit(datas.NewCommitSet().Insert(
datas.NewCommit().SetParents(outputDataset.Heads().NomsValue()).SetValue(output)))
datas.NewCommit().SetParents(outputDataset.Heads().NomsValue()).SetValue(output.NomsValue())))
}

View File

@@ -76,6 +76,66 @@ func (l PitchList) fromElemSlice(p []Pitch) []types.Value {
return r
}
// StringStringMap
type StringStringMap struct {
m types.Map
}
type StringStringMapIterCallback (func(k types.String, v types.String) (stop bool))
func NewStringStringMap() StringStringMap {
return StringStringMap{types.NewMap()}
}
func StringStringMapFromVal(p types.Value) StringStringMap {
return StringStringMap{p.(types.Map)}
}
func (m StringStringMap) NomsValue() types.Map {
return m.m
}
func (m StringStringMap) Equals(p StringStringMap) bool {
return m.m.Equals(p.m)
}
func (m StringStringMap) Ref() ref.Ref {
return m.m.Ref()
}
func (m StringStringMap) Empty() bool {
return m.m.Empty()
}
func (m StringStringMap) Len() uint64 {
return m.m.Len()
}
func (m StringStringMap) Has(p types.String) bool {
return m.m.Has(p)
}
func (m StringStringMap) Get(p types.String) types.String {
return types.StringFromVal(m.m.Get(p))
}
func (m StringStringMap) Set(k types.String, v types.String) StringStringMap {
return StringStringMapFromVal(m.m.Set(k, v))
}
// TODO: Implement SetM?
func (m StringStringMap) Remove(p types.String) StringStringMap {
return StringStringMapFromVal(m.m.Remove(p))
}
func (m StringStringMap) Iter(cb StringStringMapIterCallback) {
m.m.Iter(func(k, v types.Value) bool {
return cb(types.StringFromVal(k), types.StringFromVal(v))
})
}
// Pitch
type Pitch struct {
@@ -119,3 +179,63 @@ func (s Pitch) SetX(p types.Float64) Pitch {
return PitchFromVal(s.m.Set(types.NewString("X"), p))
}
// StringPitchListMap
type StringPitchListMap struct {
m types.Map
}
type StringPitchListMapIterCallback (func(k types.String, v PitchList) (stop bool))
func NewStringPitchListMap() StringPitchListMap {
return StringPitchListMap{types.NewMap()}
}
func StringPitchListMapFromVal(p types.Value) StringPitchListMap {
return StringPitchListMap{p.(types.Map)}
}
func (m StringPitchListMap) NomsValue() types.Map {
return m.m
}
func (m StringPitchListMap) Equals(p StringPitchListMap) bool {
return m.m.Equals(p.m)
}
func (m StringPitchListMap) Ref() ref.Ref {
return m.m.Ref()
}
func (m StringPitchListMap) Empty() bool {
return m.m.Empty()
}
func (m StringPitchListMap) Len() uint64 {
return m.m.Len()
}
func (m StringPitchListMap) Has(p types.String) bool {
return m.m.Has(p)
}
func (m StringPitchListMap) Get(p types.String) PitchList {
return PitchListFromVal(m.m.Get(p))
}
func (m StringPitchListMap) Set(k types.String, v PitchList) StringPitchListMap {
return StringPitchListMapFromVal(m.m.Set(k, v.NomsValue()))
}
// TODO: Implement SetM?
func (m StringPitchListMap) Remove(p types.String) StringPitchListMap {
return StringPitchListMapFromVal(m.m.Remove(p))
}
func (m StringPitchListMap) Iter(cb StringPitchListMapIterCallback) {
m.m.Iter(func(k, v types.Value) bool {
return cb(types.StringFromVal(k), PitchListFromVal(v))
})
}

View File

@@ -32,10 +32,12 @@ func New(w io.Writer) NG {
return NG{w: w, written: types.NewSet(), toWrite: types.NewSet()}
}
func (ng *NG) WriteGo(val types.Map, pkg string) {
func (ng *NG) WriteGo(pkg string, val ...types.Map) {
headerTmpl.Execute(ng.w, struct{ PackageName string }{pkg})
ng.addType(val)
for _, v := range val {
ng.addType(v)
}
for !ng.toWrite.Empty() {
t := ng.toWrite.Any()