#!/usr/bin/env python3

# @Time    : 02-02-2021 11:52 AM
# @Author  : sg
# @File    : json2hiveschema.py
# @Software: IntelliJ IDEA

"""
json2hiveschema.py is some awesome script

"""
import json


def isScalar(o):
    return o is None or isinstance(o, (str, int, bool))


def to_hive_schema(o):
    if isinstance(o, dict):
        return to_dict_schema(o)
    elif isinstance(o, list):
        return to_list_schema(o)
    else:
        return scalarType(o)


def scalarType(o):
    if isinstance(o, str): return 'string'
    if isinstance(o, int): return 'int'
    if isinstance(o, bool): return 'boolean'
    return 'string'


def arrayJoin(a, sep=','):
    _schema = []
    if len(a) == 0:
        return 'string'

    e = a[0]
    if isScalar(e):
        _schema.append(scalarType(e))
    elif isinstance(e, dict):
        _schema.append(to_dict_schema(e))
    elif isinstance(e, list):
        _schema.append(to_list_schema(e))

    return ''.join(_schema)


def to_list_schema(a):
    return "array<" + arrayJoin(a, ",") + '>'


def to_dict_schema(d):
    _schema = ['struct<']

    for k in d.keys():
        _schema.append(k)
        _schema.append(':')
        _schema.append(to_hive_schema(d[k]))
        _schema.append(", ")
    # sb.replace(sb.length() - 2, sb.length(), ">"); // remove last comma
    return ''.join(_schema[0:-1]) + ">"


filename = 'some_path'
file_handler = open(filename, 'r')
output_file_handler = open(filename + '.out', 'w')
json_schema = None
for line in file_handler:
    line = line.rstrip()
    jd = json.loads(line)

    schema = []
    for j in jd:
        schema.append(j)
        schema.append(' ')
        schema.append(to_hive_schema(jd[j]))
        schema.append(',')
        schema.append('\n')
    print('result:{0}'.format(c))
    output_file_handler.write(''.join(schema[0:-1]))
    c += 1
