001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. 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 implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.fusesource.hawtbuf.proto.compiler;
018
019 import java.util.HashSet;
020 import java.util.List;
021 import java.util.Map;
022 import java.util.Set;
023
024 public class FieldDescriptor {
025
026 public static final String STRING_TYPE = "string".intern();
027 public static final String BOOL_TYPE = "bool".intern();
028 public static final String BYTES_TYPE = "bytes".intern();
029 public static final String DOUBLE_TYPE = "double".intern();
030 public static final String FLOAT_TYPE = "float".intern();
031
032 public static final String INT32_TYPE = "int32".intern();
033 public static final String INT64_TYPE = "int64".intern();
034 public static final String UINT32_TYPE = "uint32".intern();
035 public static final String UINT64_TYPE = "uint64".intern();
036 public static final String SINT32_TYPE = "sint32".intern();
037 public static final String SINT64_TYPE = "sint64".intern();
038 public static final String FIXED32_TYPE = "fixed32".intern();
039 public static final String FIXED64_TYPE = "fixed64".intern();
040 public static final String SFIXED32_TYPE = "sfixed32".intern();
041 public static final String SFIXED64_TYPE = "sfixed64".intern();
042
043 public static final String REQUIRED_RULE = "required".intern();
044 public static final String OPTIONAL_RULE= "optional".intern();
045 public static final String REPEATED_RULE = "repeated".intern();
046
047 public static final Set<String> INT32_TYPES = new HashSet<String>();
048 public static final Set<String> INT64_TYPES = new HashSet<String>();
049 public static final Set<String> INTEGER_TYPES = new HashSet<String>();
050 public static final Set<String> NUMBER_TYPES = new HashSet<String>();
051 public static final Set<String> SCALAR_TYPES = new HashSet<String>();
052
053 public static final Set<String> SIGNED_TYPES = new HashSet<String>();
054 public static final Set<String> UNSIGNED_TYPES = new HashSet<String>();
055
056 static {
057 INT32_TYPES.add(INT32_TYPE);
058 INT32_TYPES.add(UINT32_TYPE);
059 INT32_TYPES.add(SINT32_TYPE);
060 INT32_TYPES.add(FIXED32_TYPE);
061 INT32_TYPES.add(SFIXED32_TYPE);
062
063 INT64_TYPES.add(INT64_TYPE);
064 INT64_TYPES.add(UINT64_TYPE);
065 INT64_TYPES.add(SINT64_TYPE);
066 INT64_TYPES.add(FIXED64_TYPE);
067 INT64_TYPES.add(SFIXED64_TYPE);
068
069 INTEGER_TYPES.addAll(INT32_TYPES);
070 INTEGER_TYPES.addAll(INT64_TYPES);
071
072 NUMBER_TYPES.addAll(INTEGER_TYPES);
073 NUMBER_TYPES.add(DOUBLE_TYPE);
074 NUMBER_TYPES.add(FLOAT_TYPE);
075
076 SCALAR_TYPES.addAll(NUMBER_TYPES);
077 SCALAR_TYPES.add(STRING_TYPE);
078 SCALAR_TYPES.add(BOOL_TYPE);
079 SCALAR_TYPES.add(BYTES_TYPE);
080 }
081
082
083 private String name;
084 private String type;
085 private String rule;
086 private int tag;
087 private Map<String,OptionDescriptor> options;
088 private TypeDescriptor typeDescriptor;
089 private final MessageDescriptor parent;
090 private MessageDescriptor group;
091
092 public FieldDescriptor(MessageDescriptor parent) {
093 this.parent = parent;
094 }
095
096 public void validate(List<String> errors) {
097 if( group!=null ) {
098 typeDescriptor=group;
099 }
100 if( !SCALAR_TYPES.contains(type) ) {
101 // Find the type def for that guy..
102 if( typeDescriptor==null ) {
103 typeDescriptor = parent.getType(type);
104 }
105 if( typeDescriptor == null ) {
106 typeDescriptor = parent.getProtoDescriptor().getType(type);
107 }
108 if( typeDescriptor == null ) {
109 errors.add("Field type not found: "+type);
110 }
111 }
112 }
113
114 public boolean isGroup() {
115 return group!=null;
116 }
117
118 public String getName() {
119 return name;
120 }
121 public void setName(String name) {
122 this.name = name;
123 }
124
125 public String getRule() {
126 return rule;
127 }
128 public void setRule(String rule) {
129 this.rule = rule.intern();
130 }
131
132 public boolean isOptional() {
133 return this.rule == OPTIONAL_RULE;
134 }
135 public boolean isRequired() {
136 return this.rule == REQUIRED_RULE;
137 }
138 public boolean isRepeated() {
139 return this.rule == REPEATED_RULE;
140 }
141
142 public int getTag() {
143 return tag;
144 }
145 public void setTag(int tag) {
146 this.tag = tag;
147 }
148
149 public Map<String,OptionDescriptor> getOptions() {
150 return options;
151 }
152 public void setOptions(Map<String,OptionDescriptor> options) {
153 this.options = options;
154 }
155
156 public String getType() {
157 return type;
158 }
159 public void setType(String type) {
160 this.type = type.intern();
161 }
162
163 public boolean isMessageType() {
164 return !SCALAR_TYPES.contains(type);
165 }
166
167 public boolean isScalarType() {
168 return SCALAR_TYPES.contains(type);
169 }
170
171 public boolean isNumberType() {
172 return NUMBER_TYPES.contains(type);
173 }
174
175 public boolean isIntegerType() {
176 return INTEGER_TYPES.contains(type);
177 }
178
179 public boolean isInteger32Type() {
180 return INT32_TYPES.contains(type);
181 }
182
183 public boolean isInteger64Type() {
184 return INT64_TYPES.contains(type);
185 }
186
187 public boolean isStringType() {
188 return type==STRING_TYPE;
189 }
190
191 public TypeDescriptor getTypeDescriptor() {
192 return typeDescriptor;
193 }
194
195 public void setTypeDescriptor(TypeDescriptor typeDescriptor) {
196 this.typeDescriptor = typeDescriptor;
197 }
198
199 public MessageDescriptor getGroup() {
200 return group;
201 }
202 public void setGroup(MessageDescriptor group) {
203 this.group = group;
204 }
205
206 }