001package net.gdface.codegen.thrift.metadata;
002
003import com.facebook.swift.codec.metadata.DecoratorThriftFieldMetadata;
004import com.facebook.swift.codec.metadata.ErpcProtocolType;
005import com.facebook.swift.codec.metadata.ThriftFieldMetadataUtil;
006import com.google.common.base.Function;
007
008import net.gdface.annotation.CodegenLength;
009
010public class CodegenPreAllocSizeGetter implements Function<DecoratorThriftFieldMetadata, Integer> {
011        public static final CodegenPreAllocSizeGetter PREALLOC_SIZE_GETTER = new CodegenPreAllocSizeGetter(); 
012        public CodegenPreAllocSizeGetter() {
013        }
014
015        @Override
016        public Integer apply(DecoratorThriftFieldMetadata input) {
017                 CodegenLength ann = ThriftFieldMetadataUtil.extractFieldAnnotation(input,CodegenLength.class);
018                if(ann != null){
019                        if(!ann.prealloc()){
020                                return -1;
021                        }
022                        int limit;
023                        if(ann.value() > 0){
024                                limit = ann.value();
025                        }else if(ann.max() > 0){
026                                limit = ann.max();
027                        } else{
028                                return -1;
029                        }
030                        if(ErpcProtocolType.STRING.equals(input.getErpcType().getProtocolType())){
031                                limit++;
032                        }
033                        return  limit;
034                }
035                return -1;
036        }
037
038}