Skip to content

Commit

Permalink
Remove floats. Add Escape module. Add renderIni to feature values.
Browse files Browse the repository at this point in the history
  • Loading branch information
tarik02 committed May 17, 2019
1 parent 08468e6 commit b11990a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
15 changes: 15 additions & 0 deletions buckaroo/Escape.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Buckaroo


module Escape =
open System.Text.RegularExpressions

let escapeRegex = Regex(@"([\'""])")
let escapeReplacement = "\\$1"

let escape (quote : string) (input : string) =
quote + escapeRegex.Replace(input, escapeReplacement) + quote

let escapeWithoutQuotes = escape ""
let escapeWithSingleQuotes = escape "'"
let escapeWithDoubleQuotes = escape "\""
27 changes: 24 additions & 3 deletions buckaroo/Feature.fs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
namespace Buckaroo

open Buckaroo.BuckConfig
open Buckaroo.Result
open Buckaroo.Toml


type FeatureUnitValue =
| Boolean of bool
| Integer of int64
| Float of float
| String of string
// | Version of Version

Expand All @@ -22,14 +22,18 @@ module FeatureUnitValue =
match x with
| Boolean x -> if x then "true" else "false"
| Integer x -> x.ToString()
| Float x -> x.ToString()
| String x -> "\"" + x + "\""

let renderIni (x : FeatureUnitValue) =
match x with
| Boolean x -> if x then "true" else "false"
| Integer x -> x.ToString()
| String x -> Escape.escapeWithDoubleQuotes x

let fromToml (toml : Nett.TomlObject) : Result<FeatureUnitValue, TomlError> =
match toml with
| :? Nett.TomlBool as v -> FeatureUnitValue.Boolean v.Value |> Result.Ok
| :? Nett.TomlInt as v -> FeatureUnitValue.Integer v.Value |> Result.Ok
| :? Nett.TomlFloat as v -> FeatureUnitValue.Float v.Value |> Result.Ok
| :? Nett.TomlString as v -> FeatureUnitValue.String v.Value |> Result.Ok
| _ -> TomlError.UnexpectedType toml.ReadableTypeName |> Result.Error

Expand All @@ -45,6 +49,23 @@ module FeatureValue =
) + " }"
| Array x -> "[ " + (x |> Seq.map FeatureUnitValue.show |> String.concat ", ") + " ]"

let renderIni (x : FeatureValue) =
match x with
| Value x -> x |> FeatureUnitValue.renderIni |> INIString
| Dictionary x ->
x
|> Map.toSeq
|> Seq.map (fun (key, value) ->
INIString <| Escape.escapeWithDoubleQuotes (key + "=" + (value |> FeatureUnitValue.renderIni))
)
|> List.ofSeq
|> INIList
| Array x ->
x
|> Seq.map (FeatureUnitValue.renderIni >> INIString)
|> List.ofSeq
|> INIList

let fromToml (toml : Nett.TomlObject) : Result<FeatureValue, TomlError> =
match FeatureUnitValue.fromToml toml with
| Result.Ok r -> FeatureValue.Value r |> Result.Ok
Expand Down
1 change: 1 addition & 0 deletions buckaroo/buckaroo.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<Compile Include="Paths.fs" />
<Compile Include="ArchiveType.fs" />
<Compile Include="Toml.fs" />
<Compile Include="Escape.fs" />
<Compile Include="BuckConfig.fs" />
<Compile Include="Files.fs" />
<Compile Include="Archive.fs" />
Expand Down

0 comments on commit b11990a

Please sign in to comment.