probe

package
v0.0.0-...-64443f4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 19, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const AssociationFieldToFormTemplate = `` /* 2946-byte string literal not displayed */
View Source
const AssociationSliceToFormTemplate = `` /* 6568-byte string literal not displayed */
View Source
const BasicFieldtoFormTemplate = `` /* 6230-byte string literal not displayed */
View Source
const ButtonImplGongstructFileTemplate = `` /* 1021-byte string literal not displayed */
View Source
const ButtonImplRefreshFileTemplate = `` /* 575-byte string literal not displayed */
View Source
const CellDeleteIconImplTemplate = `// generated code - do not edit
package probe

import (

	gongtable "github.com/fullstack-lang/gong/lib/table/go/models"

	"{{PkgPathRoot}}/models"
)

func NewCellDeleteIconImpl[T models.Gongstruct](
	Instance *T,
	probe *Probe,
) (cellDeleteIconImpl *CellDeleteIconImpl[T]) {
	cellDeleteIconImpl = new(CellDeleteIconImpl[T])
	cellDeleteIconImpl.Instance = Instance
	cellDeleteIconImpl.probe = probe
	return
}

type CellDeleteIconImpl[T models.Gongstruct] struct {
	Instance *T
	probe    *Probe
}

func (cellDeleteIconImpl *CellDeleteIconImpl[T]) CellIconUpdated(stage *gongtable.Stage,
	row, updatedCellIcon *gongtable.CellIcon) {
	// log.Println("CellIconUpdate: CellIconUpdated", updatedCellIcon.Name)

	switch instancesTyped := any(cellDeleteIconImpl.Instance).(type) {
	// insertion point{{` + string(rune(CellDeleteIconImplSwitchCase)) + `}}
	default:
		_ = instancesTyped
	}
	cellDeleteIconImpl.probe.stageOfInterest.Commit()

	updateAndCommitTable[T](cellDeleteIconImpl.probe)
	updateAndCommitTree(cellDeleteIconImpl.probe)
	cellDeleteIconImpl.probe.tableStage.Commit()
}
`
View Source
const EnumTypeStringToForm = `` /* 2175-byte string literal not displayed */
View Source
const FillUpFormFromGongstructNameTemplate = `// generated code - do not edit
package probe

import (
	form "github.com/fullstack-lang/gong/lib/table/go/models"

	"{{PkgPathRoot}}/models"
)

func FillUpFormFromGongstructName(
	probe *Probe,
	gongstructName string,
	isNewInstance bool,
) {
	formStage := probe.formStage
	formStage.Reset()

	var prefix string

	if isNewInstance {
		prefix = ""
	} else {
		prefix = ""
	}

	switch gongstructName {
	// insertion point{{` + string(rune(FillUpFormFromGongstructNameSwitchCase)) + `}}
	}
	formStage.Commit()
}
`
View Source
const FillUpFormFromGongstructTemplate = `// generated code - do not edit
package probe

import (
	gongtable "github.com/fullstack-lang/gong/lib/table/go/models"

	"{{PkgPathRoot}}/models"
)

func FillUpFormFromGongstruct(instance any, probe *Probe) {
	formStage := probe.formStage
	formStage.Reset()

	FillUpNamedFormFromGongstruct(instance, probe, formStage, FormName)

}

func FillUpNamedFormFromGongstruct(instance any, probe *Probe, formStage *gongtable.Stage, formName string) {

	switch instancesTyped := any(instance).(type) {
	// insertion point{{` + string(rune(FillUpFormFromGongstructSwitchCase)) + `}}
	default:
		_ = instancesTyped
	}
	formStage.Commit()
}
`
View Source
const FillUpFormTemplate = `// generated code - do not edit
package probe

import (
	form "github.com/fullstack-lang/gong/lib/table/go/models"

	"{{PkgPathRoot}}/models"
)

const FormName = "Form"

func FillUpForm(
	instance any,
	formGroup *form.FormGroup,
	probe *Probe,
) {

	switch instanceWithInferedType := any(instance).(type) {
	// insertion point{{` + string(rune(ButtonImplPerGongstructCallToFormGenerator)) + `}}
	default:
		_ = instanceWithInferedType
	}
}
`
View Source
const FillUpTableTemplate = `// generated code - do not edit
package probe

import (
	"fmt"
	"log"
	"sort"

	gongtable "github.com/fullstack-lang/gong/lib/table/go/models"

	"github.com/fullstack-lang/maticons/maticons"

	"{{PkgPathRoot}}/models"
)

func updateAndCommitTablePointerToGongstruct[T models.PointerToGongstruct](
	probe *Probe,
) {
	var typedInstance T
	switch any(typedInstance).(type) {
	// insertion point{{` + string(rune(FillUpTableCaseForCastingDown)) + `}}
	default:
		log.Println("unknow type")
	}
}

const TableName = "Table"

func updateAndCommitTable[T models.Gongstruct](
	probe *Probe,
) {

	probe.tableStage.Reset()

	table := new(gongtable.Table)
	table.Name = TableName
	table.HasColumnSorting = true
	table.HasFiltering = true
	table.HasPaginator = true
	table.HasCheckableRows = false
	table.HasSaveButton = false

	fields := models.GetFields[T]()
	reverseFields := models.GetReverseFields[T]()

	table.NbOfStickyColumns = 3

	// refresh the stage of interest
	probe.stageOfInterest.Checkout()

	setOfStructs := (*models.GetGongstructInstancesSet[T](probe.stageOfInterest))
	sliceOfGongStructsSorted := make([]*T, len(setOfStructs))
	i := 0
	for k := range setOfStructs {
		sliceOfGongStructsSorted[i] = k
		i++
	}
	sort.Slice(sliceOfGongStructsSorted, func(i, j int) bool {
		return models.GetOrder(probe.stageOfInterest, sliceOfGongStructsSorted[i]) <
			models.GetOrder(probe.stageOfInterest, sliceOfGongStructsSorted[j])
	})

	column := new(gongtable.DisplayedColumn)
	column.Name = "ID"
	table.DisplayedColumns = append(table.DisplayedColumns, column)

	column = new(gongtable.DisplayedColumn)
	column.Name = "Delete"
	table.DisplayedColumns = append(table.DisplayedColumns, column)

	for _, fieldName := range fields {
		column := new(gongtable.DisplayedColumn)
		column.Name = fieldName
		table.DisplayedColumns = append(table.DisplayedColumns, column)
	}
	for _, reverseField := range reverseFields {
		column := new(gongtable.DisplayedColumn)
		column.Name = "(" + reverseField.GongstructName + ") -> " + reverseField.Fieldname
		table.DisplayedColumns = append(table.DisplayedColumns, column)
	}

	fieldIndex := 0
	for _, structInstance := range sliceOfGongStructsSorted {
		row := new(gongtable.Row)
		value := models.GetFieldStringValue(*structInstance, "Name")
		row.Name = value.GetValueString()

		updater := NewRowUpdate(structInstance, probe)
		updater.Instance = structInstance
		row.Impl = updater

		table.Rows = append(table.Rows, row)

		cell := &gongtable.Cell{
			Name: "ID",
		}
		row.Cells = append(row.Cells, cell)
		cellInt := &gongtable.CellInt{
			Name: "ID",
			Value: int(models.GetOrder(
				probe.stageOfInterest,
				structInstance,
			)),
		}
		cell.CellInt = cellInt

		cell = &gongtable.Cell{
			Name: "Delete Icon",
		}
		row.Cells = append(row.Cells, cell)
		cellIcon := &gongtable.CellIcon{
			Name: fmt.Sprintf("Delete Icon %d", models.GetOrder(
				probe.stageOfInterest,
				structInstance,
			)),
			Icon:                string(maticons.BUTTON_delete),
			NeedsConfirmation:   true,
			ConfirmationMessage: "Do you confirm tou want to delete this instance ?",
		}
		cellIcon.Impl = NewCellDeleteIconImpl(structInstance, probe)
		cell.CellIcon = cellIcon

		for _, fieldName := range fields {
			value := models.GetFieldStringValue(*structInstance, fieldName)
			name := fmt.Sprintf("%d", fieldIndex) + " " + value.GetValueString()
			fieldIndex++
			// log.Println(fieldName, value)
			cell := &gongtable.Cell{
				Name: name,
			}
			row.Cells = append(row.Cells, cell)

			switch value.GongFieldValueType {
			case models.GongFieldValueTypeInt:
				cellInt := &gongtable.CellInt{
					Name:  name,
					Value: value.GetValueInt(),
				}
				cell.CellInt = cellInt
			case models.GongFieldValueTypeFloat:
				cellFloat := &gongtable.CellFloat64{
					Name:  name,
					Value: value.GetValueFloat(),
				}
				cell.CellFloat64 = cellFloat
			case models.GongFieldValueTypeBool:
				cellBool := &gongtable.CellBoolean{
					Name:  name,
					Value: value.GetValueBool(),
				}
				cell.CellBool = cellBool
			default:
				cellString := &gongtable.CellString{
					Name:  name,
					Value: value.GetValueString(),
				}
				cell.CellString = cellString

			}
		}
		for _, reverseField := range reverseFields {

			value := models.GetReverseFieldOwnerName(
				probe.stageOfInterest,
				structInstance,
				&reverseField)
			name := fmt.Sprintf("%d", fieldIndex) + " " + value
			fieldIndex++
			// log.Println(fieldName, value)
			cell := &gongtable.Cell{
				Name: name,
			}
			row.Cells = append(row.Cells, cell)

			cellString := &gongtable.CellString{
				Name:  name,
				Value: value,
			}
			cell.CellString = cellString
		}
	}

	gongtable.StageBranch(probe.tableStage, table)

}

func NewRowUpdate[T models.Gongstruct](
	Instance *T,
	probe *Probe,
) (rowUpdate *RowUpdate[T]) {
	rowUpdate = new(RowUpdate[T])
	rowUpdate.Instance = Instance
	rowUpdate.probe = probe
	return
}

type RowUpdate[T models.Gongstruct] struct {
	Instance *T
	probe    *Probe
}

func (rowUpdate *RowUpdate[T]) RowUpdated(stage *gongtable.Stage, row, updatedRow *gongtable.Row) {
	// log.Println("RowUpdate: RowUpdated", updatedRow.Name)

	FillUpFormFromGongstruct(rowUpdate.Instance, rowUpdate.probe)
}
`
View Source
const FillUpTree = `package probe

import (
	"fmt"
	"sort"
	"strings"
	"time"

	gongtree_buttons "github.com/fullstack-lang/gong/lib/tree/go/buttons"
	tree "github.com/fullstack-lang/gong/lib/tree/go/models"

	gong_models "github.com/fullstack-lang/gong/go/models"

	"{{PkgPathRoot}}/models"
)

func updateAndCommitTree(
	probe *Probe,
) {
	// keep in memory which nodes have been unfolded / folded
	expandedNodesSet := make(map[string]any, 0)
	var _sidebar *tree.Tree
	for __sidebar := range probe.treeStage.Trees {
		_sidebar = __sidebar
	}
	if _sidebar != nil {
		for _, node := range _sidebar.RootNodes {
			if node.IsExpanded {
				expandedNodesSet[strings.Fields(node.Name)[0]] = true
			}
		}
	}

	probe.treeStage.Reset()

	// create tree
	sidebar := &tree.Tree{Name: "Sidebar"}

	nodeRefreshButton := &tree.Node{Name: fmt.Sprintf("Stage %s, # %d, %s",
		probe.stageOfInterest.GetName(),
		probe.stageOfInterest.GetCommitId(),
		probe.stageOfInterest.GetCommitTS().Local().Format(time.Kitchen))}
	sidebar.RootNodes = append(sidebar.RootNodes, nodeRefreshButton)
	refreshButton := &tree.Button{
		Name:            "RefreshButton" + " " + string(gongtree_buttons.BUTTON_refresh),
		Icon:            string(gongtree_buttons.BUTTON_refresh),
		HasToolTip:      true,
		ToolTipText:     "Refresh probe",
		ToolTipPosition: tree.Left,
	}

	nodeRefreshButton.Buttons = append(nodeRefreshButton.Buttons, refreshButton)
	refreshButton.Impl = NewButtonImplRefresh(probe)

	// collect all gong struct to construe the true
	setOfGongStructs := *gong_models.GetGongstructInstancesSet[gong_models.GongStruct](probe.gongStage)

	sliceOfGongStructsSorted := make([]*gong_models.GongStruct, len(setOfGongStructs))
	i := 0
	for k := range setOfGongStructs {
		sliceOfGongStructsSorted[i] = k
		i++
	}
	sort.Slice(sliceOfGongStructsSorted, func(i, j int) bool {
		return sliceOfGongStructsSorted[i].Name < sliceOfGongStructsSorted[j].Name
	})

	for _, gongStruct := range sliceOfGongStructsSorted {

		name := gongStruct.Name + " (" +
			fmt.Sprintf("%d", probe.stageOfInterest.Map_GongStructName_InstancesNb[gongStruct.Name]) + ")"

		nodeGongstruct := &tree.Node{Name: name}
		nodeGongstruct.HasToolTip = true
		nodeGongstruct.ToolTipText = "Display table of all " + name + " instances"
		nodeGongstruct.ToolTipPosition = tree.Right

		nodeGongstruct.IsExpanded = false
		if _, ok := expandedNodesSet[strings.Fields(name)[0]]; ok {
			nodeGongstruct.IsExpanded = true
		}

		switch gongStruct.Name {
		// insertion point{{` + string(rune(FillUpTreeStructCase)) + `}}
		}

		nodeGongstruct.IsNodeClickable = true
		nodeGongstruct.Impl = NewTreeNodeImplGongstruct(gongStruct, probe)

		// add add button
		addButton := &tree.Button{
			Name:            gongStruct.Name + " " + string(gongtree_buttons.BUTTON_add),
			Icon:            string(gongtree_buttons.BUTTON_add),
			HasToolTip:      true,
			ToolTipText:     "Add an instance of " + gongStruct.GetName(),
			ToolTipPosition: tree.Right,
		}
		nodeGongstruct.Buttons = append(nodeGongstruct.Buttons, addButton)
		addButton.Impl = NewButtonImplGongstruct(
			gongStruct,
			gongtree_buttons.BUTTON_add,
			probe,
		)

		sidebar.RootNodes = append(sidebar.RootNodes, nodeGongstruct)
	}

	tree.StageBranch(probe.treeStage, sidebar)

	probe.treeStage.Commit()
}

type InstanceNodeCallback[T models.Gongstruct] struct {
	Instance       *T
	gongstructName string
	probe          *Probe
}

func NewInstanceNodeCallback[T models.Gongstruct](
	instance *T,
	gongstructName string,
	probe *Probe) (
	instanceNodeCallback *InstanceNodeCallback[T],
) {
	instanceNodeCallback = new(InstanceNodeCallback[T])

	instanceNodeCallback.probe = probe
	instanceNodeCallback.gongstructName = gongstructName
	instanceNodeCallback.Instance = instance

	return
}

func (instanceNodeCallback *InstanceNodeCallback[T]) OnAfterUpdate(
	gongtreeStage *tree.Stage,
	stagedNode, frontNode *tree.Node) {

	FillUpFormFromGongstruct(
		instanceNodeCallback.Instance,
		instanceNodeCallback.probe,
	)
}
`
View Source
const FormCallbackGongstructFileTemplate = `// generated code - do not edit
package probe

import (
	"log"
	"slices"
	"time"

	table "github.com/fullstack-lang/gong/lib/table/go/models"

	"{{PkgPathRoot}}/models"
)

// to avoid errors when time and slices packages are not used in the generated code
const _ = time.Nanosecond

var _ = slices.Delete([]string{"a"}, 0, 1)

var _ = log.Panicf

// insertion point{{` + string(rune(FillUpTreeStructCase)) + `}}
`
View Source
const FormDivToFieldTemplate = `` /* 3114-byte string literal not displayed */
View Source
const NewOnSortingEditonTemplate = `` /* 3276-byte string literal not displayed */
View Source
const ProbeTemplate = `` /* 4194-byte string literal not displayed */
View Source
const TreeNodeImplGongstructFileTemplate = `// generated code - do not edit
package probe

import (
	gong_models "github.com/fullstack-lang/gong/go/models"
	gongtree_models "github.com/fullstack-lang/gong/lib/tree/go/models"

	"{{PkgPathRoot}}/models"
)

type TreeNodeImplGongstruct struct {
	gongStruct *gong_models.GongStruct
	probe      *Probe
}

func NewTreeNodeImplGongstruct(
	gongStruct *gong_models.GongStruct,
	probe *Probe,
) (nodeImplGongstruct *TreeNodeImplGongstruct) {

	nodeImplGongstruct = new(TreeNodeImplGongstruct)
	nodeImplGongstruct.gongStruct = gongStruct
	nodeImplGongstruct.probe = probe
	return
}

func (nodeImplGongstruct *TreeNodeImplGongstruct) OnAfterUpdate(
	gongtreeStage *gongtree_models.Stage,
	stagedNode, frontNode *gongtree_models.Node) {

	// setting the value of the staged node	to the new value
	// otherwise, the expansion memory is lost
	if stagedNode.IsExpanded != frontNode.IsExpanded {
		stagedNode.IsExpanded = frontNode.IsExpanded
		return
	}

	// if node is unchecked
	if stagedNode.IsChecked && !frontNode.IsChecked {

	}

	// if node is checked, add gongstructshape
	if !stagedNode.IsChecked && frontNode.IsChecked {

	}

	// the node was selected. Therefore, one request the
	// table to route to the table
	// log.Println("NodeImplGongstruct:OnAfterUpdate with: ", nodeImplGongstruct.gongStruct.GetName())

	// insertion point{{` + string(rune(NodeImplGongstruct)) + `}}

	// set color for node and reset all other nodes color
	for node := range *gongtree_models.GetGongstructInstancesSet[gongtree_models.Node](gongtreeStage) {
		node.BackgroundColor = ""
	}
	stagedNode.BackgroundColor = "lightgrey"
	gongtreeStage.Commit()

	nodeImplGongstruct.probe.tableStage.Commit()
}
`

