@@ -3,17 +3,21 @@ package workspace
3
3
import (
4
4
"fmt"
5
5
"github.com/tidwall/gjson"
6
+ "go.uber.org/zap"
7
+ "regexp"
6
8
)
7
9
8
- func unmarshalDatastore (json []byte ) * Datastore {
9
- sysData := SystemData {
10
- CreationDate : gjson .GetBytes (json , "systemData.createdAt" ).Time (),
11
- CreationUserType : gjson .GetBytes (json , "systemData.createdByType" ).Str ,
12
- CreationUser : gjson .GetBytes (json , "systemData.createdBy" ).Str ,
13
- LastModifiedDate : gjson .GetBytes (json , "systemData.lastModifiedAt" ).Time (),
14
- LastModifiedUserType : gjson .GetBytes (json , "systemData.lastModifiedByType" ).Str ,
15
- LastModifiedUser : gjson .GetBytes (json , "systemData.lastModifiedBy" ).Str ,
10
+ func unmarshalDatastoreArray (json []byte ) []Datastore {
11
+ jsonDatastoreArray := gjson .GetBytes (json , "value" ).Array ()
12
+ datastoreSlice := make ([]Datastore , gjson .GetBytes (json , "value.#" ).Int ())
13
+ for i , jsonDatastore := range jsonDatastoreArray {
14
+ datastore := unmarshalDatastore ([]byte (jsonDatastore .Raw ))
15
+ datastoreSlice [i ] = * datastore
16
16
}
17
+ return datastoreSlice
18
+ }
19
+
20
+ func unmarshalDatastore (json []byte ) * Datastore {
17
21
auth := DatastoreAuth {
18
22
CredentialsType : gjson .GetBytes (json , "properties.contents.credentials.credentialsType" ).Str ,
19
23
TenantId : gjson .GetBytes (json , "properties.contents.credentials.tenantId" ).Str ,
@@ -32,20 +36,75 @@ func unmarshalDatastore(json []byte) *Datastore {
32
36
StorageContainerName : gjson .GetBytes (json , "properties.contents.containerName" ).Str ,
33
37
StorageType : gjson .GetBytes (json , "properties.contents.contentsType" ).Str ,
34
38
35
- SystemData : & sysData ,
39
+ SystemData : unmarshalSystemData ( json ) ,
36
40
Auth : & auth ,
37
41
}
38
42
}
39
43
40
- func unmarshalDatastoreArray (json []byte ) []Datastore {
41
- jsonDatastoreArray := gjson .GetBytes (json , "value" ).Array ()
42
- datastoreSlice := make ([]Datastore , gjson .GetBytes (json , "value.#" ).Int ())
43
- for i , jsonDatastore := range jsonDatastoreArray {
44
- datastore := unmarshalDatastore ([]byte (jsonDatastore .Raw ))
45
- datastoreSlice [i ] = * datastore
46
- fmt .Println (datastore )
44
+ type DatasetConverter struct {
45
+ logger * zap.SugaredLogger
46
+ }
47
+
48
+ func (d DatasetConverter ) unmarshalDatasetVersionArray (datasetName string , json []byte ) []Dataset {
49
+ jsonDatasetArray := gjson .GetBytes (json , "value" ).Array ()
50
+ datasetSlice := make ([]Dataset , gjson .GetBytes (json , "value.#" ).Int ())
51
+ for i , jsonDataset := range jsonDatasetArray {
52
+ dataset := d .unmarshalDatasetVersion (datasetName , []byte (jsonDataset .Raw ))
53
+ datasetSlice [i ] = * dataset
54
+ }
55
+ return datasetSlice
56
+ }
57
+
58
+ func (d DatasetConverter ) unmarshalDatasetVersion (datasetName string , json []byte ) * Dataset {
59
+ return & Dataset {
60
+ Id : gjson .GetBytes (json , "id" ).Str ,
61
+ Name : datasetName ,
62
+ Description : gjson .GetBytes (json , "properties.description" ).Str ,
63
+ DatastoreId : gjson .GetBytes (json , "properties.datastoreId" ).Str ,
64
+ Version : int (gjson .GetBytes (json , "name" ).Int ()),
65
+ FilePaths : d .unmarshalDatasetPaths (gjson .GetBytes (json , "properties.paths" ), "file" ),
66
+ DirectoryPaths : d .unmarshalDatasetPaths (gjson .GetBytes (json , "properties.paths" ), "folder" ),
67
+ SystemData : unmarshalSystemData (json ),
68
+ }
69
+ }
70
+
71
+ func (d DatasetConverter ) unmarshalDatasetNextVersion (json []byte ) int {
72
+ return int (gjson .GetBytes (json , "properties.nextVersion" ).Int ())
73
+ }
74
+
75
+ func (d DatasetConverter ) unmarshalDatasetPaths (jsonDatasetPaths gjson.Result , pathType string ) []DatasetPath {
76
+ result := make ([]DatasetPath , 0 )
77
+ jsonDatasetPaths .ForEach (func (key , value gjson.Result ) bool {
78
+ path := value .Get (pathType )
79
+ if path .Exists () == false {
80
+ d .logger .Errorf ("cannot unmarshal dataset path: path type %q does exist" , pathType )
81
+ return false
82
+ }
83
+ if path .Type != gjson .Null {
84
+ isDatastorePath , _ := regexp .MatchString (fmt .Sprintf ("%s.*" , datastorePathPrefix ), path .Str )
85
+ if isDatastorePath == true {
86
+ datastorePath , err := NewDatastorePath (path .Str )
87
+ if err != nil {
88
+ d .logger .Errorf ("error unmarshalling dataset path: %s" , err .Error ())
89
+ } else {
90
+ result = append (result , datastorePath )
91
+ }
92
+ }
93
+ }
94
+ return true
95
+ })
96
+ return result
97
+ }
98
+
99
+ func unmarshalSystemData (json []byte ) * SystemData {
100
+ return & SystemData {
101
+ CreationDate : gjson .GetBytes (json , "systemData.createdAt" ).Time (),
102
+ CreationUserType : gjson .GetBytes (json , "systemData.createdByType" ).Str ,
103
+ CreationUser : gjson .GetBytes (json , "systemData.createdBy" ).Str ,
104
+ LastModifiedDate : gjson .GetBytes (json , "systemData.lastModifiedAt" ).Time (),
105
+ LastModifiedUserType : gjson .GetBytes (json , "systemData.lastModifiedByType" ).Str ,
106
+ LastModifiedUser : gjson .GetBytes (json , "systemData.lastModifiedBy" ).Str ,
47
107
}
48
- return datastoreSlice
49
108
}
50
109
51
110
func toWriteDatastoreSchema (datastore * Datastore ) * SchemaWrapper {
@@ -81,3 +140,20 @@ func toWriteDatastoreSchema(datastore *Datastore) *SchemaWrapper {
81
140
},
82
141
}
83
142
}
143
+
144
+ func toWriteDatasetSchema (dataset * Dataset ) * SchemaWrapper {
145
+ pathSchemas := make ([]DatasetPathsSchema , len (dataset .FilePaths )+ len (dataset .DirectoryPaths ))
146
+ for i , filePath := range dataset .FilePaths {
147
+ pathSchemas [i ] = DatasetPathsSchema {FilePath : filePath .String ()}
148
+ }
149
+ for i , directoryPath := range dataset .DirectoryPaths {
150
+ pathSchemas [i ] = DatasetPathsSchema {DirectoryPath : directoryPath .String ()}
151
+ }
152
+
153
+ return & SchemaWrapper {
154
+ Properties : WriteDatasetSchema {
155
+ Description : dataset .Description ,
156
+ Paths : pathSchemas ,
157
+ },
158
+ }
159
+ }
0 commit comments