< 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:148
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-2024 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();
 19
 120        static readonly JavaScriptObjectNotationFormat k_JavaScriptObjectNotationFormat =
 21            new JavaScriptObjectNotationFormat();
 22
 23        /// <summary>
 24        ///     Export the content of a given <see cref="DataTableBase" /> to a target format.
 25        /// </summary>
 26        /// <param name="dataTable">The <see cref="DataTableBase" /></param>
 27        /// <param name="uri">The output path/uri where to send the data, absolute if on disk</param>
 28        /// <param name="jsonFallback">If the format cannot be determined by the uri, fallback to JSON.</param>
 29        public static void Export(DataTableBase dataTable, string uri, bool jsonFallback = true)
 030        {
 031            SerializableTable serializableTable = new SerializableTable(dataTable);
 032            FormatBase format = GetFormatFromUri(uri);
 033            if (format == null && jsonFallback)
 034            {
 035                format = k_JavaScriptObjectNotationFormat;
 036            }
 37
 038            format?.Push(uri, serializableTable);
 039        }
 40
 41        public static FormatBase[] GetFormats()
 442        {
 443            return k_KnownFormats.ToArray();
 444        }
 45
 46        public static FormatBase GetFormatFromFile(string filePath)
 047        {
 048            using FileStream fileStream = new FileStream(filePath, FileMode.Open);
 49            const int k_HeaderSize = 25;
 50
 051            byte[] chunk = new byte[k_HeaderSize];
 052            int read = fileStream.Read(chunk, 0, k_HeaderSize);
 053            return read == k_HeaderSize
 54                ? GetFormatFromFileHeader(Encoding.ASCII.GetString(chunk, 0, k_HeaderSize))
 55                : null;
 056        }
 57
 58        static FormatBase GetFormatFromFileHeader(string header)
 059        {
 060            for (int i = 0; i < k_KnownFormats.Count; i++)
 061            {
 062                if (k_KnownFormats[i].IsFileHeader(header))
 063                {
 064                    return k_KnownFormats[i];
 65                }
 066            }
 67
 068            return null;
 069        }
 70
 71        public static FormatBase GetFormatFromUri(string uri)
 072        {
 073            for (int i = 0; i < k_KnownFormats.Count; i++)
 074            {
 075                if (k_KnownFormats[i].IsUri(uri))
 076                {
 077                    return k_KnownFormats[i];
 78                }
 079            }
 80
 081            return null;
 082        }
 83
 84        public static string[] GetImportDialogExtensions()
 085        {
 086            List<string> returnData = new List<string>(4);
 087            returnData.Add("Auto");
 088            returnData.Add("*");
 089            for (int i = 0; i < k_KnownFormats.Count; i++)
 090            {
 091                string[] extensions = k_KnownFormats[i].GetImportDialogExtensions();
 092                if (extensions != null)
 093                {
 094                    returnData.AddRange(extensions);
 095                }
 096            }
 97
 098            return returnData.ToArray();
 099        }
 100
 101        /// <summary>
 102        ///     Update the <see cref="DataTableBase" /> with the data found in the given file.
 103        /// </summary>
 104        /// <remarks>
 105        ///     It's important that the Row Identifier column remains unchanged, no structural changes have occured, and
 106        ///     no changes of column order were made. Object references will be maintained during update, only values wi
 107        ///     be updated.
 108        /// </remarks>
 109        /// <param name="dataTable">The target <see cref="DataTableBase" /> to apply changes to.</param>
 110        /// <param name="uri">The resource path to load data from, absolute if on disk.</param>
 111        /// <param name="removeRowIfNotFound">Should rows that are not found in the file content be removed?</param>
 112        /// <param name="jsonFallback">
 113        ///     If the importer is unable to determine the format based on the URI, fallback to assuming its
 114        ///     JSON.
 115        /// </param>
 116        /// <returns>Was the import successful?</returns>
 117        public static bool Import(DataTableBase dataTable, string uri, bool removeRowIfNotFound = true,
 118            bool jsonFallback = true)
 0119        {
 0120            SerializableTable serializableTable = null;
 0121            FormatBase format = GetFormatFromUri(uri);
 0122            if (format == null && jsonFallback)
 0123            {
 0124                format = k_JavaScriptObjectNotationFormat;
 0125            }
 126
 0127            if (format != null)
 0128            {
 0129                serializableTable = format.Pull(uri, dataTable.GetDataVersion(), dataTable.GetStructureVersion());
 0130            }
 131
 0132            return serializableTable != null && serializableTable.Update(dataTable, removeRowIfNotFound);
 0133        }
 134
 135        public static void RegisterFormat(FormatBase format)
 2136        {
 2137            if (!k_KnownFormats.Contains(format))
 2138            {
 2139                k_KnownFormats.Add(format);
 2140            }
 2141        }
 142
 143        public static void UnregisterFormat(FormatBase format)
 0144        {
 0145            k_KnownFormats.Remove(format);
 0146        }
 147    }
 148}

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)