Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Change static blob schema format to JSON
  • Loading branch information
theprash committed May 4, 2017
1 parent e8a55cb commit 83cce0d
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 52 deletions.
1 change: 1 addition & 0 deletions FSharp.Azure.StorageTypeProvider.sln
Expand Up @@ -27,6 +27,7 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "content", "content", "{15350981-90B9-44A0-B356-776735ACC7F1}"
ProjectSection(SolutionItems) = preProject
docs\content\blobs.fsx = docs\content\blobs.fsx
docs\content\BlobSchema.json = docs\content\BlobSchema.json
docs\content\index.fsx = docs\content\index.fsx
docs\content\queues.fsx = docs\content\queues.fsx
docs\content\quickstart.fsx = docs\content\quickstart.fsx
Expand Down
10 changes: 10 additions & 0 deletions docs/content/BlobSchema.json
@@ -0,0 +1,10 @@
{
"samples": {
"file1.txt": null,
"file2.txt": null,
"file3.txt": null,
"folder": { "childFile.txt": null },
"folder2": { "child": { "descedant4.txt": null } },
"random": { "file.txt": null }
}
}
6 changes: 0 additions & 6 deletions docs/content/BlobSchema.txt

This file was deleted.

48 changes: 16 additions & 32 deletions src/FSharp.Azure.StorageTypeProvider/Blob/StaticSchema.fs
Expand Up @@ -3,40 +3,22 @@
open BlobRepository
open FSharp.Azure.StorageTypeProvider.Configuration
open Microsoft.WindowsAzure.Storage.Blob
open System
open Newtonsoft.Json.Linq
open System.IO

let private splitOn (c:char) (value:string) = value.Split c
let rec buildBlobItem prevPath (name, item:Json.Json) =
match item with
| Json.Null -> Blob (prevPath + name, name, BlobType.BlockBlob, None)
| Json.Object o ->
let folderName = name + "/"
let path = prevPath + folderName
Folder (path, folderName, lazy (o |> Array.map (buildBlobItem path)))
| _ -> failwith "Invalid JSON structure in static blob schema."

let private pathsToContainerItems paths =
let rec toFileTrees prevPath childPaths =
childPaths
|> Array.groupBy (function
| [] | [ "" ] -> None
| [ fileName ] -> Some (fileName, true)
| dirName :: _ -> Some (dirName, false))
|> Array.choose (function (Some k, v) -> Some (k, v) | _ -> None)
|> Array.map (fun ((name, isFile), childPaths) ->
if isFile then Blob (prevPath + name, name, BlobType.BlockBlob, None)
else
let folderName = name + "/"
let subPaths = childPaths |> Array.map List.tail
let path = prevPath + folderName
Folder (path, folderName, lazy (toFileTrees path subPaths |> Seq.toArray)))

toFileTrees "" paths

let private schemaLinesToContainers lines =
lines
|> Seq.toArray
|> Array.map (fun line ->
match line |> splitOn '@' with
| [| container; path |] -> (container, path)
| _ -> failwith (sprintf "Invalid blob path in static schema: %s" line))
|> Array.groupBy fst
|> Array.map (fun (containerName, paths) ->
let buildBlobSchema (json:Json.Json) =
json.AsObject |> Array.map (fun (containerName, container) ->
{ Name = containerName
Contents = lazy (paths |> Array.map (snd >> splitOn '/' >> Array.toList) |> pathsToContainerItems |> Array.toSeq) })
Contents = lazy (container.AsObject |> Seq.map (buildBlobItem "")) })
|> Array.toList

let createSchema resolutionFolder path =
Expand All @@ -48,8 +30,10 @@ let createSchema resolutionFolder path =
| Some file ->
try
file
|> File.ReadAllLines
|> schemaLinesToContainers
|> File.ReadAllText
|> JToken.Parse
|> Json.ofJToken
|> buildBlobSchema
|> Success
with ex -> Failure ex)
|> defaultArg <| Success []
10 changes: 5 additions & 5 deletions src/FSharp.Azure.StorageTypeProvider/Shared.fs
Expand Up @@ -40,11 +40,11 @@ module Json =
| Object of (string * Json)[]
| Array of Json[]
| Null
member this.AsString = match this with Json.String s -> s | _ -> failwith "Not a string"
member this.AsNumber = match this with Json.Number s -> s | _ -> failwith "Not a number"
member this.AsBoolean = match this with Json.Boolean b -> b | _ -> failwith "Not a boolean"
member this.AsObject = match this with Json.Object o -> o | _ -> failwith "Not an object"
member this.AsArray = match this with Json.Array o -> o | _ -> failwith "Not an array"
member this.AsString = match this with Json.String s -> s | _ -> failwith "Expected a JSON string"
member this.AsNumber = match this with Json.Number s -> s | _ -> failwith "Expected a JSON number"
member this.AsBoolean = match this with Json.Boolean b -> b | _ -> failwith "Expected a JSON boolean"
member this.AsObject = match this with Json.Object o -> o | _ -> failwith "Expected a JSON object"
member this.AsArray = match this with Json.Array o -> o | _ -> failwith "Expected a JSON array"
member this.GetProperty key = this.AsObject |> Array.find (fst >> (=) key) |> snd
member this.TryGetProperty key =
match this with Json.Object o -> Some o | _ -> None
Expand Down
15 changes: 15 additions & 0 deletions tests/IntegrationTests/BlobSchema.json
@@ -0,0 +1,15 @@
{
"samples": {
"file1.txt": null,
"file2.txt": null,
"file3.txt": null,
"folder": { "childFile.txt": null },
"folder2": {
"child": { "descedant4.txt": null }
}
},
"random": {
"file.txt": null,
"folder": { "emptyFolder": {} }
}
}
7 changes: 0 additions & 7 deletions tests/IntegrationTests/BlobSchema.txt

This file was deleted.

2 changes: 1 addition & 1 deletion tests/IntegrationTests/BlobUnitTests.fs
Expand Up @@ -9,7 +9,7 @@ open Expecto

type Local = AzureTypeProvider<"DevStorageAccount", "">

type BlobSchema = AzureTypeProvider<blobSchema = "BlobSchema.txt">
type BlobSchema = AzureTypeProvider<blobSchema = "BlobSchema.json">

let container = Local.Containers.samples
[<Tests>]
Expand Down
2 changes: 1 addition & 1 deletion tests/IntegrationTests/IntegrationTests.fsproj
Expand Up @@ -55,7 +55,7 @@
<Compile Include="TableUnitTests.fs" />
<Compile Include="Program.fs" />
<None Include="ResetTestData.fsx" />
<Content Include="BlobSchema.txt" />
<Content Include="BlobSchema.json" />
<None Include="paket.references" />
<None Include="app.config" />
</ItemGroup>
Expand Down

0 comments on commit 83cce0d

Please sign in to comment.