Variables

View Source
var ButtonImplFileFieldFieldSubTemplateCode map[ButtonImplSubTemplateId]string = map[ButtonImplSubTemplateId]string{

	ButtonImplSubTmplBasicField: `
		BasicFieldtoForm("{{FieldName}}", instanceWithInferedType.{{FieldName}}, instanceWithInferedType, probe.formStage, formGroup,
			{{isTextArea}}, {{isBespokeWidth}}, {{bespokeWidth}}, {{isBespokeHeight}}, {{bespokeHeight}})`,
	ButtonImplSubTmplBasicFieldEnumString: `
		EnumTypeStringToForm("{{FieldName}}", instanceWithInferedType.{{FieldName}}, instanceWithInferedType, probe.formStage, formGroup)`,
	ButtonImplSubTmplBasicFieldEnumInt: `
		EnumTypeIntToForm("{{FieldName}}", instanceWithInferedType.{{FieldName}}, instanceWithInferedType, probe.formStage, formGroup)`,
	ButtonImplSubTmplPointerField: `
		AssociationFieldToForm("{{FieldName}}", instanceWithInferedType.{{FieldName}}, formGroup, probe)`,
	ButtonImplSubTmplSliceOfPointersField: `
		AssociationSliceToForm("{{FieldName}}", instanceWithInferedType, &instanceWithInferedType.{{FieldName}}, formGroup, probe)`,
	ButtonImplSubTmplSliceOfPointersReversePointer: `
		{
			var rf models.ReverseField
			_ = rf
			rf.GongstructName = "{{AssocStructName}}"
			rf.Fieldname = "{{FieldName}}"
			reverseFieldOwner := models.GetReverseFieldOwner(probe.stageOfInterest, instanceWithInferedType, &rf)
			if reverseFieldOwner != nil {
				AssociationReverseFieldToForm(
					reverseFieldOwner.(*models.{{AssocStructName}}),
					"{{FieldName}}",
					instanceWithInferedType,
					formGroup,
					probe)
			} else {
				AssociationReverseFieldToForm[*models.{{AssocStructName}}](
					nil,
					"{{FieldName}}",
					instanceWithInferedType,
					formGroup,
					probe)
			}
		}`,
}
View Source
var CellDeleteIconImplSubTemplateCode map[string]string = map[string]string{
	string(rune(CellDeleteIconImplSwitchCase)): `
	case *models.{{Structname}}:
		instancesTyped.Unstage(cellDeleteIconImpl.probe.stageOfInterest)`,
}
View Source
var FillUpFormFromGongstructNameSubTemplateCode map[string]string = map[string]string{
	string(rune(FillUpTreeStructCase)): `
	case "{{Structname}}":
		formGroup := (&form.FormGroup{
			Name:  FormName,
			Label: prefix + "{{Structname}} Form",
		}).Stage(formStage)
		formGroup.OnSave = __gong__New__{{Structname}}FormCallback(
			nil,
			probe,
			formGroup,
		)
		{{structname}} := new(models.{{Structname}})
		formGroup.HasSuppressButton = !isNewInstance
		FillUpForm({{structname}}, formGroup, probe)`,
}
View Source
var FillUpFormFromGongstructSubTemplateCode map[string]string = map[string]string{
	string(rune(FillUpTreeStructCase)): `
	case *models.{{Structname}}:
		formGroup := (&gongtable.FormGroup{
			Name:  formName,
			Label: "{{Structname}} Form",
		}).Stage(formStage)
		formGroup.OnSave = __gong__New__{{Structname}}FormCallback(
			instancesTyped,
			probe,
			formGroup,
		)
		formGroup.HasSuppressButton = true
		FillUpForm(instancesTyped, formGroup, probe)`,
}
View Source
var FillUpFormSubTemplateCode map[FillUpFormInsertionId]string = map[FillUpFormInsertionId]string{
	ButtonImplPerGongstructCallToFormGenerator: `
	case *models.{{Structname}}:
		// insertion point{{fieldToFormCode}}
`,
}
View Source
var FillUpTableSubTemplateCode map[string]string = map[string]string{

	string(rune(FillUpTableCaseForCastingDown)): `
	case *models.{{Structname}}:
		updateAndCommitTable[models.{{Structname}}](probe)`,
	string(rune(FillUpTableCase)): `
	case *models.{{Structname}}:
		formGroup := (&gongtable.FormGroup{
			Name:  FormName,
			Label: "Update {{Structname}} Form",
			OnSave: __gong__New__{{Structname}}FormCallback(
				instancesTyped,
				rowUpdate.probe,
			),
		}).Stage(formStage)
		FillUpForm(instancesTyped, formGroup, rowUpdate.probe)`,
	string(rune(FillUpTableCaseForDeleteIcon)): `
	case *models.{{Structname}}:
		instancesTyped.Unstage(cellDeleteIconImpl.probe.stageOfInterest)`,
}
View Source
var FillUpTreeSubTemplateCode map[string]string = map[string]string{
	string(rune(FillUpTreeStructCase)): `
		case "{{Structname}}":
			nodeGongstruct.Name = name
			set := *models.GetGongstructInstancesSet[models.{{Structname}}](probe.stageOfInterest)
			for _{{structname}} := range set {
				nodeInstance := &tree.Node{Name: _{{structname}}.GetName()}
				nodeInstance.IsNodeClickable = true
				nodeInstance.Impl = NewInstanceNodeCallback(_{{structname}}, "{{Structname}}", probe)

				nodeGongstruct.Children = append(nodeGongstruct.Children, nodeInstance)
			}`,
}
View Source
var FormCallbackFileFieldFieldSubTemplateCode map[FormCallbackSubTemplateId]string = map[FormCallbackSubTemplateId]string{

	FormCallbackSubTmplBasicField: `
		case "{{FieldName}}":
			FormDivBasicFieldToField(&({{structname}}_.{{FieldName}}), formDiv)`,
	FormCallbackSubTmplBasicFieldEnumString: `
		case "{{FieldName}}":
			FormDivEnumStringFieldToField(&({{structname}}_.{{FieldName}}), formDiv)`,
	FormCallbackSubTmplBasicFieldEnumInt: `
		case "{{FieldName}}":
			FormDivEnumIntFieldToField(&({{structname}}_.{{FieldName}}), formDiv)`,
	FormCallbackSubTmplPointerToStruct: `
		case "{{FieldName}}":
			FormDivSelectFieldToField(&({{structname}}_.{{FieldName}}), {{structname}}FormCallback.probe.stageOfInterest, formDiv)`,
	FormCallbackSubTmplSliceOfPointers: `
		case "{{FieldName}}":
			instanceSet := *models.GetGongstructInstancesSetFromPointerType[*models.{{AssocStructName}}]({{structname}}FormCallback.probe.stageOfInterest)
			instanceSlice := make([]*models.{{AssocStructName}}, 0)

			// make a map of all instances by their ID
			map_id_instances := make(map[uint]*models.{{AssocStructName}})

			for instance := range instanceSet {
				id := models.GetOrderPointerGongstruct(
					{{structname}}FormCallback.probe.stageOfInterest,
					instance,
				)
				map_id_instances[id] = instance
			}

			ids, err := DecodeStringToIntSlice(formDiv.FormEditAssocButton.AssociationStorage)

			if err != nil {
				log.Panic("not a good storage", formDiv.FormEditAssocButton.AssociationStorage)
			}
			for _, id := range ids {
				instanceSlice = append(instanceSlice, map_id_instances[id])
			}
			{{structname}}_.{{FieldName}} = instanceSlice
`,
	FormCallbackSubTmplSliceOfPointersReversePointer: `
		case "{{AssocStructName}}:{{FieldName}}":
			// WARNING : this form deals with the N-N association "{{AssocStructName}}.{{FieldName}} []*{{Structname}}" but
			// it work only for 1-N associations (TODO: #660, enable this form only for field with //gong:1_N magic code)
			//
			// In many use cases, for instance tree structures, the assocation is semanticaly a 1-N
			// association. For those use cases, it is handy to set the source of the assocation with
			// the form of the target source (when editing an instance of {{Structname}}). Setting up a value
			// will discard the former value is there is one.
			//
			// Therefore, the forms works only in ONE particular case:
			// - there was no association to this target
			var formerSource *models.{{AssocStructName}}
			{
				var rf models.ReverseField
				_ = rf
				rf.GongstructName = "{{AssocStructName}}"
				rf.Fieldname = "{{FieldName}}"
				formerAssociationSource := models.GetReverseFieldOwner(
					{{structname}}FormCallback.probe.stageOfInterest,
					{{structname}}_,
					&rf)

				var ok bool
				if formerAssociationSource != nil {
					formerSource, ok = formerAssociationSource.(*models.{{AssocStructName}})
					if !ok {
						log.Fatalln("Source of {{AssocStructName}}.{{FieldName}} []*{{Structname}}, is not an {{AssocStructName}} instance")
					}
				}
			}

			newSourceName := formDiv.FormFields[0].FormFieldSelect.Value

			// case when the user set empty for the source value
			if newSourceName == nil {
				// That could mean we clear the assocation for all source instances
				if formerSource != nil {
					idx := slices.Index(formerSource.{{FieldName}}, {{structname}}_)
					formerSource.{{FieldName}} = slices.Delete(formerSource.{{FieldName}}, idx, idx+1)
				}
				break // nothing else to do for this field
			}

			// the former source is not empty. the new value could
			// be different but there mught more that one source thet
			// points to this target
			if formerSource != nil {
				break // nothing else to do for this field
			}

			// (2) find the source
			var newSource *models.{{AssocStructName}}
			for _{{assocStructName}} := range *models.GetGongstructInstancesSet[models.{{AssocStructName}}]({{structname}}FormCallback.probe.stageOfInterest) {

				// the match is base on the name
				if _{{assocStructName}}.GetName() == newSourceName.GetName() {
					newSource = _{{assocStructName}} // we have a match
					break
				}
			}
			if newSource == nil {
				log.Println("Source of {{AssocStructName}}.{{FieldName}} []*{{Structname}}, with name", newSourceName, ", does not exist")
				break
			}

			// (3) append the new value to the new source field
			newSource.{{FieldName}} = append(newSource.{{FieldName}}, {{structname}}_)`,
}
View Source
var FormCallbackGongstructSubTemplateCode map[FormCallbackGongstructInsertionId]string = map[FormCallbackGongstructInsertionId]string{
	FormCallbackPerGongstructCode: `
func __gong__New__{{Structname}}FormCallback(
	{{structname}} *models.{{Structname}},
	probe *Probe,
	formGroup *table.FormGroup,
) ({{structname}}FormCallback *{{Structname}}FormCallback) {
	{{structname}}FormCallback = new({{Structname}}FormCallback)
	{{structname}}FormCallback.probe = probe
	{{structname}}FormCallback.{{structname}} = {{structname}}
	{{structname}}FormCallback.formGroup = formGroup

	{{structname}}FormCallback.CreationMode = ({{structname}} == nil)

	return
}

type {{Structname}}FormCallback struct {
	{{structname}} *models.{{Structname}}

	// If the form call is called on the creation of a new instnace
	CreationMode bool

	probe *Probe

	formGroup *table.FormGroup
}

func ({{structname}}FormCallback *{{Structname}}FormCallback) OnSave() {

	// log.Println("{{Structname}}FormCallback, OnSave")

	// checkout formStage to have the form group on the stage synchronized with the
	// back repo (and front repo)
	{{structname}}FormCallback.probe.formStage.Checkout()

	if {{structname}}FormCallback.{{structname}} == nil {
		{{structname}}FormCallback.{{structname}} = new(models.{{Structname}}).Stage({{structname}}FormCallback.probe.stageOfInterest)
	}
	{{structname}}_ := {{structname}}FormCallback.{{structname}}
	_ = {{structname}}_

	for _, formDiv := range {{structname}}FormCallback.formGroup.FormDivs {
		switch formDiv.Name {
		// insertion point per field{{fieldToFormCode}}
		}
	}

	// manage the suppress operation
	if {{structname}}FormCallback.formGroup.HasSuppressButtonBeenPressed {
		{{structname}}_.Unstage({{structname}}FormCallback.probe.stageOfInterest)
	}

	{{structname}}FormCallback.probe.stageOfInterest.Commit()
	updateAndCommitTable[models.{{Structname}}](
		{{structname}}FormCallback.probe,
	)
	{{structname}}FormCallback.probe.tableStage.Commit()

	// display a new form by reset the form stage
	if {{structname}}FormCallback.CreationMode || {{structname}}FormCallback.formGroup.HasSuppressButtonBeenPressed {
		{{structname}}FormCallback.probe.formStage.Reset()
		newFormGroup := (&table.FormGroup{
			Name: FormName,
		}).Stage({{structname}}FormCallback.probe.formStage)
		newFormGroup.OnSave = __gong__New__{{Structname}}FormCallback(
			nil,
			{{structname}}FormCallback.probe,
			newFormGroup,
		)
		{{structname}} := new(models.{{Structname}})
		FillUpForm({{structname}}, newFormGroup, {{structname}}FormCallback.probe)
		{{structname}}FormCallback.probe.formStage.Commit()
	}

	updateAndCommitTree({{structname}}FormCallback.probe)
}`,
}
View Source
var TreeNodeImplGongstructSubTemplateCode map[string]string = map[string]string{
	string(rune(NodeImplGongstruct)): `
	if nodeImplGongstruct.gongStruct.GetName() == "{{Structname}}" {
		updateAndCommitTable[models.{{Structname}}](nodeImplGongstruct.probe)
	}`,
}

