|
| 1 | +/* |
| 2 | + * ObjectBox Generator - a build time tool for ObjectBox |
| 3 | + * Copyright (C) 2018-2024 ObjectBox Ltd. All rights reserved. |
| 4 | + * https://objectbox.io |
| 5 | + * |
| 6 | + * This file is part of ObjectBox Generator. |
| 7 | + * |
| 8 | + * ObjectBox Generator is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU Affero General Public License as published by |
| 10 | + * the Free Software Foundation, either version 3 of the License, or |
| 11 | + * (at your option) any later version. |
| 12 | + * ObjectBox Generator is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU Affero General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Affero General Public License |
| 18 | + * along with ObjectBox Generator. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +package jsgenerator |
| 22 | + |
| 23 | +import ( |
| 24 | + "bufio" |
| 25 | + "bytes" |
| 26 | + "fmt" |
| 27 | + "path/filepath" |
| 28 | + "strings" |
| 29 | + |
| 30 | + "github.com/objectbox/objectbox-generator/v4/internal/generator" |
| 31 | + "github.com/objectbox/objectbox-generator/v4/internal/generator/flatbuffersc" |
| 32 | + "github.com/objectbox/objectbox-generator/v4/internal/generator/js/templates" |
| 33 | + "github.com/objectbox/objectbox-generator/v4/internal/generator/model" |
| 34 | +) |
| 35 | + |
| 36 | +// JS generator, given a .fbs and an optional *model.json file, is responsible for generating: |
| 37 | +// - objectbox-model.js |
| 38 | +// - sche |
| 39 | +type JSGenerator struct { |
| 40 | + Optional string // std::optional, std::unique_ptr, std::shared_ptr |
| 41 | + EmptyStringAsNull bool |
| 42 | + NaNAsNull bool |
| 43 | +} |
| 44 | + |
| 45 | +// Return the names of the generated JS binding file (only one!) for the given entity file. |
| 46 | +// For example: given a schema.fbs file, outputs schema.obx.fbs. |
| 47 | +func (gen *JSGenerator) BindingFiles(forFile string, options generator.Options) []string { |
| 48 | + |
| 49 | + if len(options.OutPath) > 0 { |
| 50 | + forFile = filepath.Join(options.OutPath, filepath.Base(forFile)) |
| 51 | + } |
| 52 | + var extension = filepath.Ext(forFile) |
| 53 | + var base = forFile[0 : len(forFile)-len(extension)] |
| 54 | + return []string{base + ".obx.js"} |
| 55 | +} |
| 56 | + |
| 57 | +// Return the model filename for the given model JSON file. |
| 58 | +func (gen *JSGenerator) ModelFile(forFile string, options generator.Options) string { |
| 59 | + if len(options.OutPath) > 0 { |
| 60 | + forFile = filepath.Join(options.OutPath, filepath.Base(forFile)) |
| 61 | + } |
| 62 | + var extension = filepath.Ext(forFile) |
| 63 | + var fileStem = forFile[0 : len(forFile)-len(extension)] |
| 64 | + return fileStem + ".js" |
| 65 | +} |
| 66 | + |
| 67 | +func (JSGenerator) IsGeneratedFile(file string) bool { |
| 68 | + var name = filepath.Base(file) |
| 69 | + return name == "objectbox-model.js" || name == "schema.obx.js" |
| 70 | +} |
| 71 | + |
| 72 | +func (JSGenerator) IsSourceFile(file string) bool { |
| 73 | + return strings.HasSuffix(file, ".fbs") |
| 74 | +} |
| 75 | + |
| 76 | +func (gen *JSGenerator) ParseSource(sourceFile string) (*model.ModelInfo, error) { |
| 77 | + schemaReflection, err := flatbuffersc.ParseSchemaFile(sourceFile) |
| 78 | + if err != nil { |
| 79 | + return nil, err // already includes file name so no more context should be necessary |
| 80 | + } |
| 81 | + |
| 82 | + reader := fbSchemaReader{model: &model.ModelInfo{}, optional: gen.Optional} |
| 83 | + if err = reader.read(schemaReflection); err != nil { |
| 84 | + return nil, fmt.Errorf("error generating model from schema %s: %s", sourceFile, err) |
| 85 | + } |
| 86 | + |
| 87 | + return reader.model, nil |
| 88 | +} |
| 89 | + |
| 90 | +// Generate the schema.obx.js file, given the merged model info |
| 91 | +func (gen *JSGenerator) WriteBindingFiles(sourceFile string, options generator.Options, mergedModel *model.ModelInfo) error { |
| 92 | + var err, err2 error |
| 93 | + |
| 94 | + var bindingFile = gen.BindingFiles(sourceFile, options)[0] |
| 95 | + |
| 96 | + // First generate the binding source |
| 97 | + var bindingSource []byte |
| 98 | + if bindingSource, err = gen.generateBindingFile(bindingFile, mergedModel); err != nil { |
| 99 | + return fmt.Errorf("can't generate binding file %s: %s", sourceFile, err) |
| 100 | + } |
| 101 | + |
| 102 | + if formattedSource, err := format(bindingSource); err != nil { |
| 103 | + // We just store error but still write the file so that we can check it manually |
| 104 | + err2 = fmt.Errorf("failed to format generated binding file %s: %s", bindingFile, err) |
| 105 | + } else { |
| 106 | + bindingSource = formattedSource |
| 107 | + } |
| 108 | + |
| 109 | + if err = generator.WriteFile(bindingFile, bindingSource, sourceFile); err != nil { |
| 110 | + return fmt.Errorf("can't write binding file %s: %s", sourceFile, err) |
| 111 | + } else if err2 != nil { |
| 112 | + // Now when the binding has been written (for debugging purposes), we can return the error |
| 113 | + return err2 |
| 114 | + } |
| 115 | + |
| 116 | + return nil |
| 117 | +} |
| 118 | + |
| 119 | +func (gen *JSGenerator) generateBindingFile(bindingFile string, modelInfo *model.ModelInfo) (data []byte, err error) { |
| 120 | + var b bytes.Buffer |
| 121 | + writer := bufio.NewWriter(&b) |
| 122 | + |
| 123 | + var replaceSpecialChars = strings.NewReplacer("-", "_", ".", "_") |
| 124 | + var fileIdentifier = strings.ToLower(filepath.Base(bindingFile)) |
| 125 | + fileIdentifier = replaceSpecialChars.Replace(fileIdentifier) |
| 126 | + |
| 127 | + // Arguments for the template |
| 128 | + type TplArgs struct { |
| 129 | + Model *model.ModelInfo |
| 130 | + GeneratorVersion int |
| 131 | + FileIdentifier string |
| 132 | + Optional string |
| 133 | + LangVersion int |
| 134 | + EmptyStringAsNull bool |
| 135 | + NaNAsNull bool |
| 136 | + } |
| 137 | + var tplArgs TplArgs |
| 138 | + tplArgs.Model = modelInfo |
| 139 | + tplArgs.GeneratorVersion = generator.VersionId |
| 140 | + tplArgs.FileIdentifier = fileIdentifier |
| 141 | + tplArgs.Optional = gen.Optional |
| 142 | + tplArgs.EmptyStringAsNull = gen.EmptyStringAsNull |
| 143 | + tplArgs.NaNAsNull = gen.NaNAsNull |
| 144 | + |
| 145 | + var tpl = templates.JsBindingTemplate |
| 146 | + |
| 147 | + if err = tpl.Execute(writer, tplArgs); err != nil { |
| 148 | + return nil, fmt.Errorf("template execution failed: %s", err) |
| 149 | + } |
| 150 | + |
| 151 | + if err = writer.Flush(); err != nil { |
| 152 | + return nil, fmt.Errorf("failed to flush buffer: %s", err) |
| 153 | + } |
| 154 | + |
| 155 | + return b.Bytes(), nil |
| 156 | +} |
| 157 | + |
| 158 | +// Generate the objectbox-model.js, given the merged model info |
| 159 | +func (gen *JSGenerator) WriteModelBindingFile(options generator.Options, mergedModel *model.ModelInfo) error { |
| 160 | + var err, err2 error |
| 161 | + |
| 162 | + var modelFile = gen.ModelFile(options.ModelInfoFile, options) |
| 163 | + var modelSource []byte |
| 164 | + |
| 165 | + if modelSource, err = generateModelFile(mergedModel); err != nil { |
| 166 | + return fmt.Errorf("can't generate model file %s: %s", modelFile, err) |
| 167 | + } |
| 168 | + |
| 169 | + if formattedSource, err := format(modelSource); err != nil { |
| 170 | + // we just store error but still writ the file so that we can check it manually |
| 171 | + err2 = fmt.Errorf("failed to format generated model file %s: %s", modelFile, err) |
| 172 | + } else { |
| 173 | + modelSource = formattedSource |
| 174 | + } |
| 175 | + |
| 176 | + if err = generator.WriteFile(modelFile, modelSource, options.ModelInfoFile); err != nil { |
| 177 | + return fmt.Errorf("can't write model file %s: %s", modelFile, err) |
| 178 | + } else if err2 != nil { |
| 179 | + // now when the model has been written (for debugging purposes), we can return the error |
| 180 | + return err2 |
| 181 | + } |
| 182 | + |
| 183 | + return nil |
| 184 | +} |
| 185 | + |
| 186 | +func generateModelFile(m *model.ModelInfo) (data []byte, err error) { |
| 187 | + var b bytes.Buffer |
| 188 | + writer := bufio.NewWriter(&b) |
| 189 | + |
| 190 | + var tplArguments = struct { |
| 191 | + Model *model.ModelInfo |
| 192 | + GeneratorVersion int |
| 193 | + }{m, generator.VersionId} |
| 194 | + |
| 195 | + if err = templates.JsModelTemplate.Execute(writer, tplArguments); err != nil { |
| 196 | + return nil, fmt.Errorf("template execution failed: %s", err) |
| 197 | + } |
| 198 | + |
| 199 | + if err = writer.Flush(); err != nil { |
| 200 | + return nil, fmt.Errorf("failed to flush buffer: %s", err) |
| 201 | + } |
| 202 | + |
| 203 | + return b.Bytes(), nil |
| 204 | +} |
| 205 | + |
| 206 | +func removeEmptyLines(source []byte) []byte { |
| 207 | + // Split the source into lines |
| 208 | + lines := bytes.Split(source, []byte("\n")) |
| 209 | + |
| 210 | + // Filter out empty or whitespace-only lines |
| 211 | + var filteredLines [][]byte |
| 212 | + for _, line := range lines { |
| 213 | + if strings.TrimSpace(string(line)) != "" { |
| 214 | + filteredLines = append(filteredLines, line) |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + // Join the filtered lines back together |
| 219 | + return bytes.Join(filteredLines, []byte("\n")) |
| 220 | +} |
| 221 | + |
| 222 | +func format(source []byte) ([]byte, error) { |
| 223 | + // NOTE we could do JS source formatting here |
| 224 | + |
| 225 | + // Replace tabs with spaces |
| 226 | + formatted := bytes.ReplaceAll(source, []byte("\t"), []byte(" ")) |
| 227 | + //formatted = removeEmptyLines(formatted) |
| 228 | + |
| 229 | + return formatted, nil |
| 230 | +} |
0 commit comments