-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
123 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,41 @@ | ||
package data | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
) | ||
// func (manager *TemplateManager) CreateModelObjectAction(objectType reflect.Type, objectData map[string]interface{}) { | ||
// // Check if the type is a struct or a pointer to a struct | ||
// if objectType.Kind() != reflect.Struct { | ||
// if objectType.Kind() == reflect.Ptr && objectType.Elem().Kind() == reflect.Struct { | ||
// objectType = objectType.Elem() | ||
// } else { | ||
// panic("provided type is not a struct and not a pointer to a struct") | ||
// } | ||
// } | ||
|
||
func (manager *TemplateManager) CreateModelObjectAction(objectType reflect.Type, objectData map[string]interface{}) { | ||
// Check if the type is a struct or a pointer to a struct | ||
if objectType.Kind() != reflect.Struct { | ||
if objectType.Kind() == reflect.Ptr && objectType.Elem().Kind() == reflect.Struct { | ||
objectType = objectType.Elem() | ||
} else { | ||
panic("provided type is not a struct and not a pointer to a struct") | ||
} | ||
} | ||
// // Create a new struct of the provided type | ||
// newObject := reflect.New(objectType).Elem() | ||
|
||
// Create a new struct of the provided type | ||
newObject := reflect.New(objectType).Elem() | ||
// // Iterate through the map and set field values | ||
// for fieldName, value := range objectData { | ||
// fieldVal := newObject.FieldByName(fieldName) | ||
// if !fieldVal.IsValid() { | ||
// fmt.Printf("No such field: %s in obj\n", fieldName) | ||
// continue | ||
// } | ||
// if !fieldVal.CanSet() { | ||
// fmt.Printf("Cannot set %s field value\n", fieldName) | ||
// continue | ||
// } | ||
|
||
// Iterate through the map and set field values | ||
for fieldName, value := range objectData { | ||
fieldVal := newObject.FieldByName(fieldName) | ||
if !fieldVal.IsValid() { | ||
fmt.Printf("No such field: %s in obj\n", fieldName) | ||
continue | ||
} | ||
if !fieldVal.CanSet() { | ||
fmt.Printf("Cannot set %s field value\n", fieldName) | ||
continue | ||
} | ||
// fieldValue := reflect.ValueOf(value) | ||
// if fieldVal.Type() != fieldValue.Type() { | ||
// // Types do not match, attempt to convert types | ||
// if fieldValue.Type().ConvertibleTo(fieldVal.Type()) { | ||
// fieldValue = fieldValue.Convert(fieldVal.Type()) | ||
// } else { | ||
// fmt.Printf("Provided value type didn't match obj field type and could not be converted\n") | ||
// continue | ||
// } | ||
// } | ||
|
||
fieldValue := reflect.ValueOf(value) | ||
if fieldVal.Type() != fieldValue.Type() { | ||
// Types do not match, attempt to convert types | ||
if fieldValue.Type().ConvertibleTo(fieldVal.Type()) { | ||
fieldValue = fieldValue.Convert(fieldVal.Type()) | ||
} else { | ||
fmt.Printf("Provided value type didn't match obj field type and could not be converted\n") | ||
continue | ||
} | ||
} | ||
|
||
fieldVal.Set(fieldValue) | ||
} | ||
} | ||
// fieldVal.Set(fieldValue) | ||
// } | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters