001/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*-
002 *
003 * Copyright © 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 org.microbean.development.annotation.Issue;
020
021import org.yaml.snakeyaml.nodes.NodeId;
022import org.yaml.snakeyaml.nodes.Tag;
023
024import org.yaml.snakeyaml.resolver.Resolver;
025
026/**
027 * A {@link Resolver} that forces scalars to be {@link String}s.
028 *
029 * @author <a href="https://about.me/lairdnelson"
030 * target="_parent">Laird Nelson</a>
031 *
032 * @see <a
033 * href="https://github.com/microbean/microbean-helm/issues/131">Issue
034 * 131</a>
035 */
036@Issue(
037  id = "131",
038  uri = "https://github.com/microbean/microbean-helm/issues/131"
039)
040public final class StringResolver extends Resolver {
041
042
043  /*
044   * Constructors.
045   */
046
047  
048  /**
049   * Creates a new {@link StringResolver}.
050   */
051  public StringResolver() {
052    super();
053  }
054
055
056  /*
057   * Instance methods.
058   */
059  
060
061  /**
062   * Overrides the {@link Resolver#resolve(NodeId, String, boolean)}
063   * method so that all implicit scalar non-{@code null} YAML node
064   * values are resolved as {@link Tag#STR}.
065   *
066   * @param kind the kind of YAML node being processed; may be {@code
067   * null}
068   *
069   * @param value the value of the node; may be {@code null}
070   *
071   * @param implicit whether the typing is implicit or explicit
072   *
073   * @return a {@link Tag} instance representing the YAML node type
074   *
075   * @see Resolver#resolve(NodeId, String, boolean)
076   */
077  @Override
078  public final Tag resolve(final NodeId kind, final String value, final boolean implicit) {
079    final Tag returnValue;
080    if (implicit && kind != null && value != null && NodeId.scalar.equals(kind) && !value.isEmpty()) {
081      returnValue = Tag.STR;
082    } else {
083      returnValue = super.resolve(kind, value, implicit);
084    }
085    return returnValue;
086  }
087  
088}
089