<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/main/main.go
blob: 31e46c6434b96bff30364df09f1cabdab5d5860a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main

import (
	"fmt"
	"os"
	"bufio"
)

type PathSegment interface {}
type Path []PathSegment

type TerminalValue int
const (
	ArrayBegin TerminalValue = iota
	ArrayEnd
	MapBegin
	MapEnd
)
type ValueNull struct {}
type ValueBool bool
type ValueNumber float64
type ValueString string

type WalkValue interface {}

type Program []Command

type ProgramState struct {
	space WalkItem
	in chan WalkItem
	out chan WalkItem
	program []Command
}

type StringSegmentPathFilterAST struct {
	index string
}
func (ast StringSegmentPathFilterAST) compileWith(next PathFilterState) PathFilterState {
	return StringSegmentPathFilter {
		index: ast.index,
		next: next,
	}
}

type RepeatPathFilterAST struct {
	content PathFilterAST
}
func (ast RepeatPathFilterAST) compileWith(next PathFilterState) PathFilterState {
	nextGroup := &GroupPathFilter{}
	repeatStart := ast.content.compileWith(nextGroup)
	nextGroup.filters = []PathFilterState{next, repeatStart}
	return nextGroup
}

type SequencePathFilterAST struct {
	sequence []PathFilterAST
}
func (ast SequencePathFilterAST) compileWith(next PathFilterState) PathFilterState {
	for i := len(ast.sequence) - 1; i >= 0; i -= 1 {
		next = ast.sequence[i].compileWith(next)
	}
	return next
}

type AnySegmentPathFilterAST struct {}
func (ast AnySegmentPathFilterAST) compileWith(next PathFilterState) PathFilterState {
	return AnySegmentPathFilter{next: next}
}

type PathFilterAST interface {
	compileWith(PathFilterState) PathFilterState
}

func compilePathFilterAST(ast PathFilterAST) PathFilter {
	return PathFilter{
		initial: ast.compileWith(NonePathFilter{}),
	}
}

func main() {
	if len(os.Args) < 2 {
		fmt.Println("Missing program arg")
		return
	}
	//input := os.Args[1]
	//tokens := Lex(input)
	//program := Parse(tokens)
	
	stdin := bufio.NewReader(os.Stdin)
	dataStream := Json(stdin)
	
	var allRemainingPathFilter AnySegmentPathFilter
	{
		g := GroupPathFilter {
			filters: []PathFilterState{NonePathFilter{}},
		}
		allRemainingPathFilter = AnySegmentPathFilter {
			next: PathFilterState(&g),
		}
		g.filters = append(g.filters, PathFilterState(&allRemainingPathFilter))
	}

	state := ProgramState {
		in: dataStream,
		out: make(chan WalkItem),
		program: []Command {
			FilteredCommand {
				filter: compilePathFilterAST(
					StringSegmentPathFilterAST {"people"},
				),
				command: PrintValueCommand{},
			},
			FilteredCommand {
				filter: compilePathFilterAST(
					SequencePathFilterAST {
						[]PathFilterAST{
							StringSegmentPathFilterAST {"people"},
							AnySegmentPathFilterAST{},
							StringSegmentPathFilterAST {"age"},
						},
					},
				),
				command: PrintValueCommand{},
			},
		},
	}
	
	go func () {
		for walkItem := range dataStream {
			state.space = walkItem
			for _, cmd := range state.program {
				cmd.exec(&state)
			}
		}
		close(state.out)
	}()
	
	JsonOut(state.out)
}