<commit_before>// --------------------------------------------------------------------------------------
// JSON type provider - methods that are called from the generated erased code
// --------------------------------------------------------------------------------------
namespace FSharp.Data.RuntimeImplementation

open System
open System.Globalization
open FSharp.Data.Json
open FSharp.Data.Json.Extensions
open FSharp.Data.RuntimeImplementation.StructuralTypes

/// Underlying representation of the generated JSON types
type JsonDocument = 
  // NOTE: Using a record here to hide the ToString, GetHashCode & Equals
  // (but since this is used across multiple files, we have explicit Create method)
  { JsonValue : JsonValue }
  /// Creates a JsonDocument representing the specified value
  static member Create(value:JsonValue) = 
    { JsonValue = value }

/// Static helper methods called from the generated code
type JsonOperations = 
  // Trivial operations that return primitive values
  static member GetString(value:JsonValue) = value.AsString()
  static member GetDateTime(value:JsonValue, culture) = value.AsDateTime(Operations.GetCulture(culture))
  static member GetGuid(value:JsonValue) = value.AsGuid()
  static member GetBoolean(value:JsonValue) = value.AsBoolean()
  static member GetFloat(value:JsonValue, culture) = value.AsFloat(Operations.GetCulture(culture))
  static member GetDecimal(value:JsonValue, culture) = value.AsDecimal(Operations.GetCulture(culture))
  static member GetInteger(value:JsonValue, culture) = value.AsInteger(Operations.GetCulture(culture))
  static member GetInteger64(value:JsonValue, culture) = value.AsInteger64(Operations.GetCulture(culture))
  static member GetProperty(value:JsonValue, name) = value.GetProperty(name)

  /// Converts JSON array to array of target types
  /// The `packer` function rebuilds representation type (such as
  /// `JsonDocument`) which is then passed to projection function `f`.
  static member ConvertArray(value:JsonValue, packer:Func<_,_>, f:Func<_,_>) = 
    value.AsArray() |> Array.map (packer.Invoke >> f.Invoke)

  /// Get optional property of a specified type
  static member ConvertOptionalProperty(value:JsonValue, name, packer:Func<_,_>, f:Func<_,_>) =     
    value.TryGetProperty(name) |> Option.map (packer.Invoke >> f.Invoke)

  /// Returns all array values that match the specified tag
  /// (Follows the same pattern as ConvertXyz functions above)
  static member GetArrayChildrenByTypeTag(doc:JsonValue, tag, pack:Func<_,_>, f:Func<_,_>) = 
    let tag = InferedTypeTag.ParseCode tag
    let matchesTag = function
      | JsonValue.Null -> false
      | JsonValue.Boolean _ -> tag = InferedTypeTag.Boolean<commit_msg>Fixes #147<commit_after>// --------------------------------------------------------------------------------------
// JSON type provider - methods that are called from the generated erased code
// --------------------------------------------------------------------------------------
namespace FSharp.Data.RuntimeImplementation

open System
open System.Globalization
open FSharp.Data.Json
open FSharp.Data.Json.Extensions
open FSharp.Data.RuntimeImplementation.StructuralTypes

/// Underlying representation of the generated JSON types
type JsonDocument = 
  // NOTE: Using a record here to hide the ToString, GetHashCode & Equals
  // (but since this is used across multiple files, we have explicit Create method)
  { JsonValue : JsonValue }
  /// Creates a JsonDocument representing the specified value
  static member Create(value:JsonValue) = 
    { JsonValue = value }

/// Static helper methods called from the generated code
type JsonOperations = 
  // Trivial operations that return primitive values
  static member GetString(value:JsonValue) = value.AsString()
  static member GetDateTime(value:JsonValue, culture) = value.AsDateTime(Operations.GetCulture(culture))
  static member GetGuid(value:JsonValue) = value.AsGuid()
  static member GetBoolean(value:JsonValue) = value.AsBoolean()
  static member GetFloat(value:JsonValue, culture) = value.AsFloat(Operations.GetCulture(culture))
  static member GetDecimal(value:JsonValue, culture) = value.AsDecimal(Operations.GetCulture(culture))
  static member GetInteger(value:JsonValue, culture) = value.AsInteger(Operations.GetCulture(culture))
  static member GetInteger64(value:JsonValue, culture) = value.AsInteger64(Operations.GetCulture(culture))
  static member GetProperty(value:JsonValue, name) = value.GetProperty(name)

  /// Converts JSON array to array of target types
  /// The `packer` function rebuilds representation type (such as
  /// `JsonDocument`) which is then passed to projection function `f`.
  static member ConvertArray(value:JsonValue, packer:Func<_,_>, f:Func<_,_>) = 
    value.AsArray() |> Array.map (packer.Invoke >> f.Invoke)

  /// Get optional property of a specified type
  static member ConvertOptionalProperty(value:JsonValue, name, packer:Func<_,_>, f:Func<_,_>) =     
    value.TryGetProperty(name) 
    |> Option.bind (function JsonValue.Null -> None | x -> Some x) 
    |> Option.map (packer.Invoke >> f.Invoke)

  /// Returns all array values that match the specified tag
  /// (Follows the same pattern as ConvertXyz functions above)
  static member GetArrayChildrenByTypeTag(doc:JsonValue, tag, pack:Func<_,_>, f:Func<_,_>) = 
    let tag = InferedTypeTag.ParseCode tag
    let matchesTag = function
      | JsonValue.Null -> false
      | JsonValue.Boolean _ -> tag = InferedTypeTag.Boolean
{'fraction_non_alphanumeric': 0.154459753444525, 'fraction_numerical': 0.001994198694706309, 'mean_word_length': 3.39601593625498, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6864766', 'n_tokens_mistral': 1449, 'n_tokens_neox': 1404, 'n_words': 439}