{"modulePath":"github.com/bitechdev/ResolveSpec","version":"v1.3.14","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package reflection\n\n## Functions\n\n```go\nfunc BuildJSONToDBColumnMap(modelType reflect.Type) map[string]string\n```\nBuildJSONToDBColumnMap returns a map from JSON key names to database column names for the given model type. Only writable, non-relation fields are included. This is used to translate incoming request data (keyed by JSON names) into properly named database columns before insert/update operations.\n\n```go\nfunc ConvertToNumericType(value string, kind reflect.Kind) (interface{}, error)\n```\nConvertToNumericType converts a string value to the appropriate numeric type\n\n```go\nfunc ExtractColumnFromBunTag(tag string) string\n```\nExtractColumnFromBunTag extracts the column name from a bun tag Example: \"id,pk\" -\u003e \"id\" Example: \",pk\" -\u003e \"\" (will fall back to json tag)\n\n```go\nfunc ExtractColumnFromGormTag(tag string) string\n```\nExtractColumnFromGormTag extracts the column name from a gorm tag Example: \"column:id;primaryKey\" -\u003e \"id\"\n\n```go\nfunc ExtractSourceColumn(colName string) string\n```\nExtractSourceColumn extracts the base column name from PostgreSQL JSON operators Examples:\n\n  - \"columna-\u003e\u003e'val'\" returns \"columna\"\n  - \"columna-\u003e'key'\" returns \"columna\"\n  - \"columna\" returns \"columna\"\n  - \"table.columna-\u003e\u003e'val'\" returns \"table.columna\"\n\n```go\nfunc ExtractTableNameOnly(fullName string) string\n```\nExtractTableNameOnly extracts the table name from a fully qualified table reference. It removes any schema prefix (e.g., \"schema.table\" -\u003e \"table\") and truncates at the first delimiter (comma, space, tab, or newline). If the input contains multiple dots, it returns everything after the last dot up to the first delimiter.\n\n```go\nfunc GetColumnName(field reflect.StructField) string\n```\nGetColumnName extracts the database column name from a struct field. Priority: bun tag -\u003e gorm tag -\u003e json tag -\u003e lowercase field name. This is the exported version for use by other packages.\n\n```go\nfunc GetColumnSQLTypeName(model interface{}, colName string) (string, bool)\n```\nGetColumnSQLTypeName returns the canonical PostgreSQL type name for a column backed by a spectypes wrapper (e.g. \"geometry\", \"vector\", \"jsonb\"), or (\"\", false) if the column is not found or not a spectypes type.\n\n```go\nfunc GetColumnTypeFromModel(model interface{}, colName string) reflect.Kind\n```\nGetColumnTypeFromModel uses reflection to determine the Go type of a column in a model\n\n```go\nfunc GetForeignKeyColumn(modelType reflect.Type, parentKey string) []string\n```\nGetForeignKeyColumn returns the DB column names of the foreign key(s) that relate parentKey to modelType. Composite keys (e.g. bun \"join:a=b,join:c=d\" or GORM \"foreignKey:ColA,ColB\") yield multiple entries. Returns nil when no tag is found (caller should fall back to convention).\n\nTwo lookup strategies are tried in order:\n\n 1. Relation-field match: find a field whose name/json equals parentKey, then read its bun join: or GORM foreignKey: tag and return the local columns. e.g. parentKey=\"department\", field \\`Department bun:\"join:dept\\_id=id\"\\` → \\[\"dept\\_id\"]\n\n 2. Join left-side scan: scan every bun join tag in the struct for pairs whose left side equals parentKey and return the right-side (child FK) columns. e.g. parentKey=\"rid\\_mastertaskitem\", field \\`Children bun:\"join:rid\\_mastertaskitem=rid\\_parentmastertaskitem\"\\` → \\[\"rid\\_parentmastertaskitem\"] Strategy 1 is skipped if the matched field is a declared relation (rel:) or has a GORM tag but carries no explicit FK — callers should use convention.\n\n```go\nfunc GetJSONNameForField(modelType reflect.Type, fieldName string) string\n```\nGetJSONNameForField gets the JSON tag name for a struct field. Returns the JSON field name from the json struct tag, or an empty string if not found. Handles the \"json\" tag format: \"name\", \"name,omitempty\", etc.\n\n```go\nfunc GetModelColumns(model any) []string\n```\nGetModelColumns extracts all column names from a model using reflection It checks bun tags first, then gorm tags, then json tags, and finally falls back to lowercase field names This function recursively processes embedded structs to include their fields\n\n```go\nfunc GetPointerElement(v reflect.Type) reflect.Type\n```\nGetPointerElement returns the element type if the provided reflect.Type is a pointer. If the type is a slice of pointers, it returns the element type of the pointer within the slice. If neither condition is met, it returns the original type.\n\n```go\nfunc GetPrimaryKeyName(model any) string\n```\nGetPrimaryKeyName extracts the primary key column name from a model It first checks if the model implements PrimaryKeyNameProvider (GetIDName method) Falls back to reflection to find bun:\",pk\" tag, then gorm:\"primaryKey\" tag\n\n```go\nfunc GetPrimaryKeyValue(model any) any\n```\nGetPrimaryKeyValue extracts the primary key value from a model instance Returns the value of the primary key field\n\n```go\nfunc GetRelationModel(model interface{}, fieldName string) interface{}\n```\nGetRelationModel gets the model type for a relation field It searches for the field by name in the following order (case-insensitive): 1. Actual field name 2. Bun tag name (if exists) 3. Gorm tag name (if exists) 4. JSON tag name (if exists)\n\nSupports recursive field paths using dot notation (e.g., \"MAL.MAL.DEF\") For nested fields, it traverses through each level of the struct hierarchy\n\n```go\nfunc GetSQLModelColumns(model any) []string\n```\nGetSQLModelColumns extracts column names that have valid SQL field mappings This function only returns columns that: 1. Have bun or gorm tags (not just json tags) 2. Are not relations (no rel:, join:, foreignKey, references, many2many tags) 3. Are not scan-only embedded fields\n\n```go\nfunc GetValidJSONFieldNames(modelType reflect.Type) map[string]bool\n```\nGetValidJSONFieldNames returns a map of valid JSON field names for a model This can be used to validate input data against a model's structure The map keys are the JSON field names (from json tags) that exist in the model\n\n```go\nfunc HasColumn(model any, columnName string) bool\n```\nHasColumn reports whether the model has a struct field that bun/gorm would scan a column named columnName into. Unlike GetSQLModelColumns, this includes scanonly fields (e.g. a \\`bun:\"jsonvalue\\_product\\_cost,scanonly\"\\` field added specifically to receive a computed/JSON-path SELECT expression) since those are legitimate scan targets even though they are not writable. Matching is case-insensitive against the resolved bun/gorm/json column name and against the bare Go field name.\n\n```go\nfunc IsCitextColumn(model interface{}, colName string) bool\n```\nIsCitextColumn reports whether colName carries an explicit \\`type:citext\\` bun/gorm tag. citext columns must never be CAST(... AS TEXT) for comparisons: that swaps in case-sensitive text semantics and defeats any citext index.\n\n```go\nfunc IsColumnWritable(model any, columnName string) bool\n```\nIsColumnWritable checks if a column can be written to in the database For bun: returns false if the field has \"scanonly\" tag For gorm: returns false if the field has \"\\\u003c-:false\" or \"-\u003e\" (read-only) tag This function recursively searches embedded structs\n\n```go\nfunc IsEmptyValue(v any) bool\n```\nIsEmptyValue reports whether v is nil, an empty string, or a zero number.\n\n```go\nfunc IsJSONColumn(model interface{}, colName string) bool\n```\nIsJSONColumn reports whether colName is backed by a JSON/JSONB column on the model. It recognises the spectypes SqlJSONB wrapper, encoding/json.RawMessage, map-typed fields, and fields carrying a bun/gorm \\`type:json\\` / \\`type:jsonb\\` tag. colName should be a bare column name (callers pass the parsed base column of a JSON path, not the full \"col-\u003e\u003e'x'\" expression).\n\n```go\nfunc IsNumericType(kind reflect.Kind) bool\n```\nIsNumericType checks if a reflect.Kind is a numeric type\n\n```go\nfunc IsNumericValue(value string) bool\n```\nIsNumericValue checks if a string value can be parsed as a number\n\n```go\nfunc IsSpatialColumn(model interface{}, colName string) bool\n```\nIsSpatialColumn reports whether colName is backed by a PostGIS geometry/geography wrapper.\n\n```go\nfunc IsStringType(kind reflect.Kind) bool\n```\nIsStringType checks if a reflect.Kind is a string type\n\n```go\nfunc IsVectorColumn(model interface{}, colName string) bool\n```\nIsVectorColumn reports whether colName is backed by a pgvector wrapper.\n\n```go\nfunc Len(v any) int\n```\n\n```go\nfunc MapToStruct(dataMap map[string]interface{}, target interface{}) error\n```\nMapToStruct populates a struct from a map while preserving custom types It uses reflection to set struct fields based on map keys, matching by: 1. Bun tag column name 2. Gorm tag column name 3. JSON tag name 4. Field name (case-insensitive) This preserves custom types that implement driver.Valuer like SqlJSONB\n\n```go\nfunc NonWritableColumns(model any) []string\n```\nNonWritableColumns returns the column names of the model that cannot be written (bun scanonly/generated, gorm read-only), including embedded structs.\n\n```go\nfunc RemoveNonWritableColumns(model any, values map[string]interface{})\n```\nRemoveNonWritableColumns deletes from values every key that maps to a non-writable model column (bun scanonly/generated, gorm read-only). Used before writing a read-merged record back with UPDATE ... SET.\n\n```go\nfunc ToSnakeCase(s string) string\n```\nToSnakeCase converts a string from CamelCase to snake\\_case Handles consecutive uppercase letters (acronyms) correctly: \"HTTPServer\" -\u003e \"http\\_server\", \"UserID\" -\u003e \"user\\_id\", \"MyHTTPServer\" -\u003e \"my\\_http\\_server\"\n\n## Types\n\n```go\ntype ModelFieldDetail struct {\n\tName        string        `json:\"name\"`\n\tDataType    string        `json:\"datatype\"`\n\tSQLName     string        `json:\"sqlname\"`\n\tSQLDataType string        `json:\"sqldatatype\"`\n\tSQLKey      string        `json:\"sqlkey\"`\n\tNullable    bool          `json:\"nullable\"`\n\tFieldValue  reflect.Value `json:\"-\"`\n}\n```\n\n```go\nfunc GetModelColumnDetail(record reflect.Value) []ModelFieldDetail\n```\nGetModelColumnDetail - Get a list of columns in the SQL declaration of the model This function recursively processes embedded structs to include their fields\n\n```go\ntype PrimaryKeyNameProvider interface {\n\tGetIDName() string\n}\n```\n\n```go\ntype RelationType string\n```\nRelationType represents the type of database relationship\n\n```go\nconst (\n\tRelationHasMany    RelationType = \"has-many\"     // 1:N - use separate query\n\tRelationBelongsTo  RelationType = \"belongs-to\"   // N:1 - use JOIN\n\tRelationHasOne     RelationType = \"has-one\"      // 1:1 - use JOIN\n\tRelationManyToMany RelationType = \"many-to-many\" // M:N - use separate query\n\tRelationUnknown    RelationType = \"unknown\"\n)\n```\n\n```go\nfunc GetRelationType(model interface{}, fieldName string) RelationType\n```\nGetRelationType inspects the model's struct tags to determine the relationship type It checks both Bun and GORM tags to identify the relationship cardinality\n\n```go\nfunc (rt RelationType) ShouldUseJoin() bool\n```\nShouldUseJoin returns true if the relation type should use a JOIN instead of separate query\n\n","path":"github.com/bitechdev/ResolveSpec/pkg/reflection","name":"reflection","synopsis":"","isRedistributable":true}
