< Summary

Class:GDX.DataTables.DataBinding.DataBindingProvider
Assembly:GDX
File(s):./Packages/com.dotbunny.gdx/GDX/DataTables/DataBinding/DataBindingProvider.cs
Covered lines:12
Uncovered lines:63
Coverable lines:75
Total lines:138
Line coverage:16% (12 of 75)
Covered branches:0
Total branches:0
Covered methods:3
Total methods:10
Method coverage:30% (3 of 10)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
DataBindingProvider()0%110100%
Export(...)0%12300%
GetFormats()0%110100%
GetFormatFromFile(...)0%20400%
GetFormatFromFileHeader(...)0%12300%
GetFormatFromUri(...)0%12300%
GetImportDialogExtensions()0%12300%
Import(...)0%20400%
RegisterFormat(...)0%220100%
UnregisterFormat(...)0%2100%

File(s)

./Packages/com.dotbunny.gdx/GDX/DataTables/DataBinding/DataBindingProvider.cs

#LineLine coverage
 1// Copyright (c) 2020-2023 dotBunny Inc.
 2// dotBunny licenses this file to you under the BSL-1.0 license.
 3// See the LICENSE file in the project root for more information.
 4
 5using System.Collections.Generic;
 6using System.IO;
 7using System.Text;
 8using GDX.DataTables.DataBinding.Formats;
 9
 10namespace GDX.DataTables.DataBinding
 11{
 12    /// <summary>
 13    ///     Data translation functionality for <see cref="DataTableBase" />.
 14    /// </summary>
 15    public static class DataBindingProvider
 16    {
 117        static readonly List<FormatBase> k_KnownFormats = new List<FormatBase>(3);
 118        static readonly CommaSeperatedValueFormat k_CommaSeperatedValueFormat = new CommaSeperatedValueFormat();
 119        static readonly JavaScriptObjectNotationFormat k_JavaScriptObjectNotationFormat =
 20            new JavaScriptObjectNotationFormat();
 21
 22        /// <summary>
 23        ///     Export the content of a given <see cref="DataTableBase" /> to a target format.
 24        /// </summary>
 25        /// <param name="dataTable">The <see cref="DataTableBase" /></param>
 26        /// <param name="uri">The output path/uri where to send the data, absolute if on disk</param>
 27        /// <param name="jsonFallback">If the format cannot be determined by the uri, fallback to JSON.</param>
 28        public static void Export(DataTableBase dataTable, string uri, bool jsonFallback = true)
 029        {
 030            SerializableTable serializableTable = new SerializableTable(dataTable);
 031            FormatBase format = GetFormatFromUri(uri);
 032            if (format == null && jsonFallback)
 033            {
 034                format = k_JavaScriptObjectNotationFormat;
 035            }
 036            format?.Push(uri, serializableTable);
 037        }
 38
 39        public static FormatBase[] GetFormats()
 440        {
 441            return k_KnownFormats.ToArray();
 442        }
 43
 44        public static FormatBase GetFormatFromFile(string filePath)
 045        {
 046            using FileStream fileStream = new FileStream(filePath, FileMode.Open);
 47            const int k_HeaderSize = 25;
 48
 049            byte[] chunk = new byte[k_HeaderSize];
 050            int read = fileStream.Read(chunk, 0, k_HeaderSize);
 051            return read == k_HeaderSize
 52                ? GetFormatFromFileHeader(Encoding.ASCII.GetString(chunk, 0, k_HeaderSize))
 53                : null;
 054        }
 55
 56        static FormatBase GetFormatFromFileHeader(string header)
 057        {
 058            for (int i = 0; i < k_KnownFormats.Count; i++)
 059            {
 060                if (k_KnownFormats[i].IsFileHeader(header))
 061                {
 062                    return k_KnownFormats[i];
 63                }
 064            }
 065            return null;
 066        }
 67
 68        public static FormatBase GetFormatFromUri(string uri)
 069        {
 070            for (int i = 0; i < k_KnownFormats.Count; i++)
 071            {
 072                if (k_KnownFormats[i].IsUri(uri))
 073                {
 074                    return k_KnownFormats[i];
 75                }
 076            }
 077            return null;
 078        }
 79
 80        public static string[] GetImportDialogExtensions()
 081        {
 082            List<string> returnData = new List<string>(4);
 083            returnData.Add("Auto");
 084            returnData.Add("*");
 085            for (int i = 0; i < k_KnownFormats.Count; i++)
 086            {
 087                string[] extensions = k_KnownFormats[i].GetImportDialogExtensions();
 088                if (extensions != null)
 089                {
 090                    returnData.AddRange(extensions);
 091                }
 092            }
 093            return returnData.ToArray();
 094        }
 95
 96        /// <summary>
 97        ///     Update the <see cref="DataTableBase" /> with the data found in the given file.
 98        /// </summary>
 99        /// <remarks>
 100        ///     It's important that the Row Identifier column remains unchanged, no structural changes have occured, and
 101        ///     no changes of column order were made. Object references will be maintained during update, only values wi
 102        ///     be updated.
 103        /// </remarks>
 104        /// <param name="dataTable">The target <see cref="DataTableBase" /> to apply changes to.</param>
 105        /// <param name="uri">The resource path to load data from, absolute if on disk.</param>
 106        /// <param name="removeRowIfNotFound">Should rows that are not found in the file content be removed?</param>
 107        /// <param name="jsonFallback">If the importer is unable to determine the format based on the URI, fallback to a
 108        /// <returns>Was the import successful?</returns>
 109        public static bool Import(DataTableBase dataTable, string uri, bool removeRowIfNotFound = true,
 110            bool jsonFallback = true)
 0111        {
 0112            SerializableTable serializableTable = null;
 0113            FormatBase format = GetFormatFromUri(uri);
 0114            if (format == null && jsonFallback)
 0115            {
 0116                format = k_JavaScriptObjectNotationFormat;
 0117            }
 0118            if (format != null)
 0119            {
 0120                serializableTable = format.Pull(uri, dataTable.GetDataVersion(), dataTable.GetStructureVersion());
 0121            }
 0122            return serializableTable != null && serializableTable.Update(dataTable, removeRowIfNotFound);
 0123        }
 124
 125        public static void RegisterFormat(FormatBase format)
 2126        {
 2127            if (!k_KnownFormats.Contains(format))
 2128            {
 2129                k_KnownFormats.Add(format);
 2130            }
 2131        }
 132
 133        public static void UnregisterFormat(FormatBase format)
 0134        {
 0135            k_KnownFormats.Remove(format);
 0136        }
 137    }
 138}

Coverage by test methods





Methods/Properties

DataBindingProvider()
Export(GDX.DataTables.DataTableBase, System.String, System.Boolean)
GetFormats()
GetFormatFromFile(System.String)
GetFormatFromFileHeader(System.String)
GetFormatFromUri(System.String)
GetImportDialogExtensions()
Import(GDX.DataTables.DataTableBase, System.String, System.Boolean, System.Boolean)
RegisterFormat(GDX.DataTables.DataBinding.FormatBase)
UnregisterFormat(GDX.DataTables.DataBinding.FormatBase)