diff --git a/encoder.go b/encoder.go index 31544d9..7fc6e83 100644 --- a/encoder.go +++ b/encoder.go @@ -3,8 +3,10 @@ package main import ( "bytes" "log" + "os" "path/filepath" "text/template" + "time" "github.com/golang/protobuf/protoc-gen-go/descriptor" "github.com/golang/protobuf/protoc-gen-go/plugin" @@ -20,9 +22,13 @@ type GenericTemplateBasedEncoder struct { } type Ast struct { - Filename string - Service *descriptor.ServiceDescriptorProto - File *descriptor.FileDescriptorProto + BuildDate time.Time `json:"build-date"` + BuildHostname string `json:"build-hostname"` + BuildUser string `json:"build-user"` + Debug bool `json:"debug"` + File *descriptor.FileDescriptorProto `json:"file"` + Filename string `json:"filename"` + Service *descriptor.ServiceDescriptorProto `json:"service"` } func NewGenericTemplateBasedEncoder(templateDir string, service *descriptor.ServiceDescriptorProto, file *descriptor.FileDescriptorProto, debug bool) (e *GenericTemplateBasedEncoder) { @@ -79,10 +85,14 @@ func (e *GenericTemplateBasedEncoder) buildContent(templateFilename string) (str return "", err } + hostname, _ := os.Hostname() ast := Ast{ - Filename: templateFilename, - Service: e.service, - File: e.file, + BuildDate: time.Now(), + BuildHostname: hostname, + BuildUser: os.Getenv("USER"), + File: e.file, + Filename: templateFilename, + Service: e.service, } buffer := new(bytes.Buffer) diff --git a/examples/dummy/output/export.json b/examples/dummy/output/export.json index 28672e2..dd87bd0 100644 --- a/examples/dummy/output/export.json +++ b/examples/dummy/output/export.json @@ -1 +1,839 @@ -{"Filename":"export.json.tmpl","Service":{"name":"DummyService","method":[{"name":"Hhh","input_type":".dummy.Dummy1","output_type":".dummy.Dummy2","options":{}},{"name":"Iii","input_type":".dummy.Dummy2","output_type":".dummy.Dummy1","options":{}}]},"File":{"name":"dummy.proto","package":"dummy","message_type":[{"name":"Dummy1","field":[{"name":"aaa","number":1,"label":1,"type":2,"json_name":"aaa"},{"name":"bbb","number":2,"label":1,"type":9,"json_name":"bbb"},{"name":"ccc","number":3,"label":1,"type":5,"json_name":"ccc"},{"name":"ddd","number":4,"label":1,"type":3,"json_name":"ddd"},{"name":"eee","number":5,"label":3,"type":9,"json_name":"eee"}]},{"name":"Dummy2","field":[{"name":"fff","number":1,"label":1,"type":2,"json_name":"fff"},{"name":"ggg","number":2,"label":1,"type":11,"type_name":".dummy.Dummy1","json_name":"ggg"}]},{"name":"Dummy3"}],"service":[{"name":"DummyService","method":[{"name":"Hhh","input_type":".dummy.Dummy1","output_type":".dummy.Dummy2","options":{}},{"name":"Iii","input_type":".dummy.Dummy2","output_type":".dummy.Dummy1","options":{}}]}],"source_code_info":{"location":[{"span":[0,0,22,1]},{"path":[12],"span":[0,0,18]},{"path":[2],"span":[2,8,13]},{"path":[4,0],"span":[4,0,10,1]},{"path":[4,0,1],"span":[4,8,14]},{"path":[4,0,2,0],"span":[5,2,16]},{"path":[4,0,2,0,4],"span":[5,2,4,16]},{"path":[4,0,2,0,5],"span":[5,2,7]},{"path":[4,0,2,0,1],"span":[5,8,11]},{"path":[4,0,2,0,3],"span":[5,14,15]},{"path":[4,0,2,1],"span":[6,2,17]},{"path":[4,0,2,1,4],"span":[6,2,5,16]},{"path":[4,0,2,1,5],"span":[6,2,8]},{"path":[4,0,2,1,1],"span":[6,9,12]},{"path":[4,0,2,1,3],"span":[6,15,16]},{"path":[4,0,2,2],"span":[7,2,16]},{"path":[4,0,2,2,4],"span":[7,2,6,17]},{"path":[4,0,2,2,5],"span":[7,2,7]},{"path":[4,0,2,2,1],"span":[7,8,11]},{"path":[4,0,2,2,3],"span":[7,14,15]},{"path":[4,0,2,3],"span":[8,2,16]},{"path":[4,0,2,3,4],"span":[8,2,7,16]},{"path":[4,0,2,3,5],"span":[8,2,7]},{"path":[4,0,2,3,1],"span":[8,8,11]},{"path":[4,0,2,3,3],"span":[8,14,15]},{"path":[4,0,2,4],"span":[9,2,26]},{"path":[4,0,2,4,4],"span":[9,2,10]},{"path":[4,0,2,4,5],"span":[9,11,17]},{"path":[4,0,2,4,1],"span":[9,18,21]},{"path":[4,0,2,4,3],"span":[9,24,25]},{"path":[4,1],"span":[12,0,15,1]},{"path":[4,1,1],"span":[12,8,14]},{"path":[4,1,2,0],"span":[13,2,16]},{"path":[4,1,2,0,4],"span":[13,2,12,16]},{"path":[4,1,2,0,5],"span":[13,2,7]},{"path":[4,1,2,0,1],"span":[13,8,11]},{"path":[4,1,2,0,3],"span":[13,14,15]},{"path":[4,1,2,1],"span":[14,2,17]},{"path":[4,1,2,1,4],"span":[14,2,13,16]},{"path":[4,1,2,1,6],"span":[14,2,8]},{"path":[4,1,2,1,1],"span":[14,9,12]},{"path":[4,1,2,1,3],"span":[14,15,16]},{"path":[4,2],"span":[17,0,17]},{"path":[4,2,1],"span":[17,8,14]},{"path":[6,0],"span":[19,0,22,1]},{"path":[6,0,1],"span":[19,8,20]},{"path":[6,0,2,0],"span":[20,2,37]},{"path":[6,0,2,0,1],"span":[20,6,9]},{"path":[6,0,2,0,2],"span":[20,10,16]},{"path":[6,0,2,0,3],"span":[20,27,33]},{"path":[6,0,2,1],"span":[21,2,37]},{"path":[6,0,2,1,1],"span":[21,6,9]},{"path":[6,0,2,1,2],"span":[21,10,16]},{"path":[6,0,2,1,3],"span":[21,27,33]}]},"syntax":"proto3"}} \ No newline at end of file +{ + "build-date": "2016-11-07T09:21:48.495773574+01:00", + "build-hostname": "moul-rose.local", + "build-user": "moul", + "debug": false, + "file": { + "name": "dummy.proto", + "package": "dummy", + "message_type": [ + { + "name": "Dummy1", + "field": [ + { + "name": "aaa", + "number": 1, + "label": 1, + "type": 2, + "json_name": "aaa" + }, + { + "name": "bbb", + "number": 2, + "label": 1, + "type": 9, + "json_name": "bbb" + }, + { + "name": "ccc", + "number": 3, + "label": 1, + "type": 5, + "json_name": "ccc" + }, + { + "name": "ddd", + "number": 4, + "label": 1, + "type": 3, + "json_name": "ddd" + }, + { + "name": "eee", + "number": 5, + "label": 3, + "type": 9, + "json_name": "eee" + } + ] + }, + { + "name": "Dummy2", + "field": [ + { + "name": "fff", + "number": 1, + "label": 1, + "type": 2, + "json_name": "fff" + }, + { + "name": "ggg", + "number": 2, + "label": 1, + "type": 11, + "type_name": ".dummy.Dummy1", + "json_name": "ggg" + } + ] + }, + { + "name": "Dummy3" + } + ], + "service": [ + { + "name": "DummyService", + "method": [ + { + "name": "Hhh", + "input_type": ".dummy.Dummy1", + "output_type": ".dummy.Dummy2", + "options": {} + }, + { + "name": "Iii", + "input_type": ".dummy.Dummy2", + "output_type": ".dummy.Dummy1", + "options": {} + } + ] + } + ], + "source_code_info": { + "location": [ + { + "span": [ + 0, + 0, + 22, + 1 + ] + }, + { + "path": [ + 12 + ], + "span": [ + 0, + 0, + 18 + ] + }, + { + "path": [ + 2 + ], + "span": [ + 2, + 8, + 13 + ] + }, + { + "path": [ + 4, + 0 + ], + "span": [ + 4, + 0, + 10, + 1 + ] + }, + { + "path": [ + 4, + 0, + 1 + ], + "span": [ + 4, + 8, + 14 + ] + }, + { + "path": [ + 4, + 0, + 2, + 0 + ], + "span": [ + 5, + 2, + 16 + ] + }, + { + "path": [ + 4, + 0, + 2, + 0, + 4 + ], + "span": [ + 5, + 2, + 4, + 16 + ] + }, + { + "path": [ + 4, + 0, + 2, + 0, + 5 + ], + "span": [ + 5, + 2, + 7 + ] + }, + { + "path": [ + 4, + 0, + 2, + 0, + 1 + ], + "span": [ + 5, + 8, + 11 + ] + }, + { + "path": [ + 4, + 0, + 2, + 0, + 3 + ], + "span": [ + 5, + 14, + 15 + ] + }, + { + "path": [ + 4, + 0, + 2, + 1 + ], + "span": [ + 6, + 2, + 17 + ] + }, + { + "path": [ + 4, + 0, + 2, + 1, + 4 + ], + "span": [ + 6, + 2, + 5, + 16 + ] + }, + { + "path": [ + 4, + 0, + 2, + 1, + 5 + ], + "span": [ + 6, + 2, + 8 + ] + }, + { + "path": [ + 4, + 0, + 2, + 1, + 1 + ], + "span": [ + 6, + 9, + 12 + ] + }, + { + "path": [ + 4, + 0, + 2, + 1, + 3 + ], + "span": [ + 6, + 15, + 16 + ] + }, + { + "path": [ + 4, + 0, + 2, + 2 + ], + "span": [ + 7, + 2, + 16 + ] + }, + { + "path": [ + 4, + 0, + 2, + 2, + 4 + ], + "span": [ + 7, + 2, + 6, + 17 + ] + }, + { + "path": [ + 4, + 0, + 2, + 2, + 5 + ], + "span": [ + 7, + 2, + 7 + ] + }, + { + "path": [ + 4, + 0, + 2, + 2, + 1 + ], + "span": [ + 7, + 8, + 11 + ] + }, + { + "path": [ + 4, + 0, + 2, + 2, + 3 + ], + "span": [ + 7, + 14, + 15 + ] + }, + { + "path": [ + 4, + 0, + 2, + 3 + ], + "span": [ + 8, + 2, + 16 + ] + }, + { + "path": [ + 4, + 0, + 2, + 3, + 4 + ], + "span": [ + 8, + 2, + 7, + 16 + ] + }, + { + "path": [ + 4, + 0, + 2, + 3, + 5 + ], + "span": [ + 8, + 2, + 7 + ] + }, + { + "path": [ + 4, + 0, + 2, + 3, + 1 + ], + "span": [ + 8, + 8, + 11 + ] + }, + { + "path": [ + 4, + 0, + 2, + 3, + 3 + ], + "span": [ + 8, + 14, + 15 + ] + }, + { + "path": [ + 4, + 0, + 2, + 4 + ], + "span": [ + 9, + 2, + 26 + ] + }, + { + "path": [ + 4, + 0, + 2, + 4, + 4 + ], + "span": [ + 9, + 2, + 10 + ] + }, + { + "path": [ + 4, + 0, + 2, + 4, + 5 + ], + "span": [ + 9, + 11, + 17 + ] + }, + { + "path": [ + 4, + 0, + 2, + 4, + 1 + ], + "span": [ + 9, + 18, + 21 + ] + }, + { + "path": [ + 4, + 0, + 2, + 4, + 3 + ], + "span": [ + 9, + 24, + 25 + ] + }, + { + "path": [ + 4, + 1 + ], + "span": [ + 12, + 0, + 15, + 1 + ] + }, + { + "path": [ + 4, + 1, + 1 + ], + "span": [ + 12, + 8, + 14 + ] + }, + { + "path": [ + 4, + 1, + 2, + 0 + ], + "span": [ + 13, + 2, + 16 + ] + }, + { + "path": [ + 4, + 1, + 2, + 0, + 4 + ], + "span": [ + 13, + 2, + 12, + 16 + ] + }, + { + "path": [ + 4, + 1, + 2, + 0, + 5 + ], + "span": [ + 13, + 2, + 7 + ] + }, + { + "path": [ + 4, + 1, + 2, + 0, + 1 + ], + "span": [ + 13, + 8, + 11 + ] + }, + { + "path": [ + 4, + 1, + 2, + 0, + 3 + ], + "span": [ + 13, + 14, + 15 + ] + }, + { + "path": [ + 4, + 1, + 2, + 1 + ], + "span": [ + 14, + 2, + 17 + ] + }, + { + "path": [ + 4, + 1, + 2, + 1, + 4 + ], + "span": [ + 14, + 2, + 13, + 16 + ] + }, + { + "path": [ + 4, + 1, + 2, + 1, + 6 + ], + "span": [ + 14, + 2, + 8 + ] + }, + { + "path": [ + 4, + 1, + 2, + 1, + 1 + ], + "span": [ + 14, + 9, + 12 + ] + }, + { + "path": [ + 4, + 1, + 2, + 1, + 3 + ], + "span": [ + 14, + 15, + 16 + ] + }, + { + "path": [ + 4, + 2 + ], + "span": [ + 17, + 0, + 17 + ] + }, + { + "path": [ + 4, + 2, + 1 + ], + "span": [ + 17, + 8, + 14 + ] + }, + { + "path": [ + 6, + 0 + ], + "span": [ + 19, + 0, + 22, + 1 + ] + }, + { + "path": [ + 6, + 0, + 1 + ], + "span": [ + 19, + 8, + 20 + ] + }, + { + "path": [ + 6, + 0, + 2, + 0 + ], + "span": [ + 20, + 2, + 37 + ] + }, + { + "path": [ + 6, + 0, + 2, + 0, + 1 + ], + "span": [ + 20, + 6, + 9 + ] + }, + { + "path": [ + 6, + 0, + 2, + 0, + 2 + ], + "span": [ + 20, + 10, + 16 + ] + }, + { + "path": [ + 6, + 0, + 2, + 0, + 3 + ], + "span": [ + 20, + 27, + 33 + ] + }, + { + "path": [ + 6, + 0, + 2, + 1 + ], + "span": [ + 21, + 2, + 37 + ] + }, + { + "path": [ + 6, + 0, + 2, + 1, + 1 + ], + "span": [ + 21, + 6, + 9 + ] + }, + { + "path": [ + 6, + 0, + 2, + 1, + 2 + ], + "span": [ + 21, + 10, + 16 + ] + }, + { + "path": [ + 6, + 0, + 2, + 1, + 3 + ], + "span": [ + 21, + 27, + 33 + ] + } + ] + }, + "syntax": "proto3" + }, + "filename": "export.json.tmpl", + "service": { + "name": "DummyService", + "method": [ + { + "name": "Hhh", + "input_type": ".dummy.Dummy1", + "output_type": ".dummy.Dummy2", + "options": {} + }, + { + "name": "Iii", + "input_type": ".dummy.Dummy2", + "output_type": ".dummy.Dummy1", + "options": {} + } + ] + } +} \ No newline at end of file diff --git a/examples/dummy/templates/export.json.tmpl b/examples/dummy/templates/export.json.tmpl index 9706d90..b7645f6 100644 --- a/examples/dummy/templates/export.json.tmpl +++ b/examples/dummy/templates/export.json.tmpl @@ -1 +1 @@ -{{ . | json }} \ No newline at end of file +{{ . | prettyjson }} \ No newline at end of file