|
| 1 | +/** ****************************************************************************** |
| 2 | + * Copyright (c) 2025 Precies. Software OU and others |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v. 2.0 which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + * ****************************************************************************** */ |
| 10 | +package org.eclipse.openvsx.adapter; |
| 11 | + |
| 12 | +import java.util.Collections; |
| 13 | +import java.util.List; |
| 14 | +import java.util.stream.Stream; |
| 15 | + |
| 16 | +import static org.eclipse.openvsx.adapter.ExtensionQueryParam.*; |
| 17 | + |
| 18 | +public record ExtensionQueryExtensionData( |
| 19 | + String publicId, |
| 20 | + String extensionId, |
| 21 | + String extensionName, |
| 22 | + String displayName, |
| 23 | + String shortDescription, |
| 24 | + ExtensionQueryResult.Publisher publisher, |
| 25 | + List<ExtensionVersion> versions, |
| 26 | + List<Long> latestVersions, |
| 27 | + List<ExtensionQueryResult.Statistic> statistics, |
| 28 | + List<String> tags, |
| 29 | + String releaseDate, |
| 30 | + String publishedDate, |
| 31 | + String lastUpdated, |
| 32 | + List<String> categories, |
| 33 | + String preview |
| 34 | +) { |
| 35 | + |
| 36 | + public record ExtensionVersion(long id, ExtensionQueryResult.ExtensionVersion data) { |
| 37 | + public ExtensionQueryResult.ExtensionVersion toJson(int flags) { |
| 38 | + var files = test(flags, FLAG_INCLUDE_FILES) ? data.files() : null; |
| 39 | + var assetUri = test(flags, FLAG_INCLUDE_ASSET_URI) ? data.assetUri() : null; |
| 40 | + var properties = test(flags, FLAG_INCLUDE_VERSION_PROPERTIES) ? data.properties() : null; |
| 41 | + |
| 42 | + return new ExtensionQueryResult.ExtensionVersion( |
| 43 | + data.version(), |
| 44 | + data.lastUpdated(), |
| 45 | + assetUri, |
| 46 | + assetUri, |
| 47 | + files, |
| 48 | + properties, |
| 49 | + data.targetPlatform() |
| 50 | + ); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public ExtensionQueryResult.Extension toJson(int flags) { |
| 55 | + var extVersionStream = Stream.<ExtensionVersion>empty(); |
| 56 | + if (test(flags, FLAG_INCLUDE_LATEST_VERSION_ONLY)) { |
| 57 | + extVersionStream = versions.stream().filter(v -> latestVersions.contains(v.id())); |
| 58 | + } else if (test(flags, FLAG_INCLUDE_VERSIONS)) { |
| 59 | + extVersionStream = versions.stream(); |
| 60 | + } |
| 61 | + |
| 62 | + var extVersions = extVersionStream.map(v -> v.toJson(flags)).toList(); |
| 63 | + var stats = test(flags, FLAG_INCLUDE_STATISTICS) ? statistics : Collections.<ExtensionQueryResult.Statistic>emptyList(); |
| 64 | + return new ExtensionQueryResult.Extension( |
| 65 | + publicId, |
| 66 | + extensionName, |
| 67 | + displayName, |
| 68 | + shortDescription, |
| 69 | + publisher, |
| 70 | + extVersions, |
| 71 | + stats, |
| 72 | + tags, |
| 73 | + releaseDate, |
| 74 | + publishedDate, |
| 75 | + lastUpdated, |
| 76 | + categories, |
| 77 | + preview() |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + private static boolean test(int flags, int flag) { |
| 82 | + return (flags & flag) != 0; |
| 83 | + } |
| 84 | +} |
0 commit comments