Files
masonry/examples/lang/debug.go

472 lines
13 KiB
Go

package main
import (
"fmt"
"masonry/lang"
"os"
)
func main() {
// Read the example.masonry file
content, err := os.ReadFile("example.masonry")
if err != nil {
fmt.Printf("Error reading example.masonry: %v\n", err)
return
}
input := string(content)
ast, err := lang.ParseInput(input)
if err != nil {
fmt.Printf("Error: %v\n", err)
} else {
fmt.Printf("🎉 Successfully parsed enhanced DSL with containers and detailed fields!\n\n")
for _, def := range ast.Definitions {
if def.Server != nil {
fmt.Printf("📡 Server: %s\n", def.Server.Name)
for _, setting := range def.Server.Settings {
if setting.Host != nil {
fmt.Printf(" host: %s\n", *setting.Host)
}
if setting.Port != nil {
fmt.Printf(" port: %d\n", *setting.Port)
}
}
fmt.Printf("\n")
}
if def.Entity != nil {
entity := def.Entity
fmt.Printf("🏗️ Entity: %s", entity.Name)
if entity.Description != nil {
fmt.Printf(" - %s", *entity.Description)
}
fmt.Printf("\n")
for _, field := range entity.Fields {
fmt.Printf(" %s: %s", field.Name, field.Type)
if field.Required {
fmt.Printf(" (required)")
}
if field.Unique {
fmt.Printf(" (unique)")
}
if field.Default != nil {
fmt.Printf(" default=%s", *field.Default)
}
if field.Relationship != nil {
fmt.Printf(" relates to %s as %s", field.Relationship.Type, field.Relationship.Cardinality)
if field.Relationship.ForeignKey != nil {
fmt.Printf(" via %s", *field.Relationship.ForeignKey)
}
if field.Relationship.Through != nil {
fmt.Printf(" through %s", *field.Relationship.Through)
}
}
if len(field.Validations) > 0 {
fmt.Printf(" validates: ")
for i, val := range field.Validations {
if i > 0 {
fmt.Printf(", ")
}
fmt.Printf("%s", val.Type)
if val.Value != nil {
fmt.Printf("(%s)", *val.Value)
}
}
}
fmt.Printf("\n")
}
fmt.Printf("\n")
}
if def.Endpoint != nil {
endpoint := def.Endpoint
fmt.Printf("🚀 Endpoint: %s %s", endpoint.Method, endpoint.Path)
if endpoint.Entity != nil {
fmt.Printf(" (for %s)", *endpoint.Entity)
}
if endpoint.Description != nil {
fmt.Printf(" - %s", *endpoint.Description)
}
if endpoint.Auth {
fmt.Printf(" [AUTH]")
}
fmt.Printf("\n")
for _, param := range endpoint.Params {
fmt.Printf(" param %s: %s from %s", param.Name, param.Type, param.Source)
if param.Required {
fmt.Printf(" (required)")
}
fmt.Printf("\n")
}
if endpoint.Response != nil {
fmt.Printf(" returns %s", endpoint.Response.Type)
if endpoint.Response.Format != nil {
fmt.Printf(" as %s", *endpoint.Response.Format)
}
if len(endpoint.Response.Fields) > 0 {
fmt.Printf(" fields: %v", endpoint.Response.Fields)
}
fmt.Printf("\n")
}
if endpoint.CustomLogic != nil {
fmt.Printf(" custom logic: %s\n", *endpoint.CustomLogic)
}
fmt.Printf("\n")
}
if def.Page != nil {
page := def.Page
fmt.Printf("🎨 Page: %s at %s", page.Name, page.Path)
if page.Title != nil {
fmt.Printf(" - %s", *page.Title)
}
if page.Auth {
fmt.Printf(" [AUTH]")
}
fmt.Printf("\n")
fmt.Printf(" Layout: %s\n", page.Layout)
if page.LayoutType != nil {
fmt.Printf(" Layout Type: %s\n", *page.LayoutType)
}
for _, meta := range page.Meta {
fmt.Printf(" Meta %s: %s\n", meta.Name, meta.Content)
}
// Display containers
for _, container := range page.Containers {
fmt.Printf(" 📦 Container: %s", container.Type)
if container.Class != nil {
fmt.Printf(" class=\"%s\"", *container.Class)
}
fmt.Printf("\n")
for _, section := range container.Sections {
fmt.Printf(" 📂 Section: %s", section.Name)
if section.Class != nil {
fmt.Printf(" class=\"%s\"", *section.Class)
}
fmt.Printf("\n")
for _, comp := range section.Components {
displayComponent(comp, " ")
}
for _, panel := range section.Panels {
fmt.Printf(" 🗂️ Panel: %s trigger=\"%s\"", panel.Name, panel.Trigger)
if panel.Position != nil {
fmt.Printf(" position=\"%s\"", *panel.Position)
}
fmt.Printf("\n")
for _, comp := range panel.Components {
displayComponent(comp, " ")
}
}
}
for _, tab := range container.Tabs {
fmt.Printf(" 📋 Tab: %s label=\"%s\"", tab.Name, tab.Label)
if tab.Active {
fmt.Printf(" (active)")
}
fmt.Printf("\n")
for _, comp := range tab.Components {
displayComponent(comp, " ")
}
}
for _, comp := range container.Components {
displayComponent(comp, " ")
}
}
// Display direct components
for _, comp := range page.Components {
displayComponent(comp, " ")
}
// Display modals
for _, modal := range page.Modals {
fmt.Printf(" 🪟 Modal: %s trigger=\"%s\"\n", modal.Name, modal.Trigger)
for _, comp := range modal.Components {
displayComponent(comp, " ")
}
}
// Display master-detail layout
if page.MasterDetail != nil {
fmt.Printf(" 🔄 Master-Detail Layout\n")
if page.MasterDetail.Master != nil {
fmt.Printf(" 📋 Master: %s\n", page.MasterDetail.Master.Name)
for _, comp := range page.MasterDetail.Master.Components {
displayComponent(comp, " ")
}
}
if page.MasterDetail.Detail != nil {
fmt.Printf(" 📄 Detail: %s", page.MasterDetail.Detail.Name)
if page.MasterDetail.Detail.Trigger != nil {
fmt.Printf(" trigger=\"%s\"", *page.MasterDetail.Detail.Trigger)
}
fmt.Printf("\n")
for _, comp := range page.MasterDetail.Detail.Components {
displayComponent(comp, " ")
}
}
}
fmt.Printf("\n")
}
}
}
}
func displayComponent(comp lang.Component, indent string) {
fmt.Printf("%s🧩 Component: %s", indent, comp.Type)
if comp.Entity != nil {
fmt.Printf(" for %s", *comp.Entity)
}
fmt.Printf("\n")
// Process elements to extract fields, config, conditions, sections, and actions
var fields []lang.ComponentField
var config []lang.ComponentAttr
var conditions []lang.WhenCondition
var sections []lang.ComponentSection
var actions []lang.ComponentButtonAttr
for _, element := range comp.Elements {
if element.Field != nil {
fields = append(fields, *element.Field)
}
if element.Config != nil {
config = append(config, *element.Config)
}
if element.Condition != nil {
conditions = append(conditions, *element.Condition)
}
if element.Section != nil {
sections = append(sections, *element.Section)
}
if element.Action != nil {
actions = append(actions, *element.Action)
}
}
// Display config attributes
for _, attr := range config {
if attr.Fields != nil {
fmt.Printf("%s fields: %v\n", indent, attr.Fields.Fields)
}
if attr.Actions != nil {
fmt.Printf("%s actions: ", indent)
for i, action := range attr.Actions.Actions {
if i > 0 {
fmt.Printf(", ")
}
fmt.Printf("%s", action.Name)
if action.Endpoint != nil {
fmt.Printf(" via %s", *action.Endpoint)
}
}
fmt.Printf("\n")
}
if attr.DataSource != nil {
fmt.Printf("%s data from: %s\n", indent, attr.DataSource.Endpoint)
}
if attr.Style != nil {
fmt.Printf("%s style: %s", indent, *attr.Style.Theme)
if len(attr.Style.Classes) > 0 {
fmt.Printf(" classes: %v", attr.Style.Classes)
}
fmt.Printf("\n")
}
if attr.Pagination != nil {
fmt.Printf("%s pagination: enabled", indent)
if attr.Pagination.PageSize != nil {
fmt.Printf(" size %d", *attr.Pagination.PageSize)
}
fmt.Printf("\n")
}
if attr.Filters != nil {
fmt.Printf("%s filters: ", indent)
for i, filter := range attr.Filters.Filters {
if i > 0 {
fmt.Printf(", ")
}
fmt.Printf("%s as %s", filter.Field, filter.Type)
if filter.Label != nil {
fmt.Printf(" (%s)", *filter.Label)
}
}
fmt.Printf("\n")
}
if attr.Validation {
fmt.Printf("%s validation: enabled\n", indent)
}
}
// Display enhanced fields
for _, field := range fields {
fmt.Printf("%s 📝 Field: %s type %s", indent, field.Name, field.Type)
// Process attributes
for _, attr := range field.Attributes {
if attr.Label != nil {
fmt.Printf(" label=\"%s\"", *attr.Label)
}
if attr.Placeholder != nil {
fmt.Printf(" placeholder=\"%s\"", *attr.Placeholder)
}
if attr.Required {
fmt.Printf(" (required)")
}
if attr.Sortable {
fmt.Printf(" (sortable)")
}
if attr.Searchable {
fmt.Printf(" (searchable)")
}
if attr.Thumbnail {
fmt.Printf(" (thumbnail)")
}
if attr.Default != nil {
fmt.Printf(" default=\"%s\"", *attr.Default)
}
if len(attr.Options) > 0 {
fmt.Printf(" options=%v", attr.Options)
}
if attr.Accept != nil {
fmt.Printf(" accept=\"%s\"", *attr.Accept)
}
if attr.Rows != nil {
fmt.Printf(" rows=%d", *attr.Rows)
}
if attr.Format != nil {
fmt.Printf(" format=\"%s\"", *attr.Format)
}
if attr.Size != nil {
fmt.Printf(" size=\"%s\"", *attr.Size)
}
if attr.Source != nil {
fmt.Printf(" source=\"%s\"", *attr.Source)
}
if attr.Display != nil {
fmt.Printf(" display=\"%s\"", *attr.Display)
}
if attr.Value != nil {
fmt.Printf(" value=\"%s\"", *attr.Value)
}
if attr.Relates != nil {
fmt.Printf(" relates to %s", attr.Relates.Type)
}
if attr.Validation != nil {
fmt.Printf(" validates: %s", attr.Validation.Type)
if attr.Validation.Value != nil {
fmt.Printf("(%s)", *attr.Validation.Value)
}
}
}
fmt.Printf("\n")
}
// Display conditions
for _, condition := range conditions {
fmt.Printf("%s 🔀 When %s %s \"%s\":\n", indent, condition.Field, condition.Operator, condition.Value)
for _, field := range condition.Fields {
fmt.Printf("%s 📝 Field: %s type %s", indent, field.Name, field.Type)
// Process attributes for conditional fields
for _, attr := range field.Attributes {
if attr.Label != nil {
fmt.Printf(" label=\"%s\"", *attr.Label)
}
if len(attr.Options) > 0 {
fmt.Printf(" options=%v", attr.Options)
}
}
fmt.Printf("\n")
}
for _, section := range condition.Sections {
fmt.Printf("%s 📂 Section: %s\n", indent, section.Name)
}
for _, button := range condition.Buttons {
fmt.Printf("%s 🔘 Button: %s label=\"%s\"", indent, button.Name, button.Label)
if button.Style != nil {
fmt.Printf(" style=\"%s\"", *button.Style)
}
fmt.Printf("\n")
}
}
// Display sections
for _, section := range sections {
fmt.Printf("%s 📂 Section: %s", indent, section.Name)
if section.Class != nil {
fmt.Printf(" class=\"%s\"", *section.Class)
}
fmt.Printf("\n")
for _, field := range section.Fields {
fmt.Printf("%s 📝 Field: %s type %s", indent, field.Name, field.Type)
// Process attributes for section fields
for _, attr := range field.Attributes {
if attr.Label != nil {
fmt.Printf(" label=\"%s\"", *attr.Label)
}
}
fmt.Printf("\n")
}
for _, button := range section.Buttons {
fmt.Printf("%s 🔘 Button: %s label=\"%s\"", indent, button.Name, button.Label)
if button.Style != nil {
fmt.Printf(" style=\"%s\"", *button.Style)
}
if button.Via != nil {
fmt.Printf(" via=\"%s\"", *button.Via)
}
fmt.Printf("\n")
}
for _, action := range section.Actions {
fmt.Printf("%s ⚡ Action: %s label=\"%s\"", indent, action.Name, action.Label)
if action.Style != nil {
fmt.Printf(" style=\"%s\"", *action.Style)
}
fmt.Printf("\n")
}
}
// Display direct actions/buttons
for _, action := range actions {
fmt.Printf("%s 🔘 Button: %s label=\"%s\"", indent, action.Name, action.Label)
if action.Style != nil {
fmt.Printf(" style=\"%s\"", *action.Style)
}
if action.Icon != nil {
fmt.Printf(" icon=\"%s\"", *action.Icon)
}
if action.Loading != nil {
fmt.Printf(" loading=\"%s\"", *action.Loading)
}
if action.Disabled != nil {
fmt.Printf(" disabled when %s", *action.Disabled)
}
if action.Confirm != nil {
fmt.Printf(" confirm=\"%s\"", *action.Confirm)
}
if action.Target != nil {
fmt.Printf(" target=\"%s\"", *action.Target)
}
if action.Position != nil {
fmt.Printf(" position=\"%s\"", *action.Position)
}
if action.Via != nil {
fmt.Printf(" via=\"%s\"", *action.Via)
}
fmt.Printf("\n")
}
}