|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.bootstrap; |
| 11 | + |
| 12 | +import org.elasticsearch.xcontent.ObjectParser; |
| 13 | +import org.elasticsearch.xcontent.ParseField; |
| 14 | +import org.elasticsearch.xcontent.XContentParser; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +public class TestBuildInfoParser { |
| 20 | + |
| 21 | + private static final String NAME_KEY = "name"; |
| 22 | + private static final String LOCATIONS_KEY = "locations"; |
| 23 | + |
| 24 | + private static final ObjectParser<Builder, Void> PARSER = new ObjectParser<>("test_build_info", Builder::new); |
| 25 | + private static final ObjectParser<Location, Void> LOCATION_PARSER = new ObjectParser<>("location", Location::new); |
| 26 | + static { |
| 27 | + LOCATION_PARSER.declareString(Location::className, new ParseField("class")); |
| 28 | + LOCATION_PARSER.declareString(Location::moduleName, new ParseField("module")); |
| 29 | + |
| 30 | + PARSER.declareString(Builder::name, new ParseField("name")); |
| 31 | + PARSER.declareObjectArray(Builder::locations, LOCATION_PARSER, new ParseField("locations")); |
| 32 | + } |
| 33 | + |
| 34 | + private static class Location { |
| 35 | + private String className; |
| 36 | + private String moduleName; |
| 37 | + |
| 38 | + public void moduleName(final String moduleName) { |
| 39 | + this.moduleName = moduleName; |
| 40 | + } |
| 41 | + public void className(final String className) { |
| 42 | + this.className = className; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private static final class Builder { |
| 47 | + private String name; |
| 48 | + private List<Location> locations; |
| 49 | + |
| 50 | + public void name(final String name) { |
| 51 | + this.name = name; |
| 52 | + } |
| 53 | + |
| 54 | + public void locations(final List<Location> locations) { |
| 55 | + this.locations = locations; |
| 56 | + } |
| 57 | + |
| 58 | + TestBuildInfo build() { |
| 59 | + return new TestBuildInfo(name); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + public static TestBuildInfo fromXContent(final XContentParser parser) throws IOException { |
| 65 | + return PARSER.parse(parser, null).build(); |
| 66 | + } |
| 67 | +} |
0 commit comments