gitlab.com/evatix-go/core@v1.3.55/coredata/corepayload/newUserCreator.go (about) 1 package corepayload 2 3 import ( 4 "fmt" 5 6 "gitlab.com/evatix-go/core/coredata/corejson" 7 "gitlab.com/evatix-go/core/errcore" 8 "gitlab.com/evatix-go/core/isany" 9 ) 10 11 type newUserCreator struct{} 12 13 func (it newUserCreator) Deserialize( 14 rawJsonBytes []byte, 15 ) (*User, error) { 16 user := &User{} 17 18 err := corejson. 19 Deserialize. 20 UsingBytes( 21 rawJsonBytes, user) 22 23 if err == nil { 24 return user, nil 25 } 26 27 // has error 28 return nil, err 29 } 30 31 func (it newUserCreator) CastOrDeserializeFrom( 32 anyItem interface{}, 33 ) (*User, error) { 34 if isany.Null(anyItem) { 35 return nil, errcore. 36 CannotBeNilOrEmptyType. 37 ErrorNoRefs( 38 "given any item is nil failed to convert to user") 39 } 40 41 toUser := &User{} 42 err := corejson.CastAny.FromToDefault( 43 anyItem, 44 toUser) 45 46 return toUser, err 47 } 48 49 func (it newUserCreator) Empty() *User { 50 return &User{} 51 } 52 53 func (it newUserCreator) Create( 54 isSystemUser bool, 55 name, userType string, 56 ) *User { 57 return &User{ 58 Name: name, 59 Type: userType, 60 IsSystemUser: isSystemUser, 61 } 62 } 63 64 func (it newUserCreator) NonSysCreate( 65 name, userType string, 66 ) *User { 67 return &User{ 68 Name: name, 69 Type: userType, 70 } 71 } 72 73 func (it newUserCreator) NonSysCreateId( 74 id, name, userType string, 75 ) *User { 76 return &User{ 77 Identifier: id, 78 Name: name, 79 Type: userType, 80 } 81 } 82 83 func (it newUserCreator) System( 84 name, userType string, 85 ) *User { 86 return &User{ 87 Name: name, 88 Type: userType, 89 IsSystemUser: true, 90 } 91 } 92 93 func (it newUserCreator) SystemId( 94 id, name, userType string, 95 ) *User { 96 return &User{ 97 Identifier: id, 98 Name: name, 99 Type: userType, 100 IsSystemUser: true, 101 } 102 } 103 104 func (it newUserCreator) UsingNameTypeStringer( 105 name string, 106 userTypeStringer fmt.Stringer, 107 ) *User { 108 return &User{ 109 Name: name, 110 Type: userTypeStringer.String(), 111 } 112 } 113 114 func (it newUserCreator) SysUsingNameTypeStringer( 115 name string, 116 userTypeStringer fmt.Stringer, 117 ) *User { 118 return &User{ 119 Name: name, 120 Type: userTypeStringer.String(), 121 IsSystemUser: true, 122 } 123 } 124 125 func (it newUserCreator) UsingName( 126 name string, 127 ) *User { 128 return &User{ 129 Name: name, 130 } 131 } 132 133 func (it newUserCreator) All( 134 isSystemUser bool, 135 id, name, userType, authToken, passHash string, 136 ) *User { 137 return &User{ 138 Identifier: id, 139 Name: name, 140 Type: userType, 141 AuthToken: authToken, 142 PasswordHash: passHash, 143 IsSystemUser: isSystemUser, 144 } 145 } 146 147 func (it newUserCreator) AllTypeStringer( 148 isSystemUser bool, 149 id, name string, 150 userType fmt.Stringer, 151 authToken, passHash string, 152 ) *User { 153 return &User{ 154 Identifier: id, 155 Name: name, 156 Type: userType.String(), 157 AuthToken: authToken, 158 PasswordHash: passHash, 159 IsSystemUser: isSystemUser, 160 } 161 } 162 163 func (it newUserCreator) AllUsingStringer( 164 isSystemUser bool, 165 id, name string, 166 typeStringer fmt.Stringer, 167 authToken, passHash string, 168 ) *User { 169 return &User{ 170 Identifier: id, 171 Name: name, 172 Type: typeStringer.String(), 173 AuthToken: authToken, 174 PasswordHash: passHash, 175 IsSystemUser: isSystemUser, 176 } 177 }