Index: mojom/mojom_parser/generators/common/common.go |
diff --git a/mojom/mojom_parser/generators/common/common.go b/mojom/mojom_parser/generators/common/common.go |
new file mode 100644 |
index 0000000000000000000000000000000000000000..829442b192ff1975126abbb178ec7ef264214080 |
--- /dev/null |
+++ b/mojom/mojom_parser/generators/common/common.go |
@@ -0,0 +1,60 @@ |
+// Copyright 2016 The Chromium 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 common |
+ |
+import ( |
+ "mojom/mojom_parser/generated/mojom_files" |
+) |
+ |
+// common groups together functions which make it easier for generators |
+// implemented in go to implement the same interface. |
+ |
+// GeneratorConfig is used to specify configuration for a generator. |
+type GeneratorConfig interface { |
+ // FileGraph returns a parsed MojomFileGraph. |
+ FileGraph() *mojom_files.MojomFileGraph |
+ |
+ // OutputDir returns an absolute path in which generator output must be |
+ // written. |
+ OutputDir() string |
+ |
+ // SrcRootPath returns an absolute path to the root of the source tree. This |
+ // is used to compute the relative path from the root of the source tree to |
+ // mojom files for which output is to be generated. |
+ SrcRootPath() string |
+ |
+ // GenImports returns true if all files in the provided file graph should |
+ // be generated and and false if only the files for which a SpecifiedFileName |
+ // is specified should be generated. |
+ GenImports() bool |
rudominer
2016/02/27 01:28:10
My suggestion for a more complete description. Do
azani
2016/02/29 21:28:32
Done.
|
+} |
+ |
+// Writer is the interface used by the generators to write their output. |
+// It is also made to faciliate testing by allowing generator code to write |
+// to a provided buffer such as a bytes.Buffer. |
+type Writer interface { |
+ WriteString(s string) (n int, err error) |
+} |
+ |
+// writeOutput is the type of the generator function which writes the specified |
+// file's generated code to the writer. |
+type writeOutput func(fileName string, config GeneratorConfig, writer Writer) |
+ |
+// getOutputWriter is the type of the generator function which decides where |
+// the generated code for a particular file is to be written. |
+// This allows generators to decide where they should write their output to. |
+type getOutputWriter func(fileName string, config GeneratorConfig) Writer |
+ |
+// GenerateOutput iterates through the files in the file graph in config and |
rudominer
2016/02/27 01:28:10
config -> |config|
azani
2016/02/29 21:28:32
Done.
|
+// for all the files for which an output should be generated, it uses |
+// writeOutput to write the output to the location specified by getOutputWriter. |
+func GenerateOutput(writeOutput writeOutput, getOutputWriter getOutputWriter, config GeneratorConfig) { |
+ for fileName, file := range config.FileGraph().Files { |
+ if config.GenImports() || *file.SpecifiedFileName != "" { |
+ writer := getOutputWriter(fileName, config) |
+ writeOutput(fileName, config, writer) |
rudominer
2016/02/27 01:28:10
getOutputWriter isn't really helping here and inst
azani
2016/02/29 21:28:32
I prefer this decoupling because it means that it'
|
+ } |
+ } |
+} |