package com.lanyuanxiaoyao.service.sync.functions.type;

import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.lanyuanxiaoyao.service.common.entity.TableMeta;
import java.io.Serializable;
import java.util.Map;
import org.apache.flink.table.data.GenericRowData;
import org.apache.hudi.org.apache.avro.Schema;

/**
 * 处理类型相关内容
 *
 * @author ZhangJiacheng
 * @date 2023-07-20
 */
public interface TypeConverter extends Serializable {
    LoadingCache<Integer, TypeConverter> CACHE = Caffeine.newBuilder()
            .build(version -> {
                switch (version) {
                    case 1:
                        return new TypeConverterV2();
                    case 0:
                    default:
                        return new TypeConverterV1();
                }
            });

    static TypeConverter getInstance(TableMeta meta) {
        return getInstance(meta.getVersion());
    }

    static TypeConverter getInstance(Integer version) {
        if (version == null) {
            version = 0;
        }
        return CACHE.get(version);
    }

    Schema convertToSchema(TableMeta meta);

    GenericRowData convertToGenericRowData(TableMeta meta, Schema schema, Map<String, Object> data);
}
