001/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*- 002 * 003 * Copyright © 2017-2018 microBean. 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 014 * implied. See the License for the specific language governing 015 * permissions and limitations under the License. 016 */ 017package org.microbean.helm.chart; 018 019import java.util.Map; 020 021import hapi.chart.MetadataOuterClass.Maintainer; 022import hapi.chart.MetadataOuterClass.Metadata; 023 024/** 025 * A utility class for working with {@link Metadata} instances. 026 * 027 * @author <a href="https://about.me/lairdnelson" 028 * target="_parent">Laird Nelson</a> 029 * 030 * @see hapi.chart.MetadataOuterClass.Metadata.Builder 031 */ 032public final class Metadatas { 033 034 035 /* 036 * Constructors. 037 */ 038 039 040 /** 041 * Creates a new {@link Metadatas}. 042 * 043 * @deprecated This constructor originally did not exist explicitly 044 * in the source code. That means a default no-argument {@code 045 * public} constructor was available to end users. Consequently, 046 * this constructor cannot now be made {@code private} without 047 * breaking API compatibility. This constructor is slated for 048 * removal. 049 */ 050 @Deprecated 051 public Metadatas() { 052 super(); 053 } 054 055 056 /* 057 * Static methods. 058 */ 059 060 061 /** 062 * Given a {@link Map} assumed to represent a YAML document, calls 063 * certain mutating methods on the supplied {@link 064 * hapi.chart.MetadataOuterClass.Metadata.Builder}. 065 * 066 * @param metadataBuilder the {@link 067 * hapi.chart.MetadataOuterClass.Metadata.Builder} to populate; may 068 * be {@code null} in which case no action will be taken 069 * 070 * @param yamlMap a {@link Map} representing a YAML representation 071 * of a {@link Metadata}; may be {@code null} or {@linkplain 072 * Map#isEmpty() empty} in which case no action will be taken; if 073 * non-{@code null} expected to contain zero or more keys drawn from 074 * the following set: {@code annotations}, {@code apiVersion}, 075 * {@code appVersion}, {@code condition}, {@code deprecated}, {@code 076 * description}, {@code engine}, {@code home}, {@code icon}, {@code 077 * keywords}, {@code maintainers}, {@code name}, {@code sources}, 078 * {@code tags}, {@code tillerVersion}, {@code version} 079 * 080 * @exception ClassCastException if the supplied {@code yamlMap} 081 * does not actually represent a YAML representation of a {@link 082 * Metadata} 083 */ 084 public static final void populateMetadataBuilder(Metadata.Builder metadataBuilder, final Map<?, ?> yamlMap) { 085 if (metadataBuilder != null && yamlMap != null && !yamlMap.isEmpty()) { 086 087 @SuppressWarnings("unchecked") 088 final Map<String, String> annotationsMap = (Map<String, String>)yamlMap.get("annotations"); 089 if (annotationsMap != null) { 090 metadataBuilder.putAllAnnotations(annotationsMap); 091 } 092 093 final String apiVersion = (String)yamlMap.get("apiVersion"); 094 if (apiVersion != null) { 095 metadataBuilder.setApiVersion(apiVersion); 096 } 097 098 final String appVersion = (String)yamlMap.get("appVersion"); 099 if (appVersion != null) { 100 metadataBuilder.setAppVersion(appVersion); 101 } 102 103 final String condition = (String)yamlMap.get("condition"); 104 if (condition != null) { 105 metadataBuilder.setCondition(condition); 106 } 107 108 metadataBuilder.setDeprecated("true".equals(String.valueOf(yamlMap.get("deprecated")))); 109 110 final String description = (String)yamlMap.get("description"); 111 if (description != null) { 112 metadataBuilder.setDescription(description); 113 } 114 115 final String engine = (String)yamlMap.get("engine"); 116 if (engine != null) { 117 metadataBuilder.setEngine(engine); 118 } 119 120 final String home = (String)yamlMap.get("home"); 121 if (home != null) { 122 metadataBuilder.setHome(home); 123 } 124 125 final String icon = (String)yamlMap.get("icon"); 126 if (icon != null) { 127 metadataBuilder.setIcon(icon); 128 } 129 130 @SuppressWarnings("unchecked") 131 final Iterable<String> keywords = (Iterable<String>)yamlMap.get("keywords"); 132 if (keywords != null) { 133 metadataBuilder.addAllKeywords(keywords); 134 } 135 136 final String kubeVersion = (String)yamlMap.get("kubeVersion"); 137 if (kubeVersion != null) { 138 metadataBuilder.setKubeVersion(kubeVersion); 139 } 140 141 @SuppressWarnings("unchecked") 142 final Iterable<? extends Map<?, ?>> maintainers = (Iterable<? extends Map<?, ?>>)yamlMap.get("maintainers"); 143 if (maintainers != null) { 144 for (final Map<?, ?> maintainer : maintainers) { 145 if (maintainer != null) { 146 final Maintainer.Builder maintainerBuilder = metadataBuilder.addMaintainersBuilder(); 147 assert maintainerBuilder != null; 148 final String maintainerName = (String)maintainer.get("name"); 149 if (maintainerName != null) { 150 maintainerBuilder.setName(maintainerName); 151 } 152 final String maintainerEmail = (String)maintainer.get("email"); 153 if (maintainerEmail != null) { 154 maintainerBuilder.setEmail(maintainerEmail); 155 } 156 } 157 } 158 } 159 160 final String name = (String)yamlMap.get("name"); 161 if (name != null) { 162 metadataBuilder.setName(name); 163 } 164 165 @SuppressWarnings("unchecked") 166 final Iterable<String> sources = (Iterable<String>)yamlMap.get("sources"); 167 if (sources != null) { 168 metadataBuilder.addAllSources(sources); 169 } 170 171 final String tags = (String)yamlMap.get("tags"); 172 if (tags != null) { 173 metadataBuilder.setTags(tags); 174 } 175 176 final String tillerVersion = (String)yamlMap.get("tillerVersion"); 177 if (tillerVersion != null) { 178 metadataBuilder.setTillerVersion(tillerVersion); 179 } 180 181 final String version = (String)yamlMap.get("version"); 182 if (version != null) { 183 metadataBuilder.setVersion(version); 184 } 185 186 } 187 } 188 189}