@@ -407,63 +407,58 @@ func createNewConfig(params *ConfigNewParams) (string, error) {
407407 log .Printf ("Created config: %s" , string (jsonConfig ))
408408 return string (jsonConfig ), nil
409409}
410+
411+ func buildTree (element interface {}) (interface {}, error ) {
412+ switch v := element .(type ) {
413+ case []interface {}:
414+ if len (v ) != 2 {
415+ return nil , fmt .Errorf ("array must have exactly two elements" )
416+ }
417+ left , err := buildTree (v [0 ])
418+ if err != nil {
419+ return nil , err
420+ }
421+ right , err := buildTree (v [1 ])
422+ if err != nil {
423+ return nil , err
424+ }
425+ return map [string ]interface {}{
426+ "left" : left ,
427+ "right" : right ,
428+ }, nil
429+ case map [string ]interface {}:
430+ if tree , ok := v ["tree" ]; ok && v ["type" ] == "nested" {
431+ processedTree , err := buildTree (tree )
432+ if err != nil {
433+ return nil , fmt .Errorf ("failed to process nested tree: %w" , err )
434+ }
435+ v ["tree" ] = processedTree
436+ }
437+ return v , nil
438+ case string :
439+ return map [string ]interface {}{
440+ "type" : "node" ,
441+ "hash" : v ,
442+ }, nil
443+ default :
444+ return nil , fmt .Errorf ("unsupported element type: %T" , v )
445+ }
446+ }
447+
410448func handleRawConfig (input []byte ) (map [string ]interface {}, error ) {
411449 var rawConfig map [string ]interface {}
412450 if err := json .Unmarshal (input , & rawConfig ); err != nil {
413451 return nil , fmt .Errorf ("failed to parse raw config: %w" , err )
414452 }
415453
416- // Check if the config is nested under an "input" field
417454 if input , ok := rawConfig ["input" ].(map [string ]interface {}); ok {
418455 rawConfig = input
419456 }
420457
421- // Convert topology array to tree structure matching TypeScript
422- if topology , ok := rawConfig ["topology" ].([]interface {}); ok {
423- if len (topology ) != 2 {
424- return nil , fmt .Errorf ("topology must have exactly two elements" )
425- }
426-
427- var leftNode , rightNode map [string ]interface {}
428- var err error
429-
430- // Check if the first element is an array
431- if leftArray , isArray := topology [0 ].([]interface {}); isArray {
432- if len (leftArray ) != 2 {
433- return nil , fmt .Errorf ("left topology row must have exactly two elements" )
434- }
435- leftSubtree , err := buildSubtree (leftArray )
436- if err != nil {
437- return nil , fmt .Errorf ("failed to build left subtree: %w" , err )
438- }
439- leftNode = leftSubtree
440- } else {
441- leftNode , err = processLeaf (topology [0 ])
442- if err != nil {
443- return nil , fmt .Errorf ("failed to process left leaf: %w" , err )
444- }
445- }
446-
447- // Check if the second element is an array
448- if rightArray , isArray := topology [1 ].([]interface {}); isArray {
449- if len (rightArray ) != 2 {
450- return nil , fmt .Errorf ("right topology row must have exactly two elements" )
451- }
452- rightSubtree , err := buildSubtree (rightArray )
453- if err != nil {
454- return nil , fmt .Errorf ("failed to build right subtree: %w" , err )
455- }
456- rightNode = rightSubtree
457- } else {
458- rightNode , err = processLeaf (topology [1 ])
459- if err != nil {
460- return nil , fmt .Errorf ("failed to process right leaf: %w" , err )
461- }
462- }
463-
464- tree := map [string ]interface {}{
465- "left" : leftNode ,
466- "right" : rightNode ,
458+ if topology := rawConfig ["topology" ]; topology != nil {
459+ tree , err := buildTree (topology )
460+ if err != nil {
461+ return nil , fmt .Errorf ("failed to build tree from topology: %w" , err )
467462 }
468463 rawConfig ["tree" ] = tree
469464 delete (rawConfig , "topology" )
@@ -472,47 +467,6 @@ func handleRawConfig(input []byte) (map[string]interface{}, error) {
472467 return rawConfig , nil
473468}
474469
475- // buildSubtree builds a subtree from an array of two elements
476- func buildSubtree (row []interface {}) (map [string ]interface {}, error ) {
477- if len (row ) != 2 {
478- return nil , fmt .Errorf ("subtree row must have exactly two elements" )
479- }
480- leftItem , ok := row [0 ].(map [string ]interface {})
481- if ! ok {
482- return nil , fmt .Errorf ("invalid left item in subtree" )
483- }
484- leftNode , err := convertTree (leftItem )
485- if err != nil {
486- return nil , fmt .Errorf ("failed to convert left tree item: %w" , err )
487- }
488- rightItem , ok := row [1 ].(map [string ]interface {})
489- if ! ok {
490- return nil , fmt .Errorf ("invalid right item in subtree" )
491- }
492- rightNode , err := convertTree (rightItem )
493- if err != nil {
494- return nil , fmt .Errorf ("failed to convert right tree item: %w" , err )
495- }
496- return map [string ]interface {}{
497- "left" : leftNode ,
498- "right" : rightNode ,
499- }, nil
500- }
501-
502- func processLeaf (item interface {}) (map [string ]interface {}, error ) {
503- switch v := item .(type ) {
504- case map [string ]interface {}:
505- return convertTree (v )
506- case string :
507- return map [string ]interface {}{
508- "type" : "node" ,
509- "hash" : v ,
510- }, nil
511- default :
512- return nil , fmt .Errorf ("unsupported leaf type: %T" , v )
513- }
514- }
515-
516470// calculateImageHash calculates the image hash for a given configuration
517471func calculateImageHash (params * ConfigImageHashParams ) (string , error ) {
518472 rawConfig , err := handleRawConfig (params .Input )
0 commit comments