Initial import

This commit is contained in:
George Suntres
2026-03-29 10:32:25 -04:00
commit ebdf370c23
11 changed files with 515 additions and 0 deletions

203
struct_test.go Normal file
View File

@@ -0,0 +1,203 @@
package commons
import (
"testing"
)
func TestMapToStruct(t *testing.T) {
o := map[string]any {
"name": "name1",
"age": 22,
"active": true,
}
type O struct {
Name string
Age int
Active bool
}
var oo O
if err := MapToStruct(o, &oo); err != nil {
t.Fatalf("1 Failed to convert %v", err)
}
if oo.Name != "name1" || oo.Age != 22 || !oo.Active {
t.Fatalf("2 Failed to convert %v", oo)
}
}
func TestMapIsSubset(t *testing.T) {
super := map[string]any {
"name": "name1",
"age": 22,
"active": "2",
}
sub := map[string]any {
"name": "a",
}
if !MapIsSubset(sub, super) {
t.Fatal("Should have indicated that map is a subset")
}
}
func TestMapIsSubset_Nosubset(t *testing.T) {
super := map[string]any {
"name": "name1",
"age": 22,
"active": "2",
}
sub := map[string]any {
"foo": "a",
}
if MapIsSubset(sub, super) {
t.Fatal("Should have indicated that map is a NOT subset")
}
}
func TestMapIsSubsetOfStruct_NoSubset(t *testing.T) {
type O struct {
Name string
Age int `json:"age"`
Notes string
}
oo := map[string]any{
"name": "A Name",
"age": 1,
"notes": "dsafda",
"other": "dd",
}
if MapIsSubsetOfStruct(oo, &O{}) {
t.Fatal("Should have indicated that map is a NOT subset")
}
}
func TestMapIsSubsetOfStruct(t *testing.T) {
type O struct {
Name string
Age int `json:"age"`
Notes string
}
oo := map[string]any{
"Name": "A Name",
}
if !MapIsSubsetOfStruct(oo, &O{}) {
t.Fatal("Should have indicated that map is a subset")
}
}
func TestMapIsSubsetOfStruct_SomeFields(t *testing.T) {
type O struct {
Name string
Age int `json:"age"`
Notes string
}
oo := map[string]any{
"Name": "A Name",
"Foo": "bar",
}
if MapIsSubsetOfStruct(oo, &O{}) {
t.Fatal("Should have indicated that map is a NOT subset")
}
}
func TestStructHasJsonName_True(t *testing.T) {
type O struct {
Name string
Age int `json:"age"`
Notes string
}
o := &O{
}
if !StructHasJsonName(o, "age") {
t.Fatal("False negative for json tag")
}
}
func TestStructHasJsonName_False(t *testing.T) {
type O struct {
Name string
Age int `json:"age"`
Notes string
}
o := &O{
}
if StructHasJsonName(o, "foo") {
t.Fatal("False positive for json tag")
}
}
func TestStructHasBsonName_True(t *testing.T) {
type O struct {
Name string
Age int `bson:"age"`
Notes string
}
o := &O{
}
if !StructHasBsonName(o, "age") {
t.Fatal("False negative for bson tag")
}
}
func TestStructHasBsonName_False(t *testing.T) {
type O struct {
Name string
Age int `bson:"age"`
Notes string
}
o := &O{
}
if StructHasBsonName(o, "foo") {
t.Fatal("False positive for bson tag")
}
}
func TestCopyMatching(t *testing.T) {
type O struct {
Name string
Age int
Notes string
}
type OO struct {
Name string
Age int
Active bool
}
from := &O{
Name: "Nick",
Age: 15,
Notes: "Some notes about nick",
}
var to OO
StructCopyMatching(from, &to)
if to.Name != "Nick" && to.Age != 15 && to.Active != true {
t.Fatalf("Failed to copy matching fields to %v", to)
}
}