Functions

func CodeGeneratorFillUpForm

func CodeGeneratorFillUpForm(
	modelPkg *models.ModelPkg,
	pkgName string,
	pkgPath string,
	pkgGoPath string)

func CodeGeneratorModelFormCallback

func CodeGeneratorModelFormCallback(
	modelPkg *models.ModelPkg,
	pkgName string,
	pkgPath string,
	pkgGoPath string)

Types

type ButtonImplSubTemplateId

type ButtonImplSubTemplateId int
const (
	ButtonImplSubTmplBasicField ButtonImplSubTemplateId = iota
	// ButtonImplFileFieldSubTmplSetBasicFieldInt
	ButtonImplSubTmplBasicFieldEnumString
	ButtonImplSubTmplBasicFieldEnumInt
	// ButtonImplFileFieldSubTmplSetBasicFieldFloat64
	// ButtonImplFileFieldSubTmplSetBasicFieldString
	// ButtonImplFileFieldSubTmplSetBasicFieldStringDocLink
	// ButtonImplFileFieldSubTmplSetTimeField
	ButtonImplSubTmplPointerField
	ButtonImplSubTmplSliceOfPointersField
	ButtonImplSubTmplSliceOfPointersReversePointer
)

type CellDeleteIconImplInsertionId

type CellDeleteIconImplInsertionId int
const (
	CellDeleteIconImplSwitchCase CellDeleteIconImplInsertionId = iota
	CellDeleteIconImplInsertionNb
)

