001package org.hl7.fhir.r5.openapi;
002
003/*-
004 * #%L
005 * org.hl7.fhir.r5
006 * %%
007 * Copyright (C) 2014 - 2019 Health Level 7
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 * 
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023
024import org.hl7.fhir.r5.openapi.SchemaWriter.SchemaType;
025
026import com.google.gson.JsonArray;
027import com.google.gson.JsonObject;
028
029public class SchemaWriter extends BaseWriter {
030
031  public enum SchemaType {
032    array,
033    bool,
034    dateTime,
035    number,
036    string;
037
038    public String toCode() {
039      switch (this) {
040      case array: return "array";
041      case bool: return "boolean";
042      case dateTime: return "date-time";
043      case number: return "number";
044      case string: return "string";
045      }
046      return "??";
047    }
048  }
049
050  public SchemaWriter(JsonObject object) {
051    super(object);
052  }
053
054  public SchemaWriter type(SchemaType value) {
055    if (value != null) {
056      if (value == SchemaType.dateTime) {
057        object.addProperty("type", "string");
058        object.addProperty("pattern", "([0-9]([0-9]([0-9][1-9]|[1-9]0)|[1-9]00)|[1-9]000)(-(0[1-9]|1[0-2])(-(0[1-9]|[1-2][0-9]|3[0-1])(T([01][0-9]|2[0-3]):[0-5][0-9]:([0-5][0-9]|60)(\\.[0-9]+)?(Z|(\\+|-)((0[0-9]|1[0-3]):[0-5][0-9]|14:00)))?)?)?");
059        
060      } else 
061        object.addProperty("type", value.toCode());
062    }
063    return this;
064  }
065
066  public SchemaWriter items() {
067    JsonObject items = new JsonObject();
068    object.add("items", items);
069    return new SchemaWriter(items);
070  }
071
072  public SchemaWriter enums(String... values) {
073    JsonArray arr = forceArray("enum");
074    for (String s : values)
075      arr.add(s);    
076    return this;
077  }
078
079  public SchemaWriter format(String value) {
080    if (value != null)
081      object.addProperty("format", value);
082    return this;    
083  }
084
085}