@@ -26,10 +26,11 @@ package v2beta1
2626
2727import (
2828 "errors"
29- sharedModel "github.com/pdok/smooth-operator/model"
3029 "log"
3130 "strconv"
3231
32+ sharedModel "github.com/pdok/smooth-operator/model"
33+
3334 "sigs.k8s.io/controller-runtime/pkg/conversion"
3435
3536 pdoknlv3 "github.com/pdok/mapserver-operator/api/v3"
@@ -42,12 +43,12 @@ func (src *WMS) ConvertTo(dstRaw conversion.Hub) error {
4243 dst := dstRaw .(* pdoknlv3.WMS )
4344 log .Printf ("ConvertTo: Converting WMS from Spoke version v2beta1 to Hub version v3;" +
4445 "source: %s/%s, target: %s/%s" , src .Namespace , src .Name , dst .Namespace , dst .Name )
45- V3WMSHubFromV2 (src , dst )
46+ V3HubFromV2 (src , dst )
4647
4748 return nil
4849}
4950
50- func V3WMSHubFromV2 (src * WMS , target * pdoknlv3.WMS ) {
51+ func V3HubFromV2 (src * WMS , target * pdoknlv3.WMS ) {
5152 dst := target
5253
5354 dst .ObjectMeta = src .ObjectMeta
@@ -266,61 +267,73 @@ func (v2Service WMSService) GetTopLayer() (*WMSLayer, error) {
266267 return nil , errors .New ("unable to detect the toplayer of this WMS service" )
267268}
268269
269- func (v2Service WMSService ) GetChildLayers (parent WMSLayer ) ([]WMSLayer , error ) {
270- children := make ([]WMSLayer , 0 )
271-
270+ // MapLayersToV3
271+ func (v2Service WMSService ) MapLayersToV3 () pdoknlv3.Layer {
272+ // Creates map of Groups: layers in that group
273+ // and a list of all layers without a group
274+ groupedLayers := map [string ][]pdoknlv3.Layer {}
275+ var notGroupedLayers []pdoknlv3.Layer
272276 for _ , layer := range v2Service .Layers {
273- if layer .Group != nil && * layer .Group == parent .Name {
274- children = append (children , layer )
277+ if layer .Group == nil {
278+ notGroupedLayers = append (notGroupedLayers , layer .MapToV3 (v2Service ))
279+ } else {
280+ groupedLayers [* layer .Group ] = append (groupedLayers [* layer .Group ], layer .MapToV3 (v2Service ))
275281 }
276282 }
277283
278- if len (children ) == 0 {
279- return children , errors .New ("no child layers found" )
284+ // if a topLayer is defined in the v2 it be the only layer without a group
285+ // and there are other layers that have the topLayer as their group
286+ // so if there is exactly 1 layer without a group
287+ // and the name of that layer exist as a key in the map of Groups: layer in that group
288+ // then that layer must be the topLayer
289+ var topLayer * pdoknlv3.Layer
290+ if len (notGroupedLayers ) == 1 {
291+ _ , ok := groupedLayers [* notGroupedLayers [0 ].Name ]
292+ if ok {
293+ topLayer = & notGroupedLayers [0 ]
294+ }
280295 }
281296
282- return children , nil
283- }
284-
285- // MapLayersToV3
286- func (v2Service WMSService ) MapLayersToV3 () pdoknlv3.Layer {
287- topLayer , err := v2Service .GetTopLayer ()
288- if err != nil {
289- panic (err )
290- }
297+ var middleLayers []pdoknlv3.Layer
291298
292- var layer pdoknlv3.Layer
299+ // if the topLayer is not defined in the v2 layers
300+ // it needs to be created with defaults from the service
301+ // and in this case the middleLayers are all layers without a group
293302 if topLayer == nil {
294- layer = pdoknlv3.Layer {
295- Name : "wms" ,
303+ topLayer = & pdoknlv3.Layer {
296304 Title : & v2Service .Title ,
297305 Abstract : & v2Service .Abstract ,
298306 Keywords : v2Service .Keywords ,
299307 Layers : & []pdoknlv3.Layer {},
300308 }
301309
302- if v2Service . DataEPSG != "EPSG:28992" && v2Service . Extent != nil {
303- layer . BoundingBoxes = append ( layer . BoundingBoxes , pdoknlv3. WMSBoundingBox {
304- CRS : v2Service . DataEPSG ,
305- BBox : sharedModel . ExtentToBBox ( * v2Service . Extent ),
306- } )
310+ // adding the bottom layers to the middle layers they are grouped by
311+ for _ , layer := range notGroupedLayers {
312+ bottomLayers := groupedLayers [ * layer . Name ]
313+ layer . Layers = & bottomLayers
314+ middleLayers = append ( middleLayers , layer )
307315 }
316+ }
308317
309- var childLayersV3 []pdoknlv3.Layer
310- for _ , childLayer := range v2Service .Layers {
311- childLayersV3 = append (childLayersV3 , childLayer .MapToV3 (v2Service ))
318+ // if the topLayer is defined in the v2 layers
319+ // meaning the topLayer has a name at this point
320+ // then the middleLayers are all layers that had the topLayer name as their group
321+ // and the bottomLayers are all layers that had a middleLayer as a group
322+ if topLayer .Name != nil {
323+ for _ , layer := range groupedLayers [* topLayer .Name ] {
324+ bottomLayers := groupedLayers [* layer .Name ]
325+ layer .Layers = & bottomLayers
326+ middleLayers = append (middleLayers , layer )
312327 }
313- layer .Layers = & childLayersV3
314- } else {
315- layer = topLayer .MapToV3 (v2Service )
316328 }
329+ topLayer .Layers = & middleLayers
317330
318- return layer
331+ return * topLayer
319332}
320333
321334func (v2Layer WMSLayer ) MapToV3 (v2Service WMSService ) pdoknlv3.Layer {
322335 layer := pdoknlv3.Layer {
323- Name : v2Layer .Name ,
336+ Name : & v2Layer .Name ,
324337 Title : v2Layer .Title ,
325338 Abstract : v2Layer .Abstract ,
326339 Keywords : v2Layer .Keywords ,
@@ -388,17 +401,6 @@ func (v2Layer WMSLayer) MapToV3(v2Service WMSService) pdoknlv3.Layer {
388401
389402 if v2Layer .Data != nil {
390403 layer .Data = Pointer (ConvertV2DataToV3 (* v2Layer .Data ))
391- } else {
392- childLayersV2 , err := v2Service .GetChildLayers (v2Layer )
393- if err != nil {
394- panic (err )
395- }
396-
397- var childLayersV3 []pdoknlv3.Layer
398- for _ , childLayer := range childLayersV2 {
399- childLayersV3 = append (childLayersV3 , childLayer .MapToV3 (v2Service ))
400- }
401- layer .Layers = & childLayersV3
402404 }
403405
404406 return layer
@@ -407,7 +409,7 @@ func (v2Layer WMSLayer) MapToV3(v2Service WMSService) pdoknlv3.Layer {
407409func mapV3LayerToV2Layers (v3Layer pdoknlv3.Layer , parent * pdoknlv3.Layer , serviceEPSG string ) []WMSLayer {
408410 var layers []WMSLayer
409411
410- if parent == nil && v3Layer .Name == "wms" {
412+ if parent == nil && * v3Layer .Name == "wms" {
411413 // Default top layer, do not include in v2 layers
412414 if v3Layer .Layers != nil {
413415 for _ , childLayer := range * v3Layer .Layers {
@@ -416,7 +418,7 @@ func mapV3LayerToV2Layers(v3Layer pdoknlv3.Layer, parent *pdoknlv3.Layer, servic
416418 }
417419 } else {
418420 v2Layer := WMSLayer {
419- Name : v3Layer .Name ,
421+ Name : * v3Layer .Name ,
420422 Title : v3Layer .Title ,
421423 Abstract : v3Layer .Abstract ,
422424 Keywords : v3Layer .Keywords ,
@@ -427,7 +429,7 @@ func mapV3LayerToV2Layers(v3Layer pdoknlv3.Layer, parent *pdoknlv3.Layer, servic
427429 v2Layer .Visible = PointerVal (v3Layer .Visible , true )
428430
429431 if parent != nil {
430- v2Layer .Group = & parent .Name
432+ v2Layer .Group = parent .Name
431433 }
432434
433435 if v3Layer .DatasetMetadataURL != nil && v3Layer .DatasetMetadataURL .CSW != nil {
0 commit comments