field.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { getMetadataStorage } from "../utils/metadata-storage";
  2. import { FieldType, FieldTypes } from "../types/field";
  3. import { OdooFieldMetadata } from "../metadata/field-metadata";
  4. /**
  5. *
  6. */
  7. export function OdooField(): Function;
  8. /**
  9. *
  10. */
  11. export function OdooField(fieldName: string): Function;
  12. /**
  13. *
  14. */
  15. export function OdooField(type: FieldType): Function;
  16. /**
  17. *
  18. */
  19. export function OdooField(fieldName: string, type: FieldType);
  20. /**
  21. *
  22. */
  23. export function OdooField(fieldName?: string, type?: FieldType): Function {
  24. return function (object: Object, propertyName: string) {
  25. if (!name) {
  26. fieldName = propertyName;
  27. }
  28. if (!type) {
  29. const typeMetadata = Reflect && (Reflect as any).getMetadata ? (Reflect as any).getMetadata("design:type", object, propertyName) : undefined;
  30. if (typeMetadata) {
  31. type = FieldTypes.determineTypeFromFunction(typeMetadata);
  32. }
  33. }
  34. const args: OdooFieldMetadata = {
  35. target: object.constructor,
  36. fieldName: fieldName
  37. }
  38. getMetadataStorage().fields.add(args);
  39. }
  40. }