|
| 1 | +/* |
| 2 | + * Copyright © 2023 Vertispan |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.vertispan.tsdefs.impl.model; |
| 17 | + |
| 18 | +import static com.vertispan.tsdefs.impl.Formatting.NEW_LINE; |
| 19 | + |
| 20 | +import com.vertispan.tsdefs.impl.builders.HasDocs; |
| 21 | +import com.vertispan.tsdefs.impl.builders.HasFunctions; |
| 22 | +import com.vertispan.tsdefs.impl.builders.HasNamespace; |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.LinkedHashSet; |
| 25 | +import java.util.Set; |
| 26 | + |
| 27 | +public class TsBrandedType implements HasNamespace { |
| 28 | + private final String name; |
| 29 | + private final String namespace; |
| 30 | + private TsType type; |
| 31 | + private final Set<TsModifier> modifiers = new LinkedHashSet<>(); |
| 32 | + private final Set<String> enumerations = new LinkedHashSet<>(); |
| 33 | + private final Set<TsMethod> functions = new LinkedHashSet<>(); |
| 34 | + private TsDoc tsDoc; |
| 35 | + private boolean deprecated; |
| 36 | + |
| 37 | + private TsBrandedType(String name, String namespace, TsType type) { |
| 38 | + this.name = name; |
| 39 | + this.namespace = namespace; |
| 40 | + this.type = type; |
| 41 | + } |
| 42 | + |
| 43 | + public static TsBrandedType.TsBrandedTypeBuilder builder( |
| 44 | + String name, String namespace, TsType type) { |
| 45 | + return new TsBrandedType.TsBrandedTypeBuilder(name, namespace, type); |
| 46 | + } |
| 47 | + |
| 48 | + public String getName() { |
| 49 | + return name; |
| 50 | + } |
| 51 | + |
| 52 | + public String getNamespace() { |
| 53 | + return namespace; |
| 54 | + } |
| 55 | + |
| 56 | + public TsType getType() { |
| 57 | + return type; |
| 58 | + } |
| 59 | + |
| 60 | + public String emit(String indent, String parentNamespace) { |
| 61 | + StringBuffer sb = new StringBuffer(); |
| 62 | + |
| 63 | + TsBrand tsBrand = new TsBrand(name, namespace); |
| 64 | + sb.append(tsBrand.emit(indent)); |
| 65 | + sb.append(NEW_LINE); |
| 66 | + |
| 67 | + TsClass.TsClassBuilder classBuilder = |
| 68 | + TsClass.builder(name, namespace).addModifiers(TsModifier.EXPORT).setDocs(tsDoc); |
| 69 | + enumerations.stream() |
| 70 | + .map( |
| 71 | + enumeration -> |
| 72 | + TsProperty.builder(enumeration, tsBrand.getType()) |
| 73 | + .setDocs(TsDoc.empty()) |
| 74 | + .addModifiers(TsModifier.STATIC, TsModifier.READONLY) |
| 75 | + .build()) |
| 76 | + .forEach(classBuilder::addProperty); |
| 77 | + |
| 78 | + functions.forEach(classBuilder::addFunction); |
| 79 | + |
| 80 | + sb.append(classBuilder.build().emit(indent, parentNamespace)); |
| 81 | + |
| 82 | + return sb.toString(); |
| 83 | + } |
| 84 | + |
| 85 | + public static class TsBrandedTypeBuilder |
| 86 | + implements HasDocs<TsBrandedType.TsBrandedTypeBuilder>, |
| 87 | + HasFunctions<TsBrandedType.TsBrandedTypeBuilder> { |
| 88 | + private final TsBrandedType tsEnum; |
| 89 | + |
| 90 | + private TsBrandedTypeBuilder(String name, String namespace, TsType type) { |
| 91 | + this.tsEnum = new TsBrandedType(name, namespace, type); |
| 92 | + } |
| 93 | + |
| 94 | + public TsBrandedType.TsBrandedTypeBuilder addEnumeration(String name) { |
| 95 | + this.tsEnum.enumerations.add(name); |
| 96 | + return this; |
| 97 | + } |
| 98 | + |
| 99 | + public TsBrandedType.TsBrandedTypeBuilder addModifiers(TsModifier... modifiers) { |
| 100 | + this.tsEnum.modifiers.addAll(Arrays.asList(modifiers)); |
| 101 | + return this; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public TsBrandedType.TsBrandedTypeBuilder setDocs(TsDoc tsDoc) { |
| 106 | + this.tsEnum.tsDoc = tsDoc; |
| 107 | + return this; |
| 108 | + } |
| 109 | + |
| 110 | + public TsBrandedType.TsBrandedTypeBuilder setDeprecated(boolean deprecated) { |
| 111 | + this.tsEnum.deprecated = deprecated; |
| 112 | + return this; |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public TsBrandedType.TsBrandedTypeBuilder addFunction(TsMethod function) { |
| 117 | + this.tsEnum.functions.add(function); |
| 118 | + return this; |
| 119 | + } |
| 120 | + |
| 121 | + public TsBrandedType build() { |
| 122 | + return this.tsEnum; |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments