diff --git a/demo/29_uuid.yaml b/demo/29_uuid.yaml index bcda8d18222f67d486fc1f4b7c300386294226be..13c578dfbe9b21f02064460dba1fa04f880c7fd4 100644 --- a/demo/29_uuid.yaml +++ b/demo/29_uuid.yaml @@ -1,3 +1,9 @@ fields: - - field: field_uuid - format: "uuid(-)" + - field: f1 + from: uuid.v2.yaml + use: by_format + postfix: "\t" + + - field: f2 + from: ulid.v1.yaml + use: ulid{3} diff --git a/demo/t.yaml b/demo/t.yaml index fbc73d8bb49d8d2cb24d2aed33f8512f1c0893a8..8b24fdff119cd2b7260006a32a77aba7e8178d1f 100644 --- a/demo/t.yaml +++ b/demo/t.yaml @@ -6,4 +6,10 @@ version: 1.0 fields: - field: f1 - format: "uuid(- )" + from: uuid.v2.yaml + use: by_format + postfix: "\t" + + - field: f2 + from: ulid.v1.yaml + use: ulid{3} \ No newline at end of file diff --git a/src/gen/field.go b/src/gen/field.go index 22afa756d3f6d1f221d9ac993b322d4ae6cdc660..111a3b604184b3534c3098f830c82422ee59b869 100644 --- a/src/gen/field.go +++ b/src/gen/field.go @@ -22,6 +22,8 @@ func CreateField(field *model.DefField) model.FieldWithValues { CreateListField(field, &fieldWithValue) } else if field.Type == constant.FieldTypeTimestamp { CreateTimestampField(field, &fieldWithValue) + } else if field.Type == constant.FieldTypeUlid { + CreateUlidField(field, &fieldWithValue) } else if field.Type == constant.FieldTypeArticle { CreateArticleField(field, &fieldWithValue) } diff --git a/src/gen/timestampVar.go b/src/gen/timestampVar.go index ee9bed7b3db4315d7638c6f41251131c8bbb8202..6c6a5a74f5bb40690e1d1c65faa729f3f1eac7ee 100644 --- a/src/gen/timestampVar.go +++ b/src/gen/timestampVar.go @@ -5,6 +5,9 @@ import ( "github.com/easysoft/zendata/src/model" constant "github.com/easysoft/zendata/src/utils/const" stringUtils "github.com/easysoft/zendata/src/utils/string" + "github.com/easysoft/zendata/src/utils/vari" + "github.com/oklog/ulid/v2" + "math/rand" "strconv" "strings" "time" @@ -30,6 +33,23 @@ func CreateTimestampField(field *model.DefField, fieldWithValue *model.FieldWith fieldWithValue.Values = values } +func CreateUlidField(field *model.DefField, fieldWithValue *model.FieldWithValues) { + count := 0 + + t := time.Unix(1000000, 0) + entropy := ulid.Monotonic(rand.New(rand.NewSource(t.UnixNano())), 0) + + for true { + val := ulid.MustNew(ulid.Timestamp(t), entropy).String() + fieldWithValue.Values = append(fieldWithValue.Values, val) + + count++ + if count >= constant.MaxNumb || count > vari.Total { + break + } + } +} + func convertTmFormat(field *model.DefField) { // to 2006-01-02 15:04:05 format := field.Format diff --git a/src/utils/const/const.go b/src/utils/const/const.go index 7639e7b3387a3e4aee163e7f07349a2cd405491e..e01eb9e097b29713c138a78008631189c75266b9 100644 --- a/src/utils/const/const.go +++ b/src/utils/const/const.go @@ -56,6 +56,7 @@ var ( FieldTypeList = "list" FieldTypeTimestamp = "timestamp" + FieldTypeUlid = "ulid" FieldTypeArticle = "article" LeftBrackets rune = '(' diff --git a/yaml/ulid/v1.yaml b/yaml/ulid/v1.yaml new file mode 100644 index 0000000000000000000000000000000000000000..3ae67fe5c0f7be442281586a85c3b1b325cd7d25 --- /dev/null +++ b/yaml/ulid/v1.yaml @@ -0,0 +1,12 @@ +title: ULID +desc: 通用唯一词典分类标识符。 +author: wwccss +version: 1.0 + +field: ulid +instances: + - instance: ulid + note: 标准26位长度,按字母升序排序。 + fields: + - field: part1 + type: ulid \ No newline at end of file diff --git a/yaml/uuid/v2.yaml b/yaml/uuid/v2.yaml new file mode 100644 index 0000000000000000000000000000000000000000..9296fe5d3f961a3fbb9e99b13ee1de899b76aa27 --- /dev/null +++ b/yaml/uuid/v2.yaml @@ -0,0 +1,13 @@ +title: UUID +desc: 通用唯一识别码。 +author: wwccss +version: 1.0 + +field: uuid +instances: + - instance: by_format + note: 以-为分隔符的随机UUID + fields: + - field: part1 + range: 1-9 + format: "uuid(-)" \ No newline at end of file