001package org.hl7.fhir.r5.openapi;
002
003import com.google.gson.JsonArray;
004import com.google.gson.JsonObject;
005
006public class SchemaWriter extends BaseWriter {
007
008  public enum SchemaType {
009    array,
010    bool,
011    dateTime,
012    number,
013    string;
014
015    public String toCode() {
016      switch (this) {
017      case array: return "array";
018      case bool: return "boolean";
019      case dateTime: return "date-time";
020      case number: return "number";
021      case string: return "string";
022      }
023      return "??";
024    }
025  }
026
027  public SchemaWriter(JsonObject object) {
028    super(object);
029  }
030
031  public SchemaWriter type(SchemaType value) {
032    if (value != null) {
033      if (value == SchemaType.dateTime) {
034        object.addProperty("type", "string");
035        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)))?)?)?");
036        
037      } else 
038        object.addProperty("type", value.toCode());
039    }
040    return this;
041  }
042
043  public SchemaWriter items() {
044    JsonObject items = new JsonObject();
045    object.add("items", items);
046    return new SchemaWriter(items);
047  }
048
049  public SchemaWriter enums(String... values) {
050    JsonArray arr = forceArray("enum");
051    for (String s : values)
052      arr.add(s);    
053    return this;
054  }
055
056  public SchemaWriter format(String value) {
057    if (value != null)
058      object.addProperty("format", value);
059    return this;    
060  }
061
062}