type FillUpFormFromGongstructInsertionId

type FillUpFormFromGongstructInsertionId int
const (
	FillUpFormFromGongstructSwitchCase FillUpFormFromGongstructInsertionId = iota
	FillUpFormFromGongstructInsertionNb
)

type FillUpFormFromGongstructNameInsertionId

type FillUpFormFromGongstructNameInsertionId int
const (
	FillUpFormFromGongstructNameSwitchCase FillUpFormFromGongstructNameInsertionId = iota
	FillUpFormFromGongstructNameInsertionNb
)

type FillUpFormInsertionId

type FillUpFormInsertionId int
const (
	ButtonImplPerGongstructCallToFormGenerator FillUpFormInsertionId = iota
	FillUpFormInsertionNb
)

type FillUpTableInsertionId

type FillUpTableInsertionId int
const (
	FillUpTableCase FillUpTableInsertionId = iota
	FillUpTableCaseForCastingDown
	FillUpTableCaseForDeleteIcon
)

type FillUpTreeInsertionId

type FillUpTreeInsertionId int
const (
	FillUpTreeStructCase FillUpTreeInsertionId = iota
	FillUpTreeInsertionNb
)

type FormCallbackGongstructInsertionId

type FormCallbackGongstructInsertionId int
const (
	FormCallbackPerGongstructCode FormCallbackGongstructInsertionId = iota
	FormCallbackGongstructInsertionNb
)

type FormCallbackSubTemplateId

type FormCallbackSubTemplateId int
const (
	FormCallbackSubTmplBasicField FormCallbackSubTemplateId = iota
	FormCallbackSubTmplBasicFieldEnumString
	FormCallbackSubTmplBasicFieldEnumInt
	FormCallbackSubTmplPointerToStruct
	FormCallbackSubTmplSliceOfPointers
	FormCallbackSubTmplSliceOfPointersReversePointer
)

type NodeImplGongstructInsertionId

type NodeImplGongstructInsertionId int
const (
	NodeImplGongstruct NodeImplGongstructInsertionId = iota